-
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.
- Loading branch information
Showing
4 changed files
with
66 additions
and
0 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,11 @@ | ||
from pynamodb.attributes import Attribute | ||
from pynamodb.attributes import NumberAttribute | ||
|
||
|
||
class FloatAttribute(Attribute): | ||
""" | ||
Unlike NumberAttribute, this attribute has its type hinted as 'float'. | ||
""" | ||
attr_type = NumberAttribute.attr_type | ||
serialize = NumberAttribute.serialize | ||
deserialize = NumberAttribute.deserialize |
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,5 @@ | ||
from ._typing import Attribute | ||
|
||
|
||
class FloatAttribute(Attribute[float]): | ||
... |
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,48 @@ | ||
import pytest | ||
from pynamodb.attributes import UnicodeAttribute | ||
from pynamodb.models import Model | ||
|
||
from pynamodb_attributes import FloatAttribute | ||
from tests.meta import dynamodb_table_meta | ||
|
||
|
||
class MyModel(Model): | ||
Meta = dynamodb_table_meta(__name__) | ||
|
||
key = UnicodeAttribute(hash_key=True) | ||
value = FloatAttribute(null=True) | ||
|
||
|
||
@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 = 45.6 | ||
model.save() | ||
|
||
# verify underlying storage | ||
item = MyModel._get_connection().get_item(uuid_key) | ||
assert item['Item']['value'] == {'N': '45.6'} | ||
|
||
# verify deserialization | ||
model = MyModel.get(uuid_key) | ||
assert model.value == 45.6 | ||
|
||
|
||
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 |