File tree Expand file tree Collapse file tree 4 files changed +66
-0
lines changed Expand file tree Collapse file tree 4 files changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ from .float import FloatAttribute
1
2
from .integer import IntegerAttribute
2
3
from .integer_date import IntegerDateAttribute
3
4
from .integer_enum import IntegerEnumAttribute
8
9
from .unicode_enum import UnicodeEnumAttribute
9
10
10
11
__all__ = [
12
+ 'FloatAttribute' ,
11
13
'IntegerAttribute' ,
12
14
'IntegerDateAttribute' ,
13
15
'IntegerEnumAttribute' ,
Original file line number Diff line number Diff line change
1
+ from pynamodb .attributes import Attribute
2
+ from pynamodb .attributes import NumberAttribute
3
+
4
+
5
+ class FloatAttribute (Attribute ):
6
+ """
7
+ Unlike NumberAttribute, this attribute has its type hinted as 'float'.
8
+ """
9
+ attr_type = NumberAttribute .attr_type
10
+ serialize = NumberAttribute .serialize
11
+ deserialize = NumberAttribute .deserialize
Original file line number Diff line number Diff line change
1
+ from ._typing import Attribute
2
+
3
+
4
+ class FloatAttribute (Attribute [float ]):
5
+ ...
Original file line number Diff line number Diff line change
1
+ import pytest
2
+ from pynamodb .attributes import UnicodeAttribute
3
+ from pynamodb .models import Model
4
+
5
+ from pynamodb_attributes import FloatAttribute
6
+ from tests .meta import dynamodb_table_meta
7
+
8
+
9
+ class MyModel (Model ):
10
+ Meta = dynamodb_table_meta (__name__ )
11
+
12
+ key = UnicodeAttribute (hash_key = True )
13
+ value = FloatAttribute (null = True )
14
+
15
+
16
+ @pytest .fixture (scope = 'module' , autouse = True )
17
+ def create_table ():
18
+ MyModel .create_table ()
19
+
20
+
21
+ def test_serialization_non_null (uuid_key ):
22
+ model = MyModel ()
23
+ model .key = uuid_key
24
+ model .value = 45.6
25
+ model .save ()
26
+
27
+ # verify underlying storage
28
+ item = MyModel ._get_connection ().get_item (uuid_key )
29
+ assert item ['Item' ]['value' ] == {'N' : '45.6' }
30
+
31
+ # verify deserialization
32
+ model = MyModel .get (uuid_key )
33
+ assert model .value == 45.6
34
+
35
+
36
+ def test_serialization_null (uuid_key ):
37
+ model = MyModel ()
38
+ model .key = uuid_key
39
+ model .value = None
40
+ model .save ()
41
+
42
+ # verify underlying storage
43
+ item = MyModel ._get_connection ().get_item (uuid_key )
44
+ assert 'value' not in item ['Item' ]
45
+
46
+ # verify deserialization
47
+ model = MyModel .get (uuid_key )
48
+ assert model .value is None
You can’t perform that action at this time.
0 commit comments