-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds UUIDAttribute to pynamodb_attributes * Custom attribute is backed by unicode 'S' type in DynamoDB type * Adds docs and bumps number * Adds tests
- Loading branch information
Showing
8 changed files
with
133 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ build/ | |
.coverage | ||
.mypy_cache | ||
.pytest_cache | ||
.dmypy.json | ||
coverage.xml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Any | ||
from typing import TYPE_CHECKING | ||
from uuid import UUID | ||
|
||
import pynamodb.constants | ||
from pynamodb.attributes import Attribute | ||
|
||
|
||
class UUIDAttribute(Attribute): | ||
""" | ||
PynamoDB attribute to for UUIDs. These are backed by DynamoDB unicode (`S`) types. | ||
""" | ||
|
||
attr_type = pynamodb.constants.STRING | ||
|
||
def __init__(self, remove_dashes: bool = False, **kwargs: Any) -> None: | ||
""" | ||
Initializes a UUIDAttribute object. | ||
:param remove_dashes: if set, the string serialization will be without dashes. | ||
Defaults to False. | ||
""" | ||
super().__init__(**kwargs) | ||
self._remove_dashes = remove_dashes | ||
|
||
def serialize(self, value: UUID) -> str: | ||
result = str(value) | ||
|
||
if self._remove_dashes: | ||
result = result.replace('-', '') | ||
|
||
return result | ||
|
||
def deserialize(self, value: str) -> UUID: | ||
return UUID(value) | ||
|
||
if TYPE_CHECKING: | ||
def __get__(self, instance: Any, owner: Any) -> UUID: | ||
... |
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,6 @@ | ||
from uuid import UUID | ||
|
||
from ._typing import Attribute | ||
|
||
class UUIDAttribute(Attribute[UUID]): | ||
... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from uuid import UUID | ||
|
||
import pytest | ||
from pynamodb.attributes import UnicodeAttribute | ||
from pynamodb.models import Model | ||
|
||
from pynamodb_attributes import UUIDAttribute | ||
from tests.meta import dynamodb_table_meta | ||
|
||
|
||
class MyModel(Model): | ||
Meta = dynamodb_table_meta(__name__) | ||
|
||
key = UnicodeAttribute(hash_key=True) | ||
value = UUIDAttribute(null=True) | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def create_table(): | ||
MyModel.create_table() | ||
|
||
|
||
def test_deserialization_no_dashes(): | ||
uuid_attribute = UUIDAttribute(remove_dashes=True) | ||
uuid_str_no_dashes = "19c4f2515e364cc0bfeb983dd5d2bacd" | ||
|
||
assert UUID("19c4f251-5e36-4cc0-bfeb-983dd5d2bacd") == uuid_attribute.deserialize( | ||
uuid_str_no_dashes, | ||
) | ||
|
||
|
||
def test_serialization_no_dashes(): | ||
uuid_attribute = UUIDAttribute(remove_dashes=True) | ||
uuid_value = UUID("19c4f251-5e36-4cc0-bfeb-983dd5d2bacd") | ||
|
||
assert "19c4f2515e364cc0bfeb983dd5d2bacd" == uuid_attribute.serialize(uuid_value) | ||
|
||
|
||
def test_serialization_non_null(uuid_key): | ||
model = MyModel() | ||
model.key = uuid_key | ||
uuid_str = "19c4f251-5e36-4cc0-bfeb-983dd5d2bacd" | ||
uuid_value = UUID(uuid_str) | ||
model.value = uuid_value | ||
model.save() | ||
|
||
# verify underlying storage | ||
item = MyModel._get_connection().get_item(uuid_key) | ||
assert item["Item"]["value"] == {"S": uuid_str} | ||
|
||
# verify deserialization | ||
model = MyModel.get(uuid_key) | ||
assert model.value == uuid_value | ||
|
||
|
||
def test_serialization_null(uuid_key): | ||
model = MyModel() | ||
model.key = uuid_key | ||
model.value = None | ||
model.save() | ||
|
||
# verify underlying storage | ||
item = MyModel._get_connection().get_item(uuid_key) | ||
assert "value" not in item["Item"] | ||
|
||
# verify deserialization | ||
model = MyModel.get(uuid_key) | ||
assert model.value is None |