def test_map_empty_class_l_to_r(self):
        l_to_r_mapper = Mapper()
        l_to_r_mapper.mapping(A, B).l_to_r_empty().register()

        b = l_to_r_mapper.map(A(), B)

        self.assertEqual(b.__class__, B)
    def test_map_empty_class_r_to_l(self):
        r_to_l_mapper = Mapper()
        r_to_l_mapper.mapping(A, B).r_to_l_empty().register()

        a = r_to_l_mapper.map(B(), A)

        self.assertEqual(a.__class__, A)
    def test_parse_optional_forward_ref_enum(self):
        mapper = Mapper()
        mapper.mapping(dict, LangCarrier).map_matching().register()
        mapper.mapping(str, Lang).l_to_r_converter(lambda s: Lang(s)).register()

        lang_carrier = mapper.map({"lang": "python"}, LangCarrier)
        self.assertEqual(lang_carrier.lang, Lang.PYTHON)
Пример #4
0
    def test_list_to_proto_mapping(self):
        mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])

        mapper.mapping(Simple, SimpleData).map_matching().register()
        mapper.mapping(ListOfSimple,
                       ListOfSimpleData).map_matching().register()

        proto = mapper.map(
            ListOfSimpleData([SimpleData("abc"),
                              SimpleData("def")]), ListOfSimple)

        self.assertEqual(proto.__class__, ListOfSimple)
        self.assertEqual(len(proto.value), 2)
        self.assertEqual(proto.value[0].__class__, Simple)
        self.assertEqual(proto.value[0].value, "abc")
        self.assertEqual(proto.value[1].__class__, Simple)
        self.assertEqual(proto.value[1].value, "def")

        data = mapper.map(
            ListOfSimple(value=[Simple(
                value="123"), Simple(value="xyz")]), ListOfSimpleData)
        self.assertEqual(data.__class__, ListOfSimpleData)
        self.assertEqual(len(data.value), 2)
        self.assertEqual(data.value[0].__class__, SimpleData)
        self.assertEqual(data.value[0].value, "123")
        self.assertEqual(data.value[1].__class__, SimpleData)
        self.assertEqual(data.value[1].value, "xyz")
Пример #5
0
    def test_map_with_converter(self):
        mapper = Mapper()
        mapper.mapping(
            A, B).l_to_r_converter(lambda a: B(a.a_value + 10)).register()

        b = mapper.map(A(123), B)
        self.assertEqual(b.b_value, 133)
Пример #6
0
    def test_map_matching(self):
        class A:
            def __init__(self, a_value: int, common_value: str):
                self.a_value = a_value
                self.common_value = common_value

        class B:
            def __init__(self, b_value: int, common_value: str):
                self.b_value = b_value
                self.common_value = common_value

        mapper = Mapper()
        mapper.mapping(A,
                       B).bidirectional("a_value",
                                        "b_value").map_matching().register()

        b = mapper.map(A(123, "456"), B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.b_value, 123)
        self.assertEqual(b.common_value, "456")

        a = mapper.map(B(789, "xyz"), A)

        self.assertEqual(a.__class__, A)
        self.assertEqual(a.a_value, 789)
        self.assertEqual(a.common_value, "xyz")
Пример #7
0
    def test_map_primitive_class_r_to_l(self):
        r_to_l_mapper = Mapper()
        r_to_l_mapper.mapping(A, B).r_to_l("a_value", "b_value").register()

        a = r_to_l_mapper.map(B(456), A)

        self.assertEqual(a.__class__, A)
        self.assertEqual(a.a_value, 456)
Пример #8
0
    def test_map_primitive_class_l_to_r(self):
        l_to_r_mapper = Mapper()
        l_to_r_mapper.mapping(A, B).l_to_r("a_value", "b_value").register()

        b = l_to_r_mapper.map(A(123), B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.b_value, 123)
Пример #9
0
    def test_map_proto_enum_value_with_converter(self):
        mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])
        mapper.mapping(
            PyLang,
            Lang).l_to_r_converter(lambda l: Lang.Value(l.name)).register()

        proto_lang = mapper.map(PyLang.JAVA, Lang)
        self.assertEqual(proto_lang, Lang.Value("JAVA"))
Пример #10
0
    def test_forward_ref_resolving(self):
        mapper = Mapper()
        mapper.mapping(ForwardRefCarrierA, dict).map_matching().register()
        mapper.mapping(ForwardReferencedA, dict).map_matching().register()

        a = mapper.map({"value": {"value": "abc"}}, ForwardRefCarrierA)

        self.assertEqual(a.__class__, ForwardRefCarrierA)
        self.assertEqual(a.value.__class__, ForwardReferencedA)
        self.assertEqual(a.value.value, "abc")
Пример #11
0
    def test_map_nested_dataclasses(self):
        mapper = Mapper()
        mapper.mapping(OuterA, OuterB).map_matching().register()
        mapper.mapping(NestedA, NestedB).map_matching().register()

        b = mapper.map(OuterA(NestedA(123)), OuterB)

        self.assertEqual(b.__class__, OuterB)
        self.assertEqual(b.nested.__class__, NestedB)
        self.assertEqual(b.nested.value, 123)
