def on_starting(server): #test if menu is on cloader = ConfigLoader('./resource.ini') linebot = LineBotApi(cloader.fetch_config(resource.F_CHANNEL_ACCESS_TOKEN)) if 'RESET_MENU' in os.environ or len(linebot.get_rich_menu_list()) == 0: print("Initialize rich menus.") menu.init_menu(linebot)
def initPostgresql(filename): cloader = ConfigLoader(filename) access = cloader.db_access_config() access['db_type'] = 'postgresql' database = db(**access) print(database) with database.get_connection() as conn: with conn.cursor() as cursor: print("Try to remove table", resource.DB_USERINFO_TNAME) cursor.execute("DROP TABLE IF EXISTS userinfo") print("Create table", resource.DB_USERINFO_TNAME) cursor.execute("""CREATE TABLE userinfo ( id SERIAL PRIMARY KEY NOT NULL, user_id TEXT NOT NULL UNIQUE, notify BOOLEAN NOT NULL DEFAULT true, note TEXT );""") conn.commit()
def init(config_file): """ Initialization for linebot This function update the content of rich menu This function should be called on the new setup of linebot. """ cloader = ConfigLoader(config_file) yunlib = Yunlib(config_file) linebot = yunlib.linebot # Delete old menu for menu in linebot.get_rich_menu_list(): linebot.delete_rich_menu(menu.rich_menu_id) notify_menu = RichMenu( size = resource.RichMenuSize, name = resource.NotifyMenuName, chat_bar_text= resource.chatBarText, areas = create_slots( resource.NotifyMenuLabels, resource.NotifyMenuText, resource.NotifyMenuData, x_array = resource.RichMenuAreaX, y_array = resource.RichMenuAreaY, size_x = resource.RichMenuAreaSizeW, size_y = resource.RichMenuAreaSizeH ), selected = False ) nnotify_menu = RichMenu( size = resource.RichMenuSize, name = resource.NNotifyMenuName, chat_bar_text= resource.chatBarText, areas = create_slots( resource.NNotifyMenuLabels, resource.NNotifyMenuText, resource.NNotifyMenuData, x_array = resource.RichMenuAreaX, y_array = resource.RichMenuAreaY, size_x = resource.RichMenuAreaSizeW, size_y = resource.RichMenuAreaSizeH ), selected = False ) id1 = linebot.create_rich_menu(notify_menu) id2 = linebot.create_rich_menu(nnotify_menu) with open('./misc/icon_notify.png','rb') as f: linebot.set_rich_menu_image(id1,"image/png",f) with open('./misc/icon_nnotify.png','rb') as f: linebot.set_rich_menu_image(id2,"image/png",f)
class UnitTestPostgresql(unittest.TestCase): def setUp(self): self.cloader = ConfigLoader('./resource.ini') access = self.cloader.db_access_config() access['db_type'] = 'postgresql' self.database = db(**access) def test_connection_up(self): with self.database.get_connection().cursor() as c: c.execute('SELECT true') self.assertTrue(c.fetchone()[0] == True) with self.database.get_connection().cursor() as c: c.execute('SELECT true') self.assertFalse(c.fetchone()[0] == False) def test_crud(self): """Test CRUD""" user = {"user_id": "FAKE_USER", "notify": True, "note": "TEST"} letters = string.ascii_lowercase user['user_id'] += ''.join(random.choice(letters) for i in range(5)) self.database.userinfo.insert(**user) query = self.database.userinfo.query_by_id(user['user_id']) self.assertTrue(query[1] == user['user_id']) self.assertTrue(query[2] == user['notify']) self.assertTrue(query[3] == user['note']) print(self.database.userinfo.query_by_id(user['user_id'])) trigger1 = self.database.userinfo.is_notify_on(user['user_id']) print(self.database.userinfo.query_by_id(user['user_id'])) self.assertTrue(self.database.userinfo.is_notify_on(user['user_id'])) self.database.userinfo.update_notify(user['user_id'], not trigger1) self.assertFalse(self.database.userinfo.is_notify_on(user['user_id'])) print(self.database.userinfo.query_by_id(user['user_id'])) trigger2 = self.database.userinfo.is_notify_on(user['user_id']) print(self.database.userinfo.query_by_id(user['user_id'])) self.assertFalse(trigger1 == trigger2) self.database.userinfo.delete(user['user_id']) self.database.userinfo.querys()
def setUp(self): self.cloader = ConfigLoader('./resource.ini') access = self.cloader.db_access_config() access['db_type'] = 'sqlite3' self.database = db(**access)