Exemplo n.º 1
0
 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
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
 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)]))
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
    def test_3_nested(self):
        dt = create_dataclass(
            {
                "first_nested": {
                    "second_nested": {
                        "rien": "bla"
                    }
                },
                "texte": "text",
                "datedate": "1234-12-01",
            },
            "ONe",
            custom={"datedate": {
                "type": datetime
            }},
        )
        assert is_dataclass(dt)
        compare_class(One_object, dt, "metadata")

        # 2 level
        level2 = get_field(dt, "first_nested")
        assert is_dataclass(level2)
        compare_class(First_nested, level2)

        # 3rd level
        level3 = get_field(dt, "first_nested.second_nested")
        assert is_dataclass(level3)
        compare_class(Second_nested, level3)
Exemplo n.º 6
0
    def test_nested(self):
        dt = create_dataclass({"second_nested": {"rien": "bla"}}, "Fn")
        assert is_dataclass(dt)
        compare_class(First_nested, dt)

        assert is_dataclass(fields(dt)[0].type)
        compare_class(Second_nested, fields(dt)[0].type)
Exemplo n.º 7
0
 def test_datetime(self):
     dt = create_dataclass(
         {"when": "2012-04-21T18:25:43-05:00"},
         "Dd",
         custom={"when": {
             "type": datetime
         }},
     )
     assert is_dataclass(dt)
     assert compare_class(DDateTTime, dt, without="metadata")
Exemplo n.º 8
0
 def test_custom_metadata(self):
     dt = create_dataclass(
         {"when": "2012-04-21T18:25:43-05:00"},
         "Dd",
         custom={"when": {
             "type": datetime
         }},
     )
     assert fields(dt)[0].metadata == {
         "autodtc": Custom(type=datetime, fn=None, args=[], kwargs={})
     }
Exemplo n.º 9
0
    def test_process_list_class_ok(self):
        data = json.loads(JSON_LIST)
        dt = create_dataclass(data,
                              "ref",
                              custom={"timedate": {
                                  "type": datetime
                              }})

        compare_class(JsonListDataclass, dt[0], "metadata")
        assert get_field(dt[0], "timedate").metadata == types.MappingProxyType(
            {"autodtc": Custom(datetime)})
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
    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)))
Exemplo n.º 12
0
    def test_custom_3_levels(self):
        dt = create_dataclass(
            {"first_nested": {
                "second_nested": {
                    "dtime": "1324-12-12"
                }
            }},
            "ONe",
            custom={"first_nested.second_nested.dtime": {
                "type": datetime
            }},
        )

        assert get_field(dt,
                         "first_nested.second_nested.dtime").type == datetime
        assert get_field(dt, "first_nested.second_nested.dtime").metadata == {
            "autodtc": Custom(type=datetime, fn=None, args=[], kwargs={})
        }
Exemplo n.º 13
0
 def test_sample(self):
     data = json.loads(SAMPLE)
     dt = create_dataclass(
         data,
         "Sample",
         custom={
             "timedate": {
                 "type": datetime
             },
             "one_object.datedate": {
                 "type": datetime
             },
         },
     )
     assert is_dataclass(dt)
     compare_class(SampleDataClass, dt, without="metadata")
     assert get_field(
         dt, "one_object.datedate").metadata == types.MappingProxyType(
             {"autodtc": Custom(datetime)})
Exemplo n.º 14
0
 def test_process_list_list_of_obj(self):
     data = json.loads(JSON_LIST_INSIDE_OBJ)
     create_dataclass(data, "ref")
     assert "Ref" in cache
     assert "Objecs_item" in cache
Exemplo n.º 15
0
 def test_instance(self):
     a = create_dataclass({}, "rien")
     assert cache == {"Rien": a}
Exemplo n.º 16
0
 def test_populate(self, data, name, custom, out):
     create_dataclass(data, name, custom)
     populated = asdict(populate(data, name))
     assert out == populated
Exemplo n.º 17
0
 def test_custom_fn(self, res, custom):
     data = {"text": "bla"}
     create_dataclass(data, "Obj", custom=custom)
     assert populate(data, "Obj").text == res
Exemplo n.º 18
0
 def test_name_capswords(self):
     data = {"text": "bla'"}
     dt = create_dataclass(data, "obj")
     dt.__name__ == "Obj"
Exemplo n.º 19
0
 def test_simple(self):
     dt = create_dataclass({"string": "bla"}, "Objj")
     assert is_dataclass(dt)
     assert compare_class(Obj, dt)
Exemplo n.º 20
0
 def test_process_list_keep_only_one(self):
     data = json.loads(JSON_LIST)
     dt = create_dataclass(data, "ref")
     assert len(dt) == 1
Exemplo n.º 21
0
 def test_get_cache_present_in_create_dataclass(self):
     data = {"Obj": {"text": "bla'"}}
     dt = create_dataclass(data["Obj"], "Obj")
     ddt = create_dataclass(data["Obj"], "Obj")
     assert dt is ddt
Exemplo n.º 22
0
 def test_write_cache(self):
     data = {"text": "bla'"}
     dt = create_dataclass(data, "obj")
     dv = create_dataclass(data, "objo")
     assert cache == {"Obj": dt, "Objo": dv}
Exemplo n.º 23
0
 def test_order(self):
     data = {"maliste": [1, 2, 3], "string": "bla"}
     assert create_dataclass(data, "UneListe")
Exemplo n.º 24
0
 def test_list(self):
     data = {"maliste": [1, 2, 3]}
     dt = create_dataclass(data, "UneListe")
     compare_class(SimpleList, dt)
Exemplo n.º 25
0
 def test_get_cache_present_inside_process_dict(self):
     data = {"text": "bla'"}
     data2 = {"obj": {"text": "molpj"}}
     dt = create_dataclass(data, "Obj")
     dt2 = create_dataclass(data2, "Obj2")
     assert get_field(dt2, "obj") is dt