Пример #12
0
    def test_map_with_context(self):
        mapper = Mapper()
        mapper.mapping(A, B).l_to_r_converter(
            lambda a, ctx: B(a.a_value, ctx["contextual"])).register()

        b = mapper.map(A(123), B, {"contextual": "contextual value"})

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.b_value, 123)
        self.assertEqual(b.contextual, "contextual value")
    def test_map_empty_class_bidirectional(self):
        bi_d_mapper = Mapper()
        bi_d_mapper.mapping(A, B).bidirectional_empty().register()

        b = bi_d_mapper.map(A(), B)

        self.assertEqual(b.__class__, B)

        a = bi_d_mapper.map(B(), A)

        self.assertEqual(a.__class__, A)
Пример #14
0
    def test_map_non_constructor_attributes(self):
        mapper = Mapper()
        mapper.mapping(A, B).l_to_r("a_value", "b_value").l_to_r(
            "additional_value", "additional_value").register()

        a = A(123)
        a.additional_value = 456
        b = mapper.map(a, B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.b_value, 123)
        self.assertEqual(b.additional_value, 456)
    def test_simple_map_to_dict(self):
        mapper = Mapper()
        mapper.mapping(Simple, dict).map_matching().register()

        s = mapper.map({"value": "abc"}, Simple)

        self.assertEqual(s.__class__, Simple)
        self.assertEqual(s.value, "abc")

        d = mapper.map(Simple("def"), dict)

        self.assertEqual(d, {"value": "def"})
Пример #16
0
    def test_raises_correct_exception_on_missing_mapping_with_forward_ref(
            self):
        mapper = Mapper()
        mapper.mapping(ForwardRefListCarrierA,
                       ForwardRefListCarrierB).map_matching().register()
        mapper.mapping(ForwardReferencedB, dict).map_matching().register()

        with self.assertRaises(FieldMappingException):
            mapper.map(
                ForwardRefListCarrierA(
                    [ForwardReferencedA("abc"),
                     ForwardReferencedA("def")]), ForwardRefListCarrierB)
    def test_map_with_custom_converter(self):
        def converter(i: int) -> str:
            return f"--{i}--"

        l_to_r_mapper = Mapper()
        l_to_r_mapper.mapping(A, B).l_to_r("a_value", "b_value",
                                           converter).register()

        b = l_to_r_mapper.map(A(123), B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.b_value, "--123--")
Пример #18
0
    def test_simple_proto_mapping(self):
        mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])

        mapper.mapping(Simple, SimpleData).map_matching().register()

        s = mapper.map(SimpleData("abc"), Simple)

        self.assertEqual(s.__class__, Simple)
        self.assertEqual(s.value, "abc")

        d = mapper.map(Simple(value="def"), SimpleData)

        self.assertEqual(d.__class__, SimpleData)
        self.assertEqual(d.value, "def")
Пример #19
0
    def test_map_primitive_class_bidirectional(self):
        bi_d_mapper = Mapper()
        bi_d_mapper.mapping(A, B).bidirectional("a_value",
                                                "b_value").register()

        b = bi_d_mapper.map(A(123), B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.b_value, 123)

        a = bi_d_mapper.map(B(456), A)

        self.assertEqual(a.__class__, A)
        self.assertEqual(a.a_value, 456)
    def test_map_from_dict(self):
        mapper = Mapper()
        mapper.mapping(A, dict).map_matching().register()
        mapper.mapping(Nested, dict).map_matching().register()

        a = mapper.map({"nested": {"value": "abc",}, "list_of_nested": [{"value": "def",}, {"value": "xyz",}]}, A,)

        self.assertEqual(a.__class__, A)
        self.assertEqual(a.nested.__class__, Nested)
        self.assertEqual(a.nested.value, "abc")
        self.assertEqual(len(a.list_of_nested), 2)
        self.assertEqual(a.list_of_nested[0].__class__, Nested)
        self.assertEqual(a.list_of_nested[0].value, "def")
        self.assertEqual(a.list_of_nested[1].value, "xyz")
    def test_map_non_constructor_field_with_custom_converter(self):
        def converter(i: int) -> str:
            return f"--{i}--"

        l_to_r_mapper = Mapper()
        l_to_r_mapper.mapping(C, B).l_to_r("c_value", "b_value",
                                           converter).register()

        c = C()
        c.c_value = 123
        b = l_to_r_mapper.map(c, B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.b_value, "--123--")
Пример #22
0
    def test_simple_proto_mapping_with_ignore_case(self):
        mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])

        mapper.mapping(Simple,
                       BadCasedData).map_matching(ignore_case=True).register()

        s = mapper.map(BadCasedData("abc"), Simple)

        self.assertEqual(s.__class__, Simple)
        self.assertEqual(s.value, "abc")

        d = mapper.map(Simple(value="def"), BadCasedData)

        self.assertEqual(d.__class__, BadCasedData)
        self.assertEqual(d.Va_LuE, "def")
