Skip to content

Commit

Permalink
Add unit test example to README.md (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayongbannayan authored Jan 2, 2024
1 parent 4f25aaf commit 9ddf6b0
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,35 @@ from boto3 import Session
from skymantle_boto_buddy import EnableCache, s3

s3_client = get_s3_client(enable_cache=EnableCache.NO)
```

- unit test a function with patching (also possible to use packages like [moto](https://github.com/getmoto/moto))

```python
# my_file.py
from skymantle_boto_buddy import dynamodb

def some_function():
# ...
item = dynamodb.get_item("table_name", {"PK": "some_key"})

return item


# my_test.py
from unittest.mock import MagicMock
import pytest
from pytest_mock import MockerFixture
import my_file

@pytest.fixture()
def mock_dynamodb(mocker: MockerFixture) -> MagicMock:
mock = mocker.patch("my_file.dynamodb")
return mock

def test_some_function(mock_dynamodb):
mock_dynamodb.get_item.return_value = {"PK": "some_pk", "Name": "some value"}

result = my_file.some_function()
assert result == {"PK": "some_pk", "Name": "some value"}
```

0 comments on commit 9ddf6b0

Please sign in to comment.