|
| 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" |
0 commit comments