Пример #23
0
    def test_map_optional_value(self):
        mapper = Mapper()
        mapper.mapping(StringCarrier,
                       OptionalStringCarrier).map_matching().register()

        b = mapper.map(StringCarrier("123"), OptionalStringCarrier)

        self.assertEqual(b.value, "123")

        a = mapper.map(OptionalStringCarrier("456"), StringCarrier)

        self.assertEqual(a.value, "456")

        with self.assertRaises(MissingMappingException):
            mapper.map(OptionalStringCarrier(None), StringCarrier)
    def test_map_int_to_str_field(self):
        @dataclass
        class A:
            value: int

        @dataclass
        class B:
            value: str

        mapper = Mapper()
        mapper.mapping(A, B).map_matching().register()

        b = mapper.map(A(123), B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.value, "123")
Пример #25
0
    def test_container_proto_mapping(self):
        mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])

        mapper.mapping(Simple, SimpleData).map_matching().register()
        mapper.mapping(Container, ContainerData).map_matching().register()

        proto = mapper.map(ContainerData(SimpleData("abc")), Container)

        self.assertEqual(proto.__class__, Container)
        self.assertEqual(proto.value.__class__, Simple)
        self.assertEqual(proto.value.value, "abc")

        data = mapper.map(Container(value=Simple(value="def")), ContainerData)

        self.assertEqual(data.__class__, ContainerData)
        self.assertEqual(data.value.__class__, SimpleData)
        self.assertEqual(data.value.value, "def")
Пример #26
0
    def test_map_nested_classes(self):
        mapper = Mapper()
        mapper.mapping(A, B).map_matching().register()

        mapper.mapping(NestedA, NestedB).map_matching().register()

        b = mapper.map(A(NestedA("abc")), B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.value.__class__, NestedB)
        self.assertEqual(b.value.value, "abc")

        a = mapper.map(B(NestedB("xyz")), A)

        self.assertEqual(a.__class__, A)
        self.assertEqual(a.value.__class__, NestedA)
        self.assertEqual(a.value.value, "xyz")
Пример #27
0
    def test_map_matching_dataclasses(self):
        mapper = Mapper()
        mapper.mapping(A,
                       B).bidirectional("a_value",
                                        "b_value").map_matching().register()

        b = mapper.map(A("123", 456), B)

        self.assertEqual(b.__class__, B)
        self.assertEqual(b.b_value, "123")
        self.assertEqual(b.common_value, 456)

        a = mapper.map(B("xyz", 789), A)

        self.assertEqual(a.__class__, A)
        self.assertEqual(a.a_value, "xyz")
        self.assertEqual(a.common_value, 789)
Пример #28
0
    def test_forward_ref_in_list_resolving(self):
        mapper = Mapper()
        mapper.mapping(ForwardRefListCarrierA,
                       ForwardRefListCarrierB).map_matching().register()
        mapper.mapping(ForwardReferencedA,
                       ForwardReferencedB).map_matching().register()

        b = mapper.map(
            ForwardRefListCarrierA(
                [ForwardReferencedA("abc"),
                 ForwardReferencedA("def")]), ForwardRefListCarrierB)

        self.assertEqual(b.__class__, ForwardRefListCarrierB)
        self.assertEqual(len(b.value), 2)
        self.assertEqual(b.value[0].__class__, ForwardReferencedB)
        self.assertEqual(b.value[0].value, "abc")
        self.assertEqual(b.value[1].__class__, ForwardReferencedB)
        self.assertEqual(b.value[1].value, "def")
Пример #29
0
    def test_map_proto_with_value_map(self):
        mapper = Mapper(custom_descriptors=[ProtoMappingDescriptor])

        pairs = [
            (PyLang.PYTHON, Lang.Value("PYTHON")),
            (PyLang.JAVA, Lang.Value("JAVA")),
            (PyLang.CPP, Lang.Value("CPP")),
        ]

        mapper.mapping(PyLang, Lang).l_to_r_converter(
            values_map({py: proto
                        for py, proto in pairs})).r_to_l_converter(
                            values_map({proto: py
                                        for py, proto in pairs})).register()
        mapper.mapping(PyLangCarrier,
                       LangCarrier).bidirectional("value", "lang").register()

        self.assertEqual(mapper.map(PyLang.JAVA, Lang), Lang.Value("JAVA"))
        self.assertEqual(mapper.map(PyLang.PYTHON, Lang), Lang.Value("PYTHON"))
        self.assertEqual(mapper.map(PyLang.CPP, Lang), Lang.Value("CPP"))

        c = mapper.map(PyLangCarrier(PyLang.CPP), LangCarrier)
        self.assertEqual(c.__class__, LangCarrier)
        self.assertEqual(c.lang, Lang.Value("CPP"))
Пример #30
0
 def test_raise_when_converter_and_mapping_defined(self):
     with self.assertRaises(ImproperlyConfiguredException):
         mapper = Mapper()
         mapper.mapping(
             A, B).l_to_r_converter(lambda a: B(a.a_value + 10)).l_to_r(
                 "a_value", "b_value").register()