class TestAnnounceCommandHandler(PtbTestCase): def setUp(self): PtbTestCase.setUp(self) from components.user.models import db db.drop_all_tables(with_all_data=True) db.create_tables() self.service = UserService() self.admin_id = 42 self.handler = AnnounceCommandHandler(self.admin_id, texts, TelegramService(self.service), self.service) def test_admin_announce_no_text(self): self.updater.dispatcher.add_handler( CommandHandler("admin-announce", self.handler.admin_announce)) self.updater.start_polling() update = self.mg.get_message(text="/admin-announce") update.effective_user.id = self.admin_id self.bot.insertUpdate(update) self.assertEqual(2, len(self.bot.sent_messages)) sent = self.bot.sent_messages[0] self.assertEqual("sendChatAction", sent['method']) self.assertEqual("typing", sent['action']) sent = self.bot.sent_messages[1] self.assertEqual("sendMessage", sent['method']) self.assertEqual(texts['missing-text'](texts['admin']), sent['text']) self.updater.stop() def test_admin_announce_valid_text_users_exist(self): self.updater.dispatcher.add_handler( CommandHandler("admin-announce", self.handler.admin_announce)) self.updater.start_polling() text = "a very special announcement" update = self.mg.get_message(text=f"/admin-announce {text}") update.effective_user.id = self.admin_id self.service.add_user_chat_if_not_exists(update.effective_user.id, update.effective_chat.id) self.bot.insertUpdate(update) time.sleep(1) # the message takes some time to be sent... self.assertEqual(3, len(self.bot.sent_messages)) sent = self.bot.sent_messages[0] self.assertEqual("sendChatAction", sent['method']) self.assertEqual("typing", sent['action']) # the announcement sent to the user sent = self.bot.sent_messages[1] self.assertEqual("sendMessage", sent['method']) self.assertTrue(text in sent['text']) # the confirmation for the admin sent = self.bot.sent_messages[2] self.assertEqual("sendMessage", sent['method']) self.assertEqual(texts['announcement-sent'](1), sent['text']) update.effective_user.id = self.admin_id # set the user id for the admin check update.message.chat.id = self.admin_id # set the user id for the reply self.updater.stop()
def setUp(self): from components.user.models import db from components.user.user_service import UserService self.service = UserService() self.user1_id = 1 self.chat1_id = 2 db.drop_all_tables(with_all_data=True) db.create_tables() self.db = db
def setUp(self): PtbTestCase.setUp(self) from components.user.models import db db.drop_all_tables(with_all_data=True) db.create_tables() self.service = UserService() self.admin_id = 42 self.handler = AnnounceCommandHandler(self.admin_id, texts, TelegramService(self.service), self.service)
class TestFeedbackService(PtbTestCase): def setUp(self): PtbTestCase.setUp(self) self.user_service = UserService() self.service = TelegramService(self.user_service) def test_is_private_chat_is_private_chat(self): update = self.mg.get_message(text="/help") is_private_chat = self.service.is_private_chat(update) self.assertTrue(is_private_chat) def test_is_private_chat_is_group_chat(self): chat = self.cg.get_chat(type="group") update = self.mg.get_message(chat=chat) is_private_chat = self.service.is_private_chat(update) self.assertFalse(is_private_chat) def test_get_mention(self): user_result = types.SimpleNamespace() user_result.user = self.ug.get_user() self.bot.getChatMember = MagicMock(return_value=user_result) user_id = 3 actual = self.service.get_mention(self.bot, 2, user_id) self.assertEqual(f"[{user_result.user.name}](tg://user?id={user_id})", actual) def test_get_chat_users(self): user_result = types.SimpleNamespace() user_result.user = self.ug.get_user() self.bot.getChatMember = MagicMock(return_value=user_result) chat_id = 2 self.user_service.add_user_chat_if_not_exists(3, chat_id) actual = self.service.get_chat_users(self.bot, chat_id) self.assertGreater(len(actual), 0) def test_get_chats(self): self.bot.getChat = MagicMock(return_value=self.cg.get_chat()) user_id = 2 self.user_service.add_user_chat_if_not_exists(user_id, 3) actual = self.service.get_chats(self.bot, user_id) self.assertGreater(len(actual), 0)
def setUp(self): PtbTestCase.setUp(self) from components.user.models import db from components.user.user_service import UserService db.drop_all_tables(with_all_data=True) db.create_tables() self.db = db self.admin_id = 42 self.service = UserService() self.bot_name = "botname" self.handler = UserCommandHandler(self.admin_id, texts, TelegramService(self.service), self.bot_name, self.service)
def setUp(self): PtbTestCase.setUp(self) self._init_bot() from components.user.models import db db.drop_all_tables(with_all_data=True) db.create_tables() self.user_service = UserService() self.task_service = TaskService() self.admin_id = 42 self.job_log_service = MockJobLogService() show_far_future_tasks = 5 if date.today().weekday() != 5 else 6 self.handler = DoForMeCommandHandler( self.admin_id, texts, TelegramService(self.user_service), "bot-name", self.task_service, self.user_service, FeedbackService(), show_far_future_tasks, self.job_log_service)
def create_bot(admin_id: int, heartbeat_monitor_url: str): user_service = UserService() telegram_service = TelegramService(user_service) core_command_handler = CoreCommandHandler(admin_id, core_texts, telegram_service) user_command_handler = UserCommandHandler(admin_id, user_texts, telegram_service, bot_name, user_service) feedback_service = FeedbackService() feedback_command_handler = FeedbackCommandHandler(admin_id, feedback_texts, telegram_service, feedback_service) announce_command_handler = AnnounceCommandHandler(admin_id, announce_texts, telegram_service, user_service) quest_service = QuestService() quest_command_handler = QuestCommandHandler(admin_id, quest_texts, telegram_service, bot_name, user_service, quest_service, heartbeat_monitor_url) components = { 'core': CoreComponent(core_command_handler), 'feedback': FeedbackComponent(feedback_command_handler), 'user': UserComponent(user_command_handler), 'announce': AnnounceComponent(announce_command_handler), 'quest': QuestComponent(quest_command_handler), } bot = CommonBot(components, get_logger()) return bot
def create_bot(admin_id: int): user_service = UserService() telegram_service = TelegramService(user_service) core_command_handler = CoreCommandHandler(admin_id, core_texts, telegram_service) user_command_handler = UserCommandHandler(admin_id, user_texts, telegram_service, bot_name, user_service) feedback_service = FeedbackService() feedback_command_handler = FeedbackCommandHandler(admin_id, feedback_texts, telegram_service, feedback_service) announce_command_handler = AnnounceCommandHandler(admin_id, announce_texts, telegram_service, user_service) task_service = TaskService() job_log_service = JobLogService() doforme_command_handler = DoForMeCommandHandler(admin_id, doforme_texts, telegram_service, bot_name, task_service, user_service, feedback_service, 5, job_log_service) components = { 'core': CoreComponent(core_command_handler), 'feedback': FeedbackComponent(feedback_command_handler), 'user': UserComponent(user_command_handler), 'announce': AnnounceComponent(announce_command_handler), 'doforme': DoForMeComponent(doforme_command_handler), } bot = CommonBot(components, get_logger()) return bot
def setUp(self): self.bot = ptbtest.Mockbot() self.ug = ptbtest.UserGenerator() self.cg = ptbtest.ChatGenerator() self.mg = ptbtest.MessageGenerator(self.bot) self.updater = Updater(bot=self.bot) self.handler = CoreCommandHandler(0, {'help': 'test'}, TelegramService(UserService()))
def setUp(self): PtbTestCase.setUp(self) from components.feedback.models import db from components.feedback.feedback_service import FeedbackService db.drop_all_tables(with_all_data=True) db.create_tables() self.db = db self.admin_id = 42 self.service = FeedbackService() self.handler = FeedbackCommandHandler(self.admin_id, texts, TelegramService(UserService()), self.service)
def create_bot(admin_id: int): user_service = UserService() telegram_service = TelegramService(user_service) core_command_handler = CoreCommandHandler(admin_id, core_texts, telegram_service) user_command_handler = UserCommandHandler(admin_id, user_texts, telegram_service, bot_name, user_service) feedback_service = FeedbackService() feedback_command_handler = FeedbackCommandHandler(admin_id, feedback_texts, telegram_service, feedback_service) announce_command_handler = AnnounceCommandHandler(admin_id, announce_texts, telegram_service, user_service) components = { # put your custom components at the top to be able to overwrite commands 'feedback': FeedbackComponent(feedback_command_handler), 'user': UserComponent(user_command_handler), 'announce': AnnounceComponent(announce_command_handler), 'core': CoreComponent(core_command_handler), } bot = CommonBot(components, get_logger()) return bot
def setUp(self): PtbTestCase.setUp(self) self.user_service = UserService() self.service = TelegramService(self.user_service)
class TestUserCommandHandler(PtbTestCase): admin_id: int bot_name: str @mock.patch.dict(os.environ, {'DFM_ENV': 'Test'}) def setUp(self): PtbTestCase.setUp(self) from components.user.models import db from components.user.user_service import UserService db.drop_all_tables(with_all_data=True) db.create_tables() self.db = db self.admin_id = 42 self.service = UserService() self.bot_name = "botname" self.handler = UserCommandHandler(self.admin_id, texts, TelegramService(self.service), self.bot_name, self.service) def test_chat_member_add_no_new_chat_members_private_chat_no_chat_exists_for_user( self): self.updater.dispatcher.add_handler( MessageHandler(Filters.status_update.new_chat_members, self.handler.chat_member_add)) self.updater.dispatcher.add_handler( MessageHandler(Filters.text, self.handler.chat_member_add)) self.updater.start_polling() update = self.mg.get_message(text="hello!") self.bot.insertUpdate(update) self.assertEqual(1, len(self.bot.sent_messages)) # info message sent = self.bot.sent_messages[0] self.assertEqual("sendMessage", sent['method']) self.assertEqual(texts['add-to-group'], sent['text']) self.updater.stop() def test_chat_member_add_no_new_chat_members_private_chat_exists_for_user( self): self.updater.dispatcher.add_handler( MessageHandler(Filters.status_update.new_chat_members, self.handler.chat_member_add)) self.updater.dispatcher.add_handler( MessageHandler(Filters.text, self.handler.chat_member_add)) self.updater.start_polling() update = self.mg.get_message(text="hello!") self.service.add_user_chat_if_not_exists(update.effective_user.id, update.effective_chat.id) self.bot.insertUpdate(update) self.assertEqual(1, len(self.bot.sent_messages)) # help message sent = self.bot.sent_messages[0] self.assertEqual("sendMessage", sent['method']) self.assertEqual(texts['help'], sent['text']) self.updater.stop() def test_chat_member_add_no_new_chat_members_group_user_does_not_exist( self): self.updater.dispatcher.add_handler( MessageHandler(Filters.status_update.new_chat_members, self.handler.chat_member_add)) self.updater.dispatcher.add_handler( MessageHandler(Filters.text, self.handler.chat_member_add)) self.updater.start_polling() chat = self.cg.get_chat(type="group") update = self.mg.get_message(text="hello!", chat=chat) self.bot.insertUpdate(update) time.sleep(1) # the message takes some time to be sent... self.assertEqual(1, len(self.bot.sent_messages)) # welcome message sent = self.bot.sent_messages[0] self.assertEqual("sendMessage", sent['method']) self.assertTrue(update.effective_user.first_name in sent['text']) self.updater.stop() def test_chat_member_add_new_chat_member(self): self.updater.dispatcher.add_handler( MessageHandler(Filters.status_update.new_chat_members, self.handler.chat_member_add)) self.updater.dispatcher.add_handler( MessageHandler(Filters.text, self.handler.chat_member_add)) self.updater.start_polling() user = self.ug.get_user() chat = self.cg.get_chat(type="group") update = self.mg.get_message(text="hello!", chat=chat) update.message.new_chat_members = [user] self.bot.insertUpdate(update) self.assertEqual(1, len(self.bot.sent_messages)) # welcome message sent = self.bot.sent_messages[0] self.assertEqual("sendMessage", sent['method']) self.assertTrue(user.first_name in sent['text']) self.updater.stop() def test_chat_member_add_new_chat_member_bot(self): self.updater.dispatcher.add_handler( MessageHandler(Filters.status_update.new_chat_members, self.handler.chat_member_add)) self.updater.dispatcher.add_handler( MessageHandler(Filters.text, self.handler.chat_member_add)) self.updater.start_polling() user = self.ug.get_user(is_bot=True, username=self.bot_name) chat = self.cg.get_chat(type="group") update = self.mg.get_message(text="hello!", chat=chat) update.message.new_chat_members = [user] self.bot.insertUpdate(update) self.assertEqual(1, len(self.bot.sent_messages)) # welcome message sent = self.bot.sent_messages[0] self.assertEqual("sendMessage", sent['method']) self.assertEqual(texts['welcome-bot'], sent['text']) self.updater.stop() def test_chat_member_add_no_new_chat_members_group_user_exists(self): self.updater.dispatcher.add_handler( MessageHandler(Filters.status_update.new_chat_members, self.handler.chat_member_add)) self.updater.dispatcher.add_handler( MessageHandler(Filters.text, self.handler.chat_member_add)) self.updater.start_polling() chat = self.cg.get_chat(type="group") update = self.mg.get_message(text="hello!", chat=chat) self.service.add_user_chat_if_not_exists(update.effective_user.id, chat.id) self.bot.insertUpdate(update) self.assertEqual(0, len(self.bot.sent_messages)) # welcome message self.updater.stop() def test_chat_member_remove_chat_member_does_not_exist(self): self.updater.dispatcher.add_handler( MessageHandler(Filters.status_update.left_chat_member, self.handler.chat_member_remove)) self.updater.start_polling() user = self.ug.get_user() chat = self.cg.get_chat(type="group") update = self.mg.get_message(left_chat_member=user, chat=chat) self.bot.insertUpdate(update) self.assertEqual(0, len(self.bot.sent_messages)) # no message self.updater.stop() def test_chat_member_remove_chat_member_exists(self): self.updater.dispatcher.add_handler( MessageHandler(Filters.status_update.left_chat_member, self.handler.chat_member_remove)) self.updater.start_polling() user = self.ug.get_user() chat = self.cg.get_chat(type="group") update = self.mg.get_message(left_chat_member=user, chat=chat) self.service.add_user_chat_if_not_exists(user.id, chat.id) self.bot.insertUpdate(update) time.sleep(1) # the message takes some time to be sent... self.assertEqual(1, len(self.bot.sent_messages)) # goodbye message sent = self.bot.sent_messages[0] self.assertEqual("sendMessage", sent['method']) self.assertEqual(texts['user-goodbye'](user.first_name), sent['text']) self.updater.stop()
def setUp(self): PtbTestCase.setUp(self) from components.response.response_service import ResponseService self.service = ResponseService() self.service.init(ResponseService.get_absolute_file_name(os.path.join("tests", "messages.txt"))) self.handler = ResponseCommandHandler(-1, {}, TelegramService(UserService()), self.service)
def setUp(self): PtbTestCase.setUp(self) self.handler = CoreCommandHandler(0, {'help': 'test'}, TelegramService(UserService()))
class TestDoForMeCommandHandler(PtbTestCase): def setUp(self): PtbTestCase.setUp(self) self._init_bot() from components.user.models import db db.drop_all_tables(with_all_data=True) db.create_tables() self.user_service = UserService() self.task_service = TaskService() self.admin_id = 42 self.job_log_service = MockJobLogService() show_far_future_tasks = 5 if date.today().weekday() != 5 else 6 self.handler = DoForMeCommandHandler( self.admin_id, texts, TelegramService(self.user_service), "bot-name", self.task_service, self.user_service, FeedbackService(), show_far_future_tasks, self.job_log_service) def _init_bot(self): dfm_bot = DfmMockbot() self.bot.getChat = dfm_bot.getChat self.bot.getChatMember = dfm_bot.getChatMember def test_job_weekly_review_activity_exists_should_sort_user_by_num_tasks_completed( self): self.updater.dispatcher.add_handler( CommandHandler("dummy_cmd", self.handler.job_weekly_review)) self.updater.start_polling() update = self.mg.get_message(text=f"/dummy_cmd") user1_id = update.effective_user.id user2_id = update.effective_user.id + 1 chat_id = update.effective_chat.id self.user_service.add_user_chat_if_not_exists(user2_id, chat_id) self.user_service.add_user_chat_if_not_exists(user1_id, chat_id) task1 = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 1', 'due': datetime.utcnow() } self.task_service.add_task(task1) task = self.task_service.get_tasks(user1_id)[0] self.task_service.complete_task(task.id) self.bot.insertUpdate(update) time.sleep(2) # the message takes some time to be sent... self.assertEqual(1, len(self.bot.sent_messages)) sent = self.bot.sent_messages[0] # the message sent to the user self.assertEqual("sendMessage", sent['method']) lines = sent['text'].split("\n") for line in lines: if f"user {user2_id}:" in line: self.fail("Invalid user sort order.") if f"user {user1_id}:" in line: break self.updater.stop() def test_job_weekly_review_incomplete_tasks_exists_should_show_incomplete_tasks_but_not_done_tasks( self): self.updater.dispatcher.add_handler( CommandHandler("dummy_cmd", self.handler.job_weekly_review)) self.updater.start_polling() update = self.mg.get_message(text=f"/dummy_cmd") user1_id = update.effective_user.id user2_id = update.effective_user.id + 1 chat_id = update.effective_chat.id self.user_service.add_user_chat_if_not_exists(user2_id, chat_id) self.user_service.add_user_chat_if_not_exists(user1_id, chat_id) task1 = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 1', 'due': datetime.utcnow() - timedelta(days=2) } self.task_service.add_task(task1) task2 = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 2', 'due': datetime.utcnow() - timedelta(days=1) } self.task_service.add_task(task2) task = self.task_service.get_tasks(user1_id)[1] self.task_service.complete_task(task.id) self.bot.insertUpdate(update) time.sleep(2) # the message takes some time to be sent... self.assertEqual(1, len(self.bot.sent_messages)) sent = self.bot.sent_messages[0] self.assertEqual("sendMessage", sent['method']) text = sent['text'] self.assertTrue(texts['task-review-incomplete-tasks'] in text) self.assertTrue(texts['task-line-review-incomplete']( 'task 1', f"user {user1_id}", f"user {user2_id}", 2) in text) self.assertTrue(texts['task-line-review-incomplete']( 'task 2', f"user {user1_id}", f"user {user2_id}", 1) not in text) self.updater.stop() def test_job_daily_tasks_show_all_should_only_show_own_tasks_due_within_next_week( self): self.updater.dispatcher.add_handler( CommandHandler("dummy_cmd", self.handler.job_daily_tasks_show_all)) self.updater.start_polling() update = self.mg.get_message(text=f"/dummy_cmd") user1_id = update.effective_user.id user2_id = update.effective_user.id + 1 chat_id = update.effective_chat.id self.user_service.add_user_chat_if_not_exists(user2_id, chat_id) self.user_service.add_user_chat_if_not_exists(user1_id, chat_id) task1_today = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 1', 'due': datetime.utcnow() } self.task_service.add_task(task1_today) task2_this_week = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 2', 'due': datetime.utcnow() + timedelta(days=5) } self.task_service.add_task(task2_this_week) task3_this_month = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 3', 'due': datetime.utcnow() + timedelta(days=8) } self.task_service.add_task(task3_this_month) task4_this_week_other_user = { 'user_id': user2_id, 'chat_id': chat_id, 'owner_id': user1_id, 'title': 'task 4', 'due': datetime.utcnow() + timedelta(days=2) } self.task_service.add_task(task4_this_week_other_user) self.bot.insertUpdate(update) time.sleep(2) # the message takes some time to be sent... chat_name = f"title {update.effective_chat['id']}" expected_user1 = f"Your open tasks:\n\nDue today:\n๏ {chat_name}: {task1_today['title']} from user {user2_id}" \ f"\n\nThis week:\n๏ {chat_name}: {task2_this_week['title']} from user {user2_id}\n\n" expected_user2 = f"Your open tasks:\n\nThis week:\n๏ {chat_name}: {task4_this_week_other_user['title']}" \ f" from user {user1_id}\n\n" self.assertEqual(2, len( self.bot.sent_messages)) # each user gets a summary self.assertEquals(expected_user1, self.bot.sent_messages[1]['text']) self.assertEquals(expected_user2, self.bot.sent_messages[0]['text']) self.updater.stop() def test_job_daily_tasks_show_all_should_show_all_open_own_tasks_once_a_week( self): show_far_future_tasks = date.today().weekday() self.handler = DoForMeCommandHandler( self.admin_id, texts, TelegramService(self.user_service), "bot-name", self.task_service, self.user_service, FeedbackService(), show_far_future_tasks, self.job_log_service) self.updater.dispatcher.add_handler( CommandHandler("dummy_cmd", self.handler.job_daily_tasks_show_all)) self.updater.start_polling() update = self.mg.get_message(text=f"/dummy_cmd") user1_id = update.effective_user.id user2_id = update.effective_user.id + 1 chat_id = update.effective_chat.id self.user_service.add_user_chat_if_not_exists(user2_id, chat_id) self.user_service.add_user_chat_if_not_exists(user1_id, chat_id) task1_today = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 1', 'due': datetime.utcnow() } self.task_service.add_task(task1_today) task2_this_week = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 2', 'due': datetime.utcnow() + timedelta(days=5) } self.task_service.add_task(task2_this_week) task3_this_month = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 3', 'due': datetime.utcnow() + timedelta(days=8) } self.task_service.add_task(task3_this_month) task4_this_week_other_user = { 'user_id': user2_id, 'chat_id': chat_id, 'owner_id': user1_id, 'title': 'task 4', 'due': datetime.utcnow() + timedelta(days=2) } self.task_service.add_task(task4_this_week_other_user) self.bot.insertUpdate(update) time.sleep(2) # the message takes some time to be sent... self.assertEqual(2, len( self.bot.sent_messages)) # each user gets a summary self.assertTrue( texts["summary-due-later"] in self.bot.sent_messages[1]['text']) self.updater.stop() def test_job_daily_tasks_show_all_should_not_send_message_if_no_open_tasks_within_this_week( self): self.updater.dispatcher.add_handler( CommandHandler("dummy_cmd", self.handler.job_daily_tasks_show_all)) self.updater.start_polling() update = self.mg.get_message(text=f"/dummy_cmd") user1_id = update.effective_user.id user2_id = update.effective_user.id + 1 chat_id = update.effective_chat.id self.user_service.add_user_chat_if_not_exists(user2_id, chat_id) self.user_service.add_user_chat_if_not_exists(user1_id, chat_id) task3_this_month = { 'user_id': user1_id, 'chat_id': chat_id, 'owner_id': user2_id, 'title': 'task 3', 'due': datetime.utcnow() + timedelta(days=8) } self.task_service.add_task(task3_this_month) self.bot.insertUpdate(update) time.sleep(2) # the message takes some time to be sent... self.assertEqual(0, len( self.bot.sent_messages)) # each user gets a summary self.updater.stop()
class TestUserService(unittest.TestCase): chat1_id: int user1_id: int db: Database @mock.patch.dict(os.environ, {'DFM_ENV': 'Test'}) def setUp(self): from components.user.models import db from components.user.user_service import UserService self.service = UserService() self.user1_id = 1 self.chat1_id = 2 db.drop_all_tables(with_all_data=True) db.create_tables() self.db = db def tearDown(self): pass def test_add_user_chat_if_not_exists_combination_does_not_exist(self): result = self.service.add_user_chat_if_not_exists(self.user1_id, self.chat1_id) self.assertEqual(True, result) stats = self.service.get_stats() expected = {'num_users': 1, 'num_chats': 1} self.assertEqual(expected, stats) def test_add_user_chat_if_not_exists_combination_exists(self): result = self.service.add_user_chat_if_not_exists(self.user1_id, self.chat1_id) self.assertEqual(True, result) result = self.service.add_user_chat_if_not_exists(self.user1_id, self.chat1_id) self.assertEqual(False, result) stats = self.service.get_stats() expected = {'num_users': 1, 'num_chats': 1} self.assertEqual(expected, stats) def test_remove_user_chat_if_exists_combination_exists(self): self.service.add_user_chat_if_not_exists(self.user1_id, self.chat1_id) result = self.service.remove_user_chat_if_exists(self.user1_id, self.chat1_id) self.assertEqual(True, result) expected = {'num_users': 0, 'num_chats': 0} self.assertEqual(expected, self.service.get_stats()) def test_remove_user_chat_if_exists_combination_does_not_exist(self): result = self.service.remove_user_chat_if_exists(self.user1_id, self.chat1_id) self.assertEqual(False, result) def test_get_chat_users(self): self.service.add_user_chat_if_not_exists(1, 3) self.service.add_user_chat_if_not_exists(2, 3) self.service.add_user_chat_if_not_exists(2, 4) users = self.service.get_chat_users(3) self.assertEqual(2, len(users)) def test_get_chats_of_user(self): self.service.add_user_chat_if_not_exists(1, 3) self.service.add_user_chat_if_not_exists(2, 3) self.service.add_user_chat_if_not_exists(2, 4) self.service.add_user_chat_if_not_exists(3, 6) users = self.service.get_chats_of_user(2) self.assertEqual(2, len(users)) def test_get_all_users(self): self.service.add_user_chat_if_not_exists(1, 3) self.service.add_user_chat_if_not_exists(2, 3) self.service.add_user_chat_if_not_exists(2, 4) users = self.service.get_all_users() self.assertEqual(2, len(users)) def test_get_all_chats(self): self.service.add_user_chat_if_not_exists(1, 3) self.service.add_user_chat_if_not_exists(2, 3) self.service.add_user_chat_if_not_exists(2, 4) self.service.add_user_chat_if_not_exists(3, 6) chats = self.service.get_all_chats() self.assertEqual(3, len(chats)) def test_get_stats_no_users(self): stats = self.service.get_stats() expected = {'num_users': 0, 'num_chats': 0} self.assertEqual(expected, stats) def test_get_stats_users_exist(self): self.service.add_user_chat_if_not_exists(1, 3) self.service.add_user_chat_if_not_exists(1, 2) stats = self.service.get_stats() expected = {'num_users': 1, 'num_chats': 2} self.assertEqual(expected, stats)