def test_populate_nested(self): name = "obj" data = {"nested": {"rien": "bla"}} create_dataclass(data, name) populated = populate(data, name) cached = cache("Obj")(nested=cache("Nested")(**data["nested"])) assert cached == populated
def test_populate_nested_3_level(self): name = "obj" data = {"first_nested": {"second_nested": {"dtime": "1324-12-12"}}} create_dataclass(data, name) populated = populate(data, name) cached = cache("Obj")(first_nested=cache("First_nested")( second_nested=cache("Second_nested")(**{ "dtime": "1324-12-12" }))) assert cached == populated
def test_list_of_obj(self): data = json.loads(JSON_LIST_INSIDE_OBJ_CUSTOM) create_dataclass(data, "ref", custom={"objecs.timedate": { "type": datetime }}) a = populate(data, "ref") ref = cache("Ref") objecs_item = cache("Objecs_item") compare_instance(a, ref(a=1, objecs=[objecs_item(timedate=DTIME)]))
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
def test_custom_fn(self, res, custom): data = {"text": "bla"} create_dataclass(data, "Obj", custom=custom) assert populate(data, "Obj").text == res
def test_populate(self, data, name, custom, out): create_dataclass(data, name, custom) populated = asdict(populate(data, name)) assert out == populated