Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.

refactor: simplify logging #72

Merged
merged 3 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions int/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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())
Expand Down
32 changes: 22 additions & 10 deletions int/tests/test_basicmessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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",
),
Expand 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


Expand Down Expand Up @@ -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(
Expand All @@ -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",
),
Expand 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"
Expand Down
49 changes: 28 additions & 21 deletions int/tests/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,13 +35,11 @@ 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
print("get_list before connection: ", get_list_before_connection["connections"])
assert True # False


@pytest.mark.asyncio
Expand All @@ -68,7 +63,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
Expand All @@ -79,8 +77,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
Expand All @@ -103,8 +101,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",
Expand All @@ -125,10 +123,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
Expand Down Expand Up @@ -185,24 +186,30 @@ 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(
{
"@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"
)