-
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.
adding an IntegerSetAttribute
- Loading branch information
Showing
7 changed files
with
79 additions
and
4 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
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,15 @@ | ||
from typing import Set | ||
|
||
from pynamodb.attributes import Attribute | ||
from pynamodb.attributes import NumberSetAttribute | ||
|
||
|
||
class IntegerSetAttribute(Attribute[Set[int]]): | ||
""" | ||
Unlike NumberSetAttribute, this attribute has its type hinted as 'Set[int]'. | ||
""" | ||
|
||
attr_type = NumberSetAttribute.attr_type | ||
null = NumberSetAttribute.null | ||
serialize = NumberSetAttribute.serialize # type: ignore | ||
deserialize = NumberSetAttribute.deserialize # type: ignore |
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,56 @@ | ||
from typing import Set | ||
|
||
import pytest | ||
from pynamodb.attributes import UnicodeAttribute | ||
from pynamodb.models import Model | ||
from typing_extensions import assert_type | ||
|
||
from pynamodb_attributes import IntegerSetAttribute | ||
from tests.connection import _connection | ||
from tests.meta import dynamodb_table_meta | ||
|
||
|
||
class MyModel(Model): | ||
Meta = dynamodb_table_meta(__name__) | ||
|
||
key = UnicodeAttribute(hash_key=True) | ||
value = IntegerSetAttribute(null=True) | ||
|
||
|
||
assert_type(MyModel.value, IntegerSetAttribute) | ||
assert_type(MyModel().value, Set[int]) | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def create_table(): | ||
MyModel.create_table() | ||
|
||
|
||
def test_serialization_non_null(uuid_key): | ||
model = MyModel() | ||
model.key = uuid_key | ||
model.value = {456, 789} | ||
model.save() | ||
|
||
# verify underlying storage | ||
item = _connection(MyModel).get_item(uuid_key) | ||
assert item["Item"]["value"] == {"NS": ["456", "789"]} | ||
|
||
# verify deserialization | ||
model = MyModel.get(uuid_key) | ||
assert model.value == {456, 789} | ||
|
||
|
||
def test_serialization_null(uuid_key): | ||
model = MyModel() | ||
model.key = uuid_key | ||
model.value = None | ||
model.save() | ||
|
||
# verify underlying storage | ||
item = _connection(MyModel).get_item(uuid_key) | ||
assert "value" not in item["Item"] | ||
|
||
# verify deserialization | ||
model = MyModel.get(uuid_key) | ||
assert model.value is None |
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