def test_populate_from_dict(self, data, fn): res = fn( data, "Sample", custom={ "timedate": { "type": datetime }, "one_object.datedate": { "type": datetime }, }, ) assert res == cache("Sample")( flooat=12.564, boolean=True, integer=12, array=[1, 2, 3, 4], string="bla", obj=cache("Obj")(string="rinen"), timedate=DTIME, one_object=cache("One_object")( first_nested=cache("First_nested")( second_nested=cache("Second_nested")(rien="rienstr")), texte="text", datedate=datetime(1234, 12, 1), ), null=None, )
def test_custom_fn(self): data = {"timedate": "21 04 2012"} ref = create_dataclass( data, "Ref", custom={ "timedate": { "type": datetime, "fn": datetime.strptime, "args": ["%d %m %Y"], } }, ) a = populate(data, "ref") compare_instance(a, ref(timedate=datetime(2012, 4, 21)))
def test_populate_nested_3_level_custom(self): name = "obj" data = {"first_nested": {"second_nested": {"dtime": "1324-12-12"}}} create_dataclass( data, name, custom={"first_nested.second_nested.dtime": { "type": datetime }}) populated = populate(data, name) cached = cache("Obj")(first_nested=cache("First_nested")( second_nested=cache("Second_nested")( **{ "dtime": datetime(1324, 12, 12) }))) assert cached == populated
def test_sample(self): data = json.loads(SAMPLE) create_dataclass( data, "Sample", custom={ "timedate": { "type": datetime }, "one_object.datedate": { "type": datetime }, }, ) populated = populate(data, "Sample") # data["timedate"] = DTIME cached = cache("Sample")( flooat=12.564, boolean=True, integer=12, array=[1, 2, 3, 4], string="bla", obj=cache("Obj")(string="rinen"), timedate=DTIME, one_object=cache("One_object")( first_nested=cache("First_nested")( second_nested=cache("Second_nested")(rien="rienstr")), texte="text", datedate=datetime(1234, 12, 1), ), null=None, ) assert cached == populated # As dict let datetime object unchanged to_dict = asdict(populated) to_dict["one_object"]["datedate"] = to_dict["one_object"][ "datedate"].strftime("%Y-%m-%d") to_dict["timedate"] = to_dict["timedate"].isoformat() assert to_dict == data
"integer": 12, "array": [1,2,3,4], "string": "bla", "obj": {"string":"rinen"}, "timedate": "2012-04-21T18:25:43-05:00", "one_object": { "first_nested":{"second_nested":{"rien":"rienstr"}}, "texte":"text", "datedate": "1234-12-01" }, "null": null }""" DTIME = datetime( 2012, 4, 21, 18, 25, 43, tzinfo=timezone(timedelta(days=-1, seconds=68400)) ) @dataclass class DDateTTime: when: datetime @dataclass class Second_nested: rien: str @dataclass class First_nested: