def test_save_msg(msg='hahah', tag='test tag', talker='admin', expiry=None, time=None, force_create=True): """test save a message""" interface = DataTransfer(Database(PATH)) msg = MsgWithTag(msg, tag, talker, expiry, time) interface.save_msg(msg, force_create=force_create) assert interface.get_msg_by_id(1) is not None
def test_get_all_id_and_tags(): """test get all id and tags""" interface = DataTransfer(Database(PATH)) all_data = interface.get_all_id_and_tags() assert len(all_data) == 2 assert all_data[0][1] == 'test 2' assert all_data[1][1] == 'test 3'
def test_get_msgs_by_time_range(): """test get msgs by time range""" interface = DataTransfer(Database(PATH)) start = datetime.now() + timedelta(days=10) end = start + timedelta(days=20) for i in range(10): test_save_msg(time=start + timedelta(days=i)) assert len(interface.get_msgs_by_time_range(start, end)) == 10
def test_get_all_msgs(): """test get all messages""" test_save_msg('second', 'test 2', 'admin2', datetime(2020, 3, 26)) test_save_msg('third', 'test 3') interface = DataTransfer(Database(PATH)) all_msg = interface.get_all_msgs() _, msg = all_msg[0] assert len(all_msg) == 2 assert msg.expiry == datetime(2020, 3, 26)
def test_expiry(): """test expiry of messages""" interface = DataTransfer(Database(PATH)) controller = TagController(interface) data_num = len(interface.get_all_msgs()) expiry = datetime.now() + timedelta(seconds=1) msg = f'test expiry, {KEY_EXPIRY}{KEY_SPLIT}{str(expiry)}' controller.handle_msg(quoted='second', msg=msg, talker='me', to_bot=True) sleep(1) assert data_num == len(interface.get_all_msgs())
def test_update(): """test insert duplicated message with different tag""" interface = DataTransfer(Database(PATH)) test_save_msg(msg='update message', tag='test update') data_num = len(interface.get_all_msgs()) test_save_msg(msg='update message', tag='test update 2', force_create=False) all_msg = interface.get_all_msgs() assert len(all_msg) == data_num assert all_msg[-1][1].msg == 'update message' assert all_msg[-1][1].tags == 'test update 2'
def test_save_msg(): """test save messaga""" interface = DataTransfer(Database(PATH)) controller = TagController(interface) ifreply = controller.handle_msg(quoted='first', msg='test save msg', talker='me', to_bot=True) assert ifreply assert controller.reply == reply.save_msg_success() assert len(interface.get_all_msgs()) == 1
def init_data(): """create tag_controller""" for path in [PATH, CONFIG]: try: os.remove(path) except FileNotFoundError: pass interface = DataTransfer(Database(PATH, CONFIG)) for quoted, tag, in MSG_CONTENTS: msg = MsgWithTag(quoted, tag, 'me') # talker is not important here interface.save_msg(msg) return interface
def test_delete(): """test delete message""" interface = DataTransfer(Database(PATH)) controller = TagController(interface) data_num = len(interface.get_all_msgs()) ifreply = controller.handle_msg(quoted=None, msg=f'{KEY_DELETE} {KEY_SPLIT}1', talker='me', to_bot=True) assert ifreply assert controller.reply == reply.del_msg_success(1) assert len(interface.get_all_msgs()) == data_num - 1
def test_not_reply(): """not saying to the bot""" controller = TagController(DataTransfer(Database(PATH, CONFIG))) reply = controller.handle_msg(quoted='null', msg='null', talker='me', to_bot=False) assert not reply
def test_create(): """create table""" for path in [PATH, 'database_config.pkl']: try: os.remove(path) except FileNotFoundError: pass interface = DataTransfer(Database(PATH))
def test_create(): """create tag_controller""" for path in [PATH, CONFIG]: try: os.remove(path) except FileNotFoundError: pass controller = TagController(DataTransfer(Database(PATH, CONFIG)))
def test_wrong_cron_format(): """test timed delete with wrong format""" controller = TagController(DataTransfer(Database(PATH))) ifreply = controller.handle_msg(quoted=None, msg=f'{KEY_DELETE} {KEY_SPLIT}*-*-0-0-0-1', talker='me', to_bot=True) assert ifreply assert controller.reply == reply.parse_datetime_error()
def __init__(self, data_path='database.db', config_path='config.pkl'): """params: data_path: path of database file, config_path: path of database config file, which contains auto-created information""" self.data_path = data_path self.config_path = config_path self.interface = DataTransfer(Database(data_path, config_path)) self.question_answering = QuestionAnswering(self.interface) self.tag_controller = TagController(self.interface) self.display = Display(self.interface)
def test_del_by_time(): """test delete by time stamp""" interface = DataTransfer(Database(PATH)) data_num = len(interface.get_all_msgs()) test_save_msg(time=datetime.now() - timedelta(days=3)) interface.del_msg_by_timedelta(timedelta(days=3)) assert len(interface.get_all_msgs()) == data_num
def test_timed_delete(): """test timed delete""" controller = TagController(DataTransfer(Database(PATH))) ifreply = controller.handle_msg(quoted=None, msg=f'{KEY_DELETE} {KEY_SPLIT}*-*-2-0-0-1', talker='me', to_bot=True) assert ifreply # substitute former plan ifreply = controller.handle_msg(quoted=None, msg=f'{KEY_DELETE} {KEY_SPLIT}*-*-*-*-*-1', talker='me', to_bot=True) assert ifreply
def test_display_msg(): """test display message function""" start = datetime.now() init_data() end = datetime.now() interface = DataTransfer(Database(PATH, CONFIG)) display = Display(interface) assert display.handle_msg(KEY_DISPLAY, True) assert len(display.reply.split('\n')) == len(MSG_CONTENTS) assert display.handle_msg( f'{KEY_DISPLAY}{KEY_SPLIT}' f'{start.year}.{start.month}.{start.day}' f'-{end.year}.{end.month}.{end.day}', True) assert len(display.reply.split('\n')) == len(MSG_CONTENTS) assert display.handle_msg(f'{KEY_DISPLAY}{KEY_SPLIT}aaaa-aaaaa', True) assert display.reply == reply.parse_datetime_error()
def test_stop_timed_delete(): """test stop timed delete task""" controller = TagController(DataTransfer(Database(PATH))) controller.handle_msg(quoted=None, msg=f'{KEY_DELETE} {KEY_SPLIT}*-*-2-0-0-1', talker='me', to_bot=True) ifreply = controller.handle_msg(quoted=None, msg=f'{KEY_DELETE}{KEY_SPLIT}{KEY_STOP}', talker='me', to_bot=True) assert ifreply assert controller.reply == reply.stop_timed_delete(True) ifreply = controller.handle_msg(quoted=None, msg=f'{KEY_DELETE}{KEY_SPLIT}{KEY_STOP}', talker='me', to_bot=True) assert ifreply assert controller.reply == reply.stop_timed_delete(False)
def test_del_msg_by_id(): """test delete a message""" interface = DataTransfer(Database(PATH)) interface.del_msg_by_id(1) assert interface.get_msg_by_id(1) is None