Skip to content

Commit 6cf7ef1

Browse files
surajitsurajit
authored andcommitted
add test for log dynamo db
1 parent 074db0f commit 6cf7ef1

File tree

6 files changed

+81
-9
lines changed

6 files changed

+81
-9
lines changed

aws/services/dynamo_db/logs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ async def create_log(data):
2525
"request_ip": data["request_ip"],
2626
"message": json.dumps(data["message"]),
2727
}
28-
return table.put_item(Item=trans)
28+
return table.put_item(trans)
2929
except botocore.exceptions.ClientError as e:
3030
raise BotoClientException(e)

logger/log.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ async def wrapper(*args, **kwargs):
5959
request_ip=client,
6060
message=message
6161
)
62-
await create_log(data)
62+
res = await create_log(data)
63+
print('res',res)
6364
except Exception as exc:
6465
logger.exception(f"Exception occurred {exc}")
6566
pass

tests/data/dynamo_db_response.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"ResponseMetadata": {
3+
"RequestId": "PFK7OPBR2L9FLV3N9OFCHO3VJNVV4KQNSO5A",
4+
"HTTPStatusCode": 200,
5+
"HTTPHeaders": {
6+
"server": "Server",
7+
"date": "Sat, 28 May 2022 18:32:28 GMT",
8+
"content-type": "application/x-amz-json-1.0",
9+
"content-length": "2",
10+
"connection": "keep-alive",
11+
"x-amzn-requestid": "PFK7OPBR2L9FLV3N9OFCHO3V",
12+
"x-amz-crc32": "274561"
13+
},
14+
"RetryAttempts": 0
15+
}
16+
}

tests/integrations/common.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import json
2+
3+
from settings import TEST_DATA_DIR
4+
5+
6+
def read_json(filename):
7+
with open(TEST_DATA_DIR.joinpath(filename)) as resp_file:
8+
return json.load(resp_file)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pytest
2+
from unittest import mock
3+
4+
from aws.services.dynamo_db.logs import create_log, BotoClientException
5+
from tests.integrations.common import read_json
6+
7+
8+
class MockDynamoDbTable:
9+
def put_item(self, item):
10+
response = read_json("dynamo_db_response.json")
11+
return response
12+
13+
14+
@mock.patch("aws.services.dynamo_db.logs.dynamodb.Table")
15+
@mock.patch("aws.services.dynamo_db.logs.boto3.resource")
16+
@pytest.mark.asyncio
17+
async def test_dynamo_db_log(mock_boto3_connect, mock_dynamo_db_table):
18+
mock_boto3_connect.return_value = "success"
19+
mock_dynamo_db_table.return_value = MockDynamoDbTable()
20+
data = {
21+
"log_id": 12345,
22+
"request_ip": "localhost",
23+
"message": {
24+
"url": "http://127.0.0.1:8000/api/v1/companies/",
25+
"method": "POST",
26+
"data": [{"org_id": 1234, "name": "New test"}],
27+
},
28+
}
29+
response = await create_log(data)
30+
assert response == read_json("dynamo_db_response.json")
31+
32+
33+
@mock.patch("aws.services.dynamo_db.logs.dynamodb.Table")
34+
@mock.patch("aws.services.dynamo_db.logs.boto3.resource")
35+
@pytest.mark.asyncio
36+
async def test_dynamo_db_log_raises_client_error(
37+
mock_boto3_connect, mock_dynamo_db_table
38+
):
39+
mock_boto3_connect.return_value = "success"
40+
mock_dynamo_db_table.side_effect = BotoClientException("Something went wrong")
41+
data = {
42+
"log_id": 12345,
43+
"request_ip": "localhost",
44+
"message": {
45+
"url": "http://127.0.0.1:8000/api/v1/companies/",
46+
"method": "POST",
47+
"data": [{"org_id": 1234, "name": "New test"}],
48+
},
49+
}
50+
with pytest.raises(BotoClientException) as exc:
51+
await create_log(data)
52+
assert isinstance(exc.value, BotoClientException)
53+
assert exc.value.args[0] == "Something went wrong"

tests/integrations/test_sendgrid.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import json
21
import pytest
32
from unittest import mock
43

54
from email_api.email import send_email
65
from email_api.email import UnauthorizedException
76
from email_api.email import BadRequestException
8-
from settings import TEST_DATA_DIR
9-
10-
11-
def read_json(filename):
12-
with open(TEST_DATA_DIR.joinpath(filename)) as resp_file:
13-
return json.load(resp_file)
7+
from tests.integrations.common import read_json
148

159

1610
@mock.patch("email_api.email.SendGridAPIClient.send")

0 commit comments

Comments
 (0)