-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoder_test.py
222 lines (162 loc) · 5.92 KB
/
encoder_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import json
from datetime import datetime, timezone
import pytest
from pynamodb.attributes import (
BinaryAttribute,
BinarySetAttribute,
BooleanAttribute,
DiscriminatorAttribute,
DynamicMapAttribute,
JSONAttribute,
ListAttribute,
MapAttribute,
NumberAttribute,
NumberSetAttribute,
TTLAttribute,
UnicodeAttribute,
UnicodeSetAttribute,
UTCDateTimeAttribute,
VersionAttribute,
)
from pynamodb.models import Model
from pynamodb_encoder.encoder import Encoder
@pytest.fixture(scope="module", autouse=True)
def encoder() -> Encoder:
return Encoder()
def test_encode_simple_model(encoder):
class Pet(Model):
name = UnicodeAttribute()
age = NumberAttribute()
pet = Pet(name="Garfield", age=43)
assert encoder.encode(pet) == {"name": "Garfield", "age": 43}
def test_encode_skip_none_attribute(encoder):
class Pet(Model):
name = UnicodeAttribute()
age = NumberAttribute()
pet = Pet(name="Garfield")
assert encoder.encode(pet) == {"name": "Garfield"}
def test_encode_binary_attribute(encoder):
class Pet(Model):
name = UnicodeAttribute()
age = NumberAttribute()
weight = BinaryAttribute(legacy_encoding=True)
pet = Pet(name="Garfield", age=43, weight=bytes([40]))
assert encoder.encode(pet) == {"name": "Garfield", "age": 43, "weight": b"KA=="}
def test_encode_list_attribute(encoder):
class Pet(Model):
name = UnicodeAttribute()
age = NumberAttribute()
likes = ListAttribute()
pet = Pet(name="Garfield", age=43, likes=["lasagna"])
assert encoder.encode(pet) == {"name": "Garfield", "age": 43, "likes": ["lasagna"]}
def test_encode_map_attribute(encoder):
class Pet(Model):
name = UnicodeAttribute()
age = NumberAttribute()
tags = MapAttribute()
pet = Pet(name="Garfield", age=43, tags={"breed": "Tabby"})
assert encoder.encode(pet) == {"name": "Garfield", "age": 43, "tags": {"breed": "Tabby"}}
def test_encode_custom_map_attribute(encoder):
class Human(MapAttribute):
name = UnicodeAttribute()
age = NumberAttribute()
class Pet(Model):
name = UnicodeAttribute()
age = NumberAttribute()
owner = Human()
pet = Pet(name="Garfield", age=43, owner=Human(name="Jon", age=70))
assert encoder.encode(pet) == {"name": "Garfield", "age": 43, "owner": {"name": "Jon", "age": 70}}
def test_encode_dynamic_map_attribute(encoder):
class Human(DynamicMapAttribute):
name = UnicodeAttribute()
class Pet(Model):
name = UnicodeAttribute()
age = NumberAttribute()
owner = Human()
pet = Pet(name="Garfield", age=43, owner=Human(name="Jon", job="Cartoonist"))
assert encoder.encode(pet) == {"name": "Garfield", "age": 43, "owner": {"name": "Jon", "job": "Cartoonist"}}
def test_encode_typed_list_attribute(encoder):
class Pet(Model):
name = UnicodeAttribute()
age = NumberAttribute()
pets = ListAttribute(of=MapAttribute)
pet = Pet(name="Garfield", age=43, pets=[MapAttribute(name="Pooky")])
assert encoder.encode(pet) == {"name": "Garfield", "age": 43, "pets": [{"name": "Pooky"}]}
def test_encode_polymorphic_attribute(encoder):
class Pet(MapAttribute):
cls = DiscriminatorAttribute()
class Cat(Pet, discriminator="Cat"):
name = UnicodeAttribute()
class Dog(Pet, discriminator="Dog"):
breed = UnicodeAttribute()
cat = Cat(name="Garfield")
assert encoder.encode(cat) == {"cls": "Cat", "name": "Garfield"}
dog = Dog(breed="Terrier")
assert encoder.encode(dog) == {"cls": "Dog", "breed": "Terrier"}
def test_encode_polymorphic_model(encoder):
class Pet(Model):
cls = DiscriminatorAttribute()
class Cat(Pet, discriminator="Cat"):
name = UnicodeAttribute()
class Dog(Pet, discriminator="Dog"):
breed = UnicodeAttribute()
cat = Cat(name="Garfield")
assert encoder.encode(cat) == {"cls": "Cat", "name": "Garfield"}
dog = Dog(breed="Terrier")
assert encoder.encode(dog) == {"cls": "Dog", "breed": "Terrier"}
def test_encode_complex_model(encoder):
class Pet(DynamicMapAttribute):
cls = DiscriminatorAttribute()
name = UnicodeAttribute()
class Cat(Pet, discriminator="Cat"):
pass
class Dog(Pet, discriminator="Dog"):
pass
class Human(MapAttribute):
name = UnicodeAttribute()
pets = ListAttribute(of=Pet)
jon = Human(name="Jon", pets=[Cat(name="Garfield", age=43), Dog(name="Odie")])
assert encoder.encode(jon) == {
"name": "Jon",
"pets": [{"cls": "Cat", "name": "Garfield", "age": 43}, {"cls": "Dog", "name": "Odie"}],
}
def test_encode_all_primitive_types(encoder):
class Foo(Model):
binary = BinaryAttribute(legacy_encoding=True)
binary_set = BinarySetAttribute(legacy_encoding=True)
boolean = BooleanAttribute()
json = JSONAttribute()
number = NumberAttribute()
number_set = NumberSetAttribute()
ttl = TTLAttribute()
unicode = UnicodeAttribute()
unicode_set = UnicodeSetAttribute()
utc_datetime = UTCDateTimeAttribute()
version = VersionAttribute()
now = datetime.now(tz=timezone.utc)
foo = Foo(
binary=bytes([0]),
binary_set=[bytes([0])],
boolean=True,
json={"key": "value"},
number=1,
number_set=[1],
ttl=now,
unicode="foo",
unicode_set=["foo"],
utc_datetime=now,
version=1,
)
assert encoder.encode(foo) == {
"binary": b"AA==",
"binary_set": [b"AA=="],
"boolean": True,
"json": json.dumps({"key": "value"}),
"number": 1,
"number_set": [1],
"ttl": now.replace(microsecond=0).timestamp(),
"unicode": "foo",
"unicode_set": ["foo"],
"utc_datetime": now.isoformat(),
"version": 1,
}