Skip to content

Commit

Permalink
adding float attributes (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
hallie authored and Ilya Konstantinov committed Jul 8, 2019
1 parent b9a1fd9 commit 12b1014
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pynamodb_attributes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .float import FloatAttribute
from .integer import IntegerAttribute
from .integer_date import IntegerDateAttribute
from .integer_enum import IntegerEnumAttribute
Expand All @@ -8,6 +9,7 @@
from .unicode_enum import UnicodeEnumAttribute

__all__ = [
'FloatAttribute',
'IntegerAttribute',
'IntegerDateAttribute',
'IntegerEnumAttribute',
Expand Down
11 changes: 11 additions & 0 deletions pynamodb_attributes/float.py
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
5 changes: 5 additions & 0 deletions pynamodb_attributes/float.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from ._typing import Attribute


class FloatAttribute(Attribute[float]):
...
48 changes: 48 additions & 0 deletions tests/float_attribute_test.py
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

0 comments on commit 12b1014

Please sign in to comment.