@@ -186,44 +186,51 @@ def test_schema_datetime_serialization():
186186 assert data ["check_out" ] is None
187187
188188
189- #
190- #
191- # def test_schema_decimal_serialization():
192- # inventory_item = typesystem.Schema(fields={
193- # "name": typesystem.String(),
194- # "price": typesystem.Decimal(precision="0.01", allow_null=True),
195- # })
196- #
197- # item = {"name": "Example", "price": 123.45}
198- #
199- # assert item.price ==
200- # assert item["price"] == 123.45
201- #
202- # item = InventoryItem(name="test")
203- # assert dict(item) == {"name": "test", "price": None}
204- # item = InventoryItem(name="test", price=0)
205- # assert dict(item) == {"name": "test", "price": 0}
206- #
207- #
208- # def test_schema_uuid_serialization():
209- # class User(typesystem.Schema):
210- # id = typesystem.String(format="uuid")
211- # username = typesystem.String()
212- #
213- # item = User(id="b769df4a-18ec-480f-89ef-8ea961a82269", username="tom")
214- #
215- # assert item.id == uuid.UUID("b769df4a-18ec-480f-89ef-8ea961a82269")
216- # assert item["id"] == "b769df4a-18ec-480f-89ef-8ea961a82269"
217- #
218- #
219- # def test_schema_with_callable_default():
220- # class Example(typesystem.Schema):
221- # created = typesystem.Date(default=datetime.date.today)
222- #
223- # value, error = Example.validate_or_error({})
224- # assert value.created == datetime.date.today()
225- #
226- #
189+ def test_schema_decimal_serialization ():
190+ inventory = typesystem .Schema (
191+ fields = {
192+ "name" : typesystem .String (),
193+ "price" : typesystem .Decimal (precision = "0.01" , allow_null = True ),
194+ }
195+ )
196+
197+ item = {"name" : "example" , "price" : 123.45 }
198+ data = inventory .serialize (item )
199+
200+ assert data ["name" ] == "example"
201+ assert data ["price" ] == 123.45
202+
203+ item = {"name" : "example" , "price" : None }
204+ assert inventory .serialize (item ) == {"name" : "example" , "price" : None }
205+
206+ item = {"name" : "example" , "price" : 0 }
207+ assert inventory .serialize (item ) == {"name" : "example" , "price" : 0 }
208+
209+
210+ def test_schema_uuid_serialization ():
211+ user = typesystem .Schema (
212+ fields = {
213+ "id" : typesystem .String (format = "uuid" ),
214+ "username" : typesystem .String (),
215+ }
216+ )
217+
218+ item = {"id" : "b769df4a-18ec-480f-89ef-8ea961a82269" , "username" : "tom" }
219+ data = user .serialize (item )
220+
221+ assert data ["id" ] == "b769df4a-18ec-480f-89ef-8ea961a82269"
222+ assert data ["username" ] == "tom"
223+
224+
225+ def test_schema_with_callable_default ():
226+ schema = typesystem .Schema (
227+ fields = {"created" : typesystem .Date (default = datetime .date .today )}
228+ )
229+
230+ value , _ = schema .validate_or_error ({})
231+ assert value ["created" ] == datetime .date .today ()
232+
233+
227234# def test_nested_schema():
228235# class Artist(typesystem.Schema):
229236# name = typesystem.String(max_length=100)
0 commit comments