-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from xoeye/feature/better-item-exceptions
- Loading branch information
Showing
14 changed files
with
208 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pytest | ||
|
||
from xoto3.dynamodb.exceptions import ( | ||
get_item_exception_type, | ||
raise_if_empty_getitem_response, | ||
ItemNotFoundException, | ||
ItemAlreadyExistsException, | ||
AlreadyExistsException, | ||
) | ||
|
||
|
||
def test_dynamically_named_exceptions_names_and_caches_different_types(): | ||
media_not_found = get_item_exception_type("Media", ItemNotFoundException) | ||
|
||
assert media_not_found.__name__ == "MediaNotFoundException" | ||
assert issubclass(media_not_found, ItemNotFoundException) | ||
|
||
assert get_item_exception_type("Media", ItemNotFoundException) is media_not_found | ||
|
||
media_already_exists = get_item_exception_type("Media", ItemAlreadyExistsException) | ||
assert issubclass(media_already_exists, AlreadyExistsException) | ||
assert media_already_exists.__name__ == "MediaAlreadyExistsException" | ||
assert not issubclass(media_already_exists, ItemNotFoundException) | ||
|
||
|
||
def test_raises_uses_nicename(): | ||
with pytest.raises(ItemNotFoundException) as infe_info: | ||
raise_if_empty_getitem_response(dict(), nicename="Duck") | ||
assert infe_info.value.__class__.__name__ == "DuckNotFoundException" | ||
|
||
|
||
def test_raises_includes_key_and_table_name(): | ||
with pytest.raises(ItemNotFoundException) as infe_info: | ||
raise_if_empty_getitem_response( | ||
dict(), nicename="Plant", table_name="Greenhouse", key=dict(id="p0001") | ||
) | ||
assert infe_info.value.key == dict(id="p0001") | ||
assert infe_info.value.table_name == "Greenhouse" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
import random | ||
|
||
import pytest | ||
import boto3 | ||
|
||
import xoto3.dynamodb.put as xput | ||
|
||
from tests.xoto3.dynamodb.testing_utils import make_pytest_put_fixture_for_table | ||
|
||
|
||
XOTO3_INTEGRATION_TEST_ID_TABLE_NAME = os.environ.get( | ||
"XOTO3_INTEGRATION_TEST_DYNAMODB_ID_TABLE_NAME" | ||
) | ||
|
||
|
||
_INTEGRATION_ID_TABLE = ( | ||
boto3.resource("dynamodb").Table(XOTO3_INTEGRATION_TEST_ID_TABLE_NAME) | ||
if XOTO3_INTEGRATION_TEST_ID_TABLE_NAME | ||
else None | ||
) | ||
integration_table_put = make_pytest_put_fixture_for_table(_INTEGRATION_ID_TABLE) | ||
|
||
|
||
@pytest.mark.skipif( | ||
not XOTO3_INTEGRATION_TEST_ID_TABLE_NAME, reason="No integration id table was defined" | ||
) | ||
def test_put_already_exists(integration_table_put): | ||
|
||
random_key = dict(id=str(random.randint(9999999, 999999999999999999999))) | ||
item = dict(random_key, test_item_please_ignore=True) | ||
integration_table_put(item) | ||
|
||
with pytest.raises(xput.ItemAlreadyExistsException) as ae_info: | ||
xput.put_but_raise_if_exists( | ||
_INTEGRATION_ID_TABLE, dict(item, new_attribute="testing attr"), nicename="TestThing" | ||
) | ||
|
||
assert ae_info.value.__class__.__name__ == "TestThingAlreadyExistsException" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pytest | ||
|
||
from xoto3.dynamodb.put import PutItem | ||
from xoto3.dynamodb.types import TableResource, InputItem | ||
from xoto3.dynamodb.utils.table import extract_key_from_item | ||
|
||
|
||
def make_pytest_put_fixture_for_table(table: TableResource): | ||
@pytest.fixture | ||
def put_item_fixture(): | ||
keys_put = list() | ||
|
||
def _put_item(item: InputItem): | ||
keys_put.append(extract_key_from_item(table, item)) | ||
PutItem(table, item) | ||
|
||
yield _put_item | ||
|
||
for key in keys_put: | ||
table.delete_item(Key=key) | ||
|
||
return put_item_fixture |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""xoto3""" | ||
__version__ = "1.3.3" | ||
__version__ = "1.4.0" | ||
__author__ = "Peter Gaultney" | ||
__author_email__ = "[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.