From 618c46b5c31bf4437914c7b611c15a8b94362531 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Fri, 25 Jun 2021 20:46:05 -0500 Subject: [PATCH 1/3] refactor: simplify logging Signed-off-by: Daniel Bluhm --- int/tests/__init__.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/int/tests/__init__.py b/int/tests/__init__.py index 18d0b56e..20430124 100644 --- a/int/tests/__init__.py +++ b/int/tests/__init__.py @@ -1,8 +1,12 @@ -# 1. Copy BaseAgent implementation from agent-testing into int/tests/__init__.py +"""Common helpers.""" + +import logging from aiohttp import web from aries_staticagent import StaticConnection, Module -import logging + +LOGGER = logging.getLogger(__name__) + class BaseAgent: """Simple Agent class. @@ -20,17 +24,10 @@ async def handle_web_request(self, request: web.Request): """Handle HTTP POST.""" response = [] with self.connection.session(response.append) as session: - await self.connection.handle(await request.read(), session) - - # create logger - logger = logging.getLogger(__name__) - logger.setLevel(logging.DEBUG) - console_handler = logging.StreamHandler() - console_handler.setLevel(logging.DEBUG) - formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') - console_handler.setFormatter(formatter) - logger.addHandler(console_handler) - logger.warning('No suitable message handler for this type') + try: + await self.connection.handle(await request.read(), session) + except: + LOGGER.exception("Message handling failed") if response: return web.Response(body=response.pop()) From 819c12ef7cc6c00234a21609449e602b0160a39c Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Fri, 25 Jun 2021 20:58:32 -0500 Subject: [PATCH 2/3] style: reformat with black Signed-off-by: Daniel Bluhm --- int/tests/test_basicmessage.py | 32 ++++++++++++++++------- int/tests/test_connections.py | 48 +++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/int/tests/test_basicmessage.py b/int/tests/test_basicmessage.py index 06a55322..6d31621c 100644 --- a/int/tests/test_basicmessage.py +++ b/int/tests/test_basicmessage.py @@ -20,7 +20,10 @@ async def test_send(connection: StaticConnection, connection_id: str): timeout=60, ) recip_message = await asyncio.wait_for(future_recip_message, 60) - assert recip_message["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/sent" + assert ( + recip_message["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/sent" + ) assert recip_message["message"]["content"] == "Your hovercraft is full of eels." # Delete messages to clear the state between tests await connection.send_and_await_reply_async( @@ -39,7 +42,7 @@ async def test_delete(connection: StaticConnection, connection_id: str): { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/send", "connection_id": connection_id, - "content": "Test Message #{}".format(i), + "content": "Test Message #{}".format(i), }, return_route="all", ), @@ -53,10 +56,13 @@ async def test_delete(connection: StaticConnection, connection_id: str): ) get_messages = await connection.send_and_await_reply_async( { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/get", + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/get", } ) - assert delete_message["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/deleted" + assert ( + delete_message["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/deleted" + ) assert get_messages["count"] == 0 @@ -90,10 +96,13 @@ async def test_get(connection: StaticConnection, connection_id: str): recip_message = await asyncio.wait_for(future_recip_message, 60) get_messages = await connection.send_and_await_reply_async( { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/get", + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/get", } ) - assert get_messages["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/messages" + assert ( + get_messages["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/messages" + ) assert get_messages["count"] == 2 # Delete messages to clear the state between tests await connection.send_and_await_reply_async( @@ -112,7 +121,7 @@ async def test_get_limit_offset(connection: StaticConnection, connection_id: str { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/send", "connection_id": connection_id, - "content": "Test Message #{}".format(i), + "content": "Test Message #{}".format(i), }, return_route="all", ), @@ -121,12 +130,15 @@ async def test_get_limit_offset(connection: StaticConnection, connection_id: str recip_message = await asyncio.wait_for(future_recip_message, 60) get_messages = await connection.send_and_await_reply_async( { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/get", + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/get", "limit": 3, - "offset": 2 + "offset": 2, } ) - assert get_messages["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/messages" + assert ( + get_messages["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-basicmessage/0.1/messages" + ) assert get_messages["count"] == 3 assert get_messages["messages"][0]["content"] == "Test Message #3" assert get_messages["messages"][1]["content"] == "Test Message #2" diff --git a/int/tests/test_connections.py b/int/tests/test_connections.py index b7f660e5..376e5d29 100644 --- a/int/tests/test_connections.py +++ b/int/tests/test_connections.py @@ -38,13 +38,13 @@ async def clear_connection_state(backchannel: Client): async def test_get_list_before_connection(connection): get_list_before_connection = await connection.send_and_await_reply_async( { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" } ) - logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO) - logging.warning('Log of test_get_list_before_connection') - print("get_list before connection: ",get_list_before_connection["connections"]) - assert True#False + logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO) + logging.warning("Log of test_get_list_before_connection") + print("get_list before connection: ", get_list_before_connection["connections"]) + assert True # False @pytest.mark.asyncio @@ -68,7 +68,10 @@ async def test_create_connection(connection): "auto_accept": True, } ) - assert received["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/connection" + assert ( + received["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/connection" + ) # Temporary Test: after connection @@ -79,8 +82,8 @@ async def test_get_list_after_connection(connection): "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" } ) - print("get_list after connection: ",get_list_after_connection["connections"]) - assert True#False + print("get_list after connection: ", get_list_after_connection["connections"]) + assert True # False @pytest.mark.asyncio @@ -103,8 +106,8 @@ async def test_get_list(connection): "auto_accept": True, } ) - print("Invitation: ",invitation) - print("Received: ",received) + print("Invitation: ", invitation) + print("Received: ", received) invitation2 = await connection.send_and_await_reply_async( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-invitations/0.1/create", @@ -125,10 +128,13 @@ async def test_get_list(connection): ) get_list = await connection.send_and_await_reply_async( { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" } ) - assert get_list["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/list" + assert ( + get_list["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/list" + ) @pytest.mark.asyncio @@ -185,15 +191,18 @@ async def test_delete(connection): ) get_list_beforedelete = await connection.send_and_await_reply_async( { - "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" + "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" } ) - print('Connections before delete: ',get_list_beforedelete["connections"]) - assert received["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/connection" + print("Connections before delete: ", get_list_beforedelete["connections"]) + assert ( + received["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/connection" + ) delete_connection = await connection.send_and_await_reply_async( { "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/delete", - "connection_id": received["connection_id"] + "connection_id": received["connection_id"], } ) get_list_afterdelete = await connection.send_and_await_reply_async( @@ -201,8 +210,11 @@ async def test_delete(connection): "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" } ) - print("List after delete",get_list_afterdelete["connections"]) + print("List after delete", get_list_afterdelete["connections"]) # for i in get_list_beforedelete["connections"]: # if i not in get_list_afterdelete["connections"]: # print(i) - assert delete_connection["@type"] == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/deleted" + assert ( + delete_connection["@type"] + == "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/deleted" + ) From f703b3b016cf32091bcd7dc769d9b305b79b1352 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Fri, 25 Jun 2021 20:59:55 -0500 Subject: [PATCH 3/3] refactor: drop logging in test_connections for now Signed-off-by: Daniel Bluhm --- int/tests/test_connections.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/int/tests/test_connections.py b/int/tests/test_connections.py index 376e5d29..32a8d35a 100644 --- a/int/tests/test_connections.py +++ b/int/tests/test_connections.py @@ -4,9 +4,6 @@ from acapy_backchannel import Client from acapy_backchannel.api.connection import delete_connection, get_connections import time -import logging - -logging.basicConfig(level=logging.INFO) @pytest.mark.asyncio @@ -41,8 +38,6 @@ async def test_get_list_before_connection(connection): "@type": "https://github.com/hyperledger/aries-toolbox/tree/master/docs/admin-connections/0.1/get-list" } ) - logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO) - logging.warning("Log of test_get_list_before_connection") print("get_list before connection: ", get_list_before_connection["connections"]) assert True # False