|
| 1 | +from typing import Set |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pynamodb.attributes import UnicodeAttribute |
| 5 | +from pynamodb.models import Model |
| 6 | +from typing_extensions import assert_type |
| 7 | + |
| 8 | +from pynamodb_attributes import IntegerSetAttribute |
| 9 | +from tests.connection import _connection |
| 10 | +from tests.meta import dynamodb_table_meta |
| 11 | + |
| 12 | + |
| 13 | +class MyModel(Model): |
| 14 | + Meta = dynamodb_table_meta(__name__) |
| 15 | + |
| 16 | + key = UnicodeAttribute(hash_key=True) |
| 17 | + value = IntegerSetAttribute(null=True) |
| 18 | + |
| 19 | + |
| 20 | +assert_type(MyModel.value, IntegerSetAttribute) |
| 21 | +assert_type(MyModel().value, Set[int]) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="module", autouse=True) |
| 25 | +def create_table(): |
| 26 | + MyModel.create_table() |
| 27 | + |
| 28 | + |
| 29 | +def test_serialization_non_null(uuid_key): |
| 30 | + model = MyModel() |
| 31 | + model.key = uuid_key |
| 32 | + model.value = {456, 789} |
| 33 | + model.save() |
| 34 | + |
| 35 | + # verify underlying storage |
| 36 | + item = _connection(MyModel).get_item(uuid_key) |
| 37 | + assert item["Item"]["value"] == {"NS": ["456", "789"]} |
| 38 | + |
| 39 | + # verify deserialization |
| 40 | + model = MyModel.get(uuid_key) |
| 41 | + assert model.value == {456, 789} |
| 42 | + |
| 43 | + |
| 44 | +def test_serialization_null(uuid_key): |
| 45 | + model = MyModel() |
| 46 | + model.key = uuid_key |
| 47 | + model.value = None |
| 48 | + model.save() |
| 49 | + |
| 50 | + # verify underlying storage |
| 51 | + item = _connection(MyModel).get_item(uuid_key) |
| 52 | + assert "value" not in item["Item"] |
| 53 | + |
| 54 | + # verify deserialization |
| 55 | + model = MyModel.get(uuid_key) |
| 56 | + assert model.value is None |
0 commit comments