def test_client_connection(self): connection_spy = unittest.mock.MagicMock() client = ChatClient("User 1", connection_provider=lambda *args: connection_spy) client.send_message("Hello World") connection_spy.broadcast.assert_called_with(("User 1: Hello World"))
def test_client_fetch_messages(self): connection = unittest.mock.Mock() connection.get_messages.return_value = ["message1", "message2"] client = ChatClient("User 1", connection_provider=lambda *args: connection) starting_messages = client.fetch_messages() client.connection.get_messages().append("message3") new_messages = client.fetch_messages() assert starting_messages == ["message1", "message2"] assert new_messages == ["message3"]
def test_exchange_with_server(self): c1 = ChatClient("User1") c2 = ChatClient("User2") c1.send_message("connected message") assert c2.fetch_messages()[-1] == "User1: connected message"
def test_many_users(self): firstUser = ChatClient("John Doe") for uid in range(5): moreuser = ChatClient(f"User {uid}") moreuser.send_message("Hello!") messages = firstUser.fetch_messages() assert len(messages) == 5
def test_message_exchange(self): with new_chat_server() as srv: user1 = ChatClient("John Doe") user2 = ChatClient("Harry Potter") user1.send_message("Hello World") messages = user2.fetch_messages() assert messages == ["John Doe: Hello World"]
def run_command(cls, token: str, source_id: str, profile_id: str, text: str) -> None: command = text.lower() reply_texts = None if command.startswith('main'): if command == 'main 24': SessionClient.start_game(source_id, 'dua_empat') reply_texts = DuaEmpatClient.start(source_id) elif command == 'udahan': game = SessionClient.get_game(source_id) if game == 'dua_empat': reply_texts = DuaEmpatClient.end(source_id) SessionClient.end_game(source_id) else: game = SessionClient.get_game(source_id) if game == 'dua_empat': reply_texts = DuaEmpatClient.reply(source_id, profile_id, text) else: cls._test_postback(token) ChatClient.send_text_reply(token, reply_texts)
def main(): parser = create_parser() args = parser.parse_args() host = args.host port = args.port history_file_path = args.history timeout = int(args.timeout) logger = get_logger('general_logger', logging.DEBUG) if not os.path.isfile(history_file_path): sys.stdout.write(f'File is not exists or cannot be open: {history_file_path}') sys.exit(1) chat_client = ChatClient(host, port, timeout, history_file_path, logger) try: if args.command == 'listen': asyncio.run(run_listen_chat(chat_client)) elif args.command == 'register': async_function = register_scenario asyncio.run( process_scenario(chat_client, async_function, username=args.username) ) elif args.command == 'send': async_function = send_message_scenario asyncio.run( process_scenario( chat_client, async_function, token=args.token, message=args.message) ) else: sys.stdout.write('Wrong command.') sys.exit(1) except KeyboardInterrupt as e: sys.stdout.write('gently closing\n')
def test_multiple_readers(self): user1 = ChatClient("John Doe") user2 = ChatClient("User 2") user3 = ChatClient("User 3") user1.send_message("Hi all") user2.send_message("Hello World") user3.send_message("Hi") user1_messages = user1.fetch_messages() user2_messages = user2.fetch_messages() self.assertEqual(user1_messages, user2_messages)
def test_smoke_sending_message(self): with new_chat_server() as srv: user1 = ChatClient("User1") user1.send_message("Hello World")
def test_nickname(self): client = ChatClient("User 1") assert client.nickname == "User 1"
def test_send_message(self): client = ChatClient("User 1", connection_provider=unittest.mock.Mock()) sent_message = client.send_message("Hello World") assert sent_message == "User 1: Hello World"
def test_sending_messages(self): with new_chat_server() as srv: user1 = ChatClient("User1") self.bench(lambda: user1.send_message("Hello World"), number=10)
async def setUp(self): self.host = 'host' self.port = 5000 self.chat_reader_client = ChatClient(self.host, self.port, None, None, logging.getLogger(__file__))
def main(): args = _parse_args() chat = ChatClient() chat.run()