Example #1
0
    def test_map_with_multiple_nested_mappings_for_one_attribute_when_target_type_known(
            self):
        # given
        root_mapper = ObjectMapper.from_prototype(
            TestClassSomeProperty1(None).__dict__,
            TestClassSomeProperty2(
                some_property=TestClassSomePropertyEmptyInit2()))

        root_mapper.nested_mapper(
            ObjectMapper.from_prototype(
                TestClassSomePropertyEmptyInit1().__dict__,
                TestClassSomeProperty2(None)))
        root_mapper.nested_mapper(
            ObjectMapper.from_prototype(
                TestClassSomePropertyEmptyInit1().__dict__,
                TestClassSomePropertyEmptyInit2()))

        # when
        mapped_object = root_mapper.map(
            dict(some_property=dict(some_property_02="nested_value_02")))

        # then
        assert_that(mapped_object).is_instance_of(TestClassSomeProperty2)

        nested_mapped_obj = mapped_object.some_property
        assert_that(nested_mapped_obj).is_not_none()
        assert_that(nested_mapped_obj).is_instance_of(
            TestClassSomePropertyEmptyInit2)
        assert_that(
            nested_mapped_obj.some_property_02).is_equal_to("nested_value_02")
Example #2
0
    def test_map_unknown_property_should_raise_exception(self):
        # given
        mapper = ObjectMapper.from_class(
            TestClassSomeProperty1, TestClassMappedProperty).custom_mappings(
                {"some_property": "unknown"})

        # when
        try:
            mapper.map(TestClassSomeProperty1(some_property="some_value"))
            self.fail("Should raise AttributeError")
        except AttributeError as er:
            # then
            assert_that(er.args[0]).contains("unknown")

        # given
        mapper = ObjectMapper.from_class(
            TestClassSomeProperty1, TestClassMappedProperty).custom_mappings(
                {"unknown": "mapped_property"})

        # when
        try:
            mapper.map(TestClassSomeProperty1(some_property="some_value"))
            self.fail("Should raise AttributeError")
        except AttributeError as er:
            # then
            assert_that(er.args[0]).contains("unknown")
Example #3
0
    def test_map_to_dict_with_nested_mapper(self):
        # given
        root_mapper = ObjectMapper.from_prototype(TestClassSomeProperty1(None), TestClassSomeProperty2(None).__dict__)
        root_mapper.nested_mapper(
            ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1(), TestClassSomePropertyEmptyInit2().__dict__))

        # when
        mapped_object = root_mapper.map(TestClassSomeProperty1(
            some_property="some_value",
            some_property_02=TestClassSomePropertyEmptyInit1(
                some_property="nested_value",
                some_property_02="nested_value_02",
                some_property_03="nested_value_03",
                unmapped_property1="unmapped_nested_value"),
            some_property_03="some_value_03",
            unmapped_property1="unmapped_value"))

        # then
        assert_that(mapped_object).is_instance_of(dict)
        assert_that(mapped_object['some_property']).is_equal_to("some_value")
        assert_that(mapped_object['some_property_03']).is_equal_to("some_value_03")
        assert_that(mapped_object).does_not_contain_key('unmapped_property2')

        assert_that(mapped_object).contains_key('some_property_02')
        nested_mapped_obj = mapped_object['some_property_02']
        assert_that(nested_mapped_obj).is_instance_of(dict)
        assert_that(nested_mapped_obj['some_property']).is_equal_to("nested_value")
        assert_that(nested_mapped_obj['some_property_02']).is_equal_to("nested_value_02")
        assert_that(nested_mapped_obj['some_property_03']).is_equal_to("nested_value_03")
        assert_that(nested_mapped_obj).does_not_contain_key('unmapped_property2')
Example #4
0
 def test_map_unknown_class_should_raise_exception(self):
     try:
         ObjectMapper.from_class(TestEmptyClass1,
                                 TestEmptyClass2).map(TestOtherClass())
         self.fail("Should raise ValueError")
     except ValueError as er:
         assert_that(er.args[0]).contains(TestOtherClass.__name__)
Example #5
0
    def test_map_from_dict_with_nested_mapper(self):
        # given
        root_mapper = ObjectMapper.from_prototype(TestClassSomeProperty1(None).__dict__, TestClassSomeProperty2(None))
        root_mapper.nested_mapper(
            ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1().__dict__, TestClassSomePropertyEmptyInit2()))

        # when
        mapped_object = root_mapper.map(dict(
            some_property="some_value",
            some_property_02=dict(
                some_property="nested_value",
                some_property_02="nested_value_02",
                some_property_03="nested_value_03",
                unmapped_property1="unmapped_nested_value"),
            some_property_03="some_value_03",
            unmapped_property1="unmapped_value"))

        # then
        assert_that(mapped_object).is_instance_of(TestClassSomeProperty2)
        assert_that(mapped_object.some_property).is_equal_to("some_value")
        assert_that(mapped_object.some_property_03).is_equal_to("some_value_03")
        assert_that(mapped_object.unmapped_property2).is_none()

        nested_mapped_obj = mapped_object.some_property_02
        assert_that(nested_mapped_obj).is_instance_of(TestClassSomePropertyEmptyInit2)
        assert_that(nested_mapped_obj.some_property).is_equal_to("nested_value")
        assert_that(nested_mapped_obj.some_property_02).is_equal_to("nested_value_02")
        assert_that(nested_mapped_obj.some_property_03).is_equal_to("nested_value_03")
        assert_that(nested_mapped_obj.unmapped_property2).is_none()
Example #6
0
 def test_map_attr_value_when_direction_nor_target_set_should_raise_exception(
         self):
     with self.assertRaises(ValueError) as context:
         ObjectMapper.from_class(
             TestClassSomePropertyEmptyInit1,
             TestClassSomePropertyEmptyInit2).map_attr_value(
                 "some_property", "some_value", None, None)
Example #7
0
    def test_map_with_multiple_nested_mappings_when_no_matching_mapper_for_target_type_should_raise_exception(
            self):
        # given
        root_mapper = ObjectMapper.from_prototype(
            TestClassSomeProperty1(None).__dict__,
            TestClassSomeProperty2(
                some_property=TestClassMappedPropertyEmptyInit()))

        root_mapper.nested_mapper(
            ObjectMapper.from_prototype(
                TestClassSomePropertyEmptyInit1().__dict__,
                TestClassSomeProperty2(None)))
        root_mapper.nested_mapper(
            ObjectMapper.from_prototype(
                TestClassSomePropertyEmptyInit1().__dict__,
                TestClassSomePropertyEmptyInit2()))

        with self.assertRaises(ConfigurationException) as context:
            root_mapper.map(
                dict(some_property=dict(some_property_02="nested_value_02")))

        # then
        assert_that(context.exception.args[0]).contains("some_property")
        assert_that(context.exception.args[0]).contains("dict")
        assert_that(context.exception.args[0]).contains(
            "TestClassMappedPropertyEmptyInit")
Example #8
0
    def test_object_mapper_value_converters_when_converter_not_callable_should_raise_exception(
            self):
        with self.assertRaises(ValueError) as context:
            ObjectMapper.from_class(
                TestClassSomePropertyEmptyInit1,
                TestClassSomePropertyEmptyInit2).value_converters(
                    {"some_property_02": (lambda val: 7, "not_a_function")})

        assert_that(context.exception.args[0]).contains("some_property_02")
    def test_object_mapper_value_converters_when_converter_not_in_2_element_tuple_should_raise_exception(
            self):
        with self.assertRaises(ValueError) as context:
            ObjectMapper.from_class(
                TestClassSomePropertyEmptyInit1,
                TestClassSomePropertyEmptyInit2).value_converters(
                    {"some_property_02": (lambda val: 7, )})

        assert_that(context.exception.message).contains("some_property_02")
Example #10
0
    def test_map_attr_value_when_attr_name_unknown_should_raise_exception_rev(
            self):
        # when
        with self.assertRaises(ValueError) as context:
            ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassSomePropertyEmptyInit2).\
                map_attr_value("unknown_property", "some_value", MappingDirection.right_to_left)

        # then
        assert_that(context.exception.args[0]).contains("unknown_property")
Example #11
0
    def test_map_attr_value_when_opposite_direction_should_raise_exception_rev(
            self):
        # when
        with self.assertRaises(ValueError) as context:
            ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassMappedPropertyEmptyInit).\
                custom_mappings({"some_property_02": "mapped_property_02"}).\
                map_attr_value("mapped_property_02", "some_value", MappingDirection.left_to_right)

        # then
        assert_that(context.exception.args[0]).contains("mapped_property_02")
        assert_that(context.exception.args[0]).contains("left_to_right")
Example #12
0
    def test_map_attr_value_when_opposite_direction_for_class_should_raise_exception(
            self):
        # when
        with self.assertRaises(ValueError) as context:
            ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassMappedPropertyEmptyInit).\
                custom_mappings({"some_property_02": "mapped_property_02"}).\
                map_attr_value("some_property_02", "some_value", target_class=TestClassSomePropertyEmptyInit1)

        # then
        assert_that(context.exception.message).contains("some_property_02")
        assert_that(context.exception.message).contains(
            "TestClassSomePropertyEmptyInit1")
Example #13
0
    def test_map_when_ambiguous_nested_mapping_should_raise_exception(self):
        # given
        root_mapper = ObjectMapper.from_prototype(TestClassSomeProperty1(None).__dict__, TestClassSomeProperty2(None))
        root_mapper.nested_mapper(
            ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1().__dict__, TestClassSomePropertyEmptyInit2()))
        root_mapper.nested_mapper(
            ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1().__dict__, TestClassSomeProperty2(None)))

        # when
        with self.assertRaises(ConfigurationException) as context:
            root_mapper.map(dict(some_property=TestClassSomePropertyEmptyInit1().__dict__))

        # then
        assert_that(context.exception.message).contains("some_property")
        assert_that(context.exception.message).contains("dict")
Example #14
0
    def test_suppress_implicit_mapping(self):
        # given
        mapper = ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassSomePropertyEmptyInit2).\
            custom_mappings({"some_property": None})

        # when
        mapped_object = mapper.map(
            TestClassSomePropertyEmptyInit1(some_property="some_value",
                                            some_property_02="some_value_02"))

        # then
        assert_that(mapped_object).is_instance_of(
            TestClassSomePropertyEmptyInit2)
        assert_that(mapped_object.some_property).is_none()
        assert_that(
            mapped_object.some_property_02).is_equal_to("some_value_02")

        # when
        mapped_object_rev = mapper.map(
            TestClassSomePropertyEmptyInit2(some_property="some_value",
                                            some_property_02="some_value_02"))

        # then
        assert_that(mapped_object_rev).is_instance_of(
            TestClassSomePropertyEmptyInit1)
        assert_that(mapped_object_rev.some_property).is_none()
        assert_that(
            mapped_object_rev.some_property_02).is_equal_to("some_value_02")
Example #15
0
    def test_map_explicit_when_ambiguous_nested_mapping_should_raise_exception(self):
        # given
        root_mapper = ObjectMapper.from_class(dict, TestClassMappedProperty).\
            custom_mappings({"some_property": "mapped_property"})

        root_mapper.nested_mapper(ObjectMapper.from_class(dict, TestClassSomePropertyEmptyInit2))
        root_mapper.nested_mapper(ObjectMapper.from_class(dict, TestClassSomeProperty2))

        # when
        with self.assertRaises(ConfigurationException) as context:
            root_mapper.map(dict(some_property=dict()))

        # then
        assert_that(context.exception.message).contains("some_property")
        assert_that(context.exception.message).contains("mapped_property")
        assert_that(context.exception.message).contains("dict")
Example #16
0
    def test_map_both_ways(self):
        # given
        mapper = ObjectMapper.from_prototype(
            TestClassSomePropertyEmptyInit1(some_property_02="str"),
            TestClassSomePropertyEmptyInit2(some_property_02=datetime.now()))
        test_datetime = datetime.now()

        # when
        mapped_object = mapper.map(
            TestClassSomePropertyEmptyInit1(
                some_property="some_value",
                some_property_02=test_datetime.isoformat()))

        # then
        assert_that(mapped_object).is_instance_of(
            TestClassSomePropertyEmptyInit2)
        assert_that(mapped_object.some_property).is_equal_to("some_value")
        assert_that(mapped_object.some_property_02).is_instance_of(datetime)
        assert_that(mapped_object.some_property_02).is_equal_to(test_datetime)

        # when
        mapped_object_rev = mapper.map(
            TestClassSomePropertyEmptyInit2(some_property="some_value",
                                            some_property_02=test_datetime))

        # then
        assert_that(mapped_object_rev).is_instance_of(
            TestClassSomePropertyEmptyInit1)
        assert_that(mapped_object_rev.some_property).is_equal_to("some_value")
        assert_that(mapped_object_rev.some_property_02).is_instance_of(str)
        assert_that(mapped_object_rev.some_property_02).is_equal_to(
            test_datetime.isoformat())
Example #17
0
    def test_map_multiple_explicit_properties(self):
        # given
        mapper = ObjectMapper.from_class(dict, TestClassMappedProperty).custom_mappings(
                            {"some_property": "mapped_property",
                             "some_property_02": "mapped_property_02",
                             "some_property_03": "mapped_property_03"})

        # when
        mapped_object = mapper.map(dict(
            some_property="some_value",
            some_property_02="some_value_02",
            some_property_03="some_value_03",
            unmapped_property1="unmapped_value"))

        # then
        assert_that(mapped_object).is_instance_of(TestClassMappedProperty)
        assert_that(mapped_object.mapped_property).is_equal_to("some_value")
        assert_that(mapped_object.mapped_property_02).is_equal_to("some_value_02")
        assert_that(mapped_object.mapped_property_03).is_equal_to("some_value_03")
        assert_that(mapped_object.unmapped_property2).is_none()

        # when
        mapped_object_rev = mapper.map(TestClassMappedProperty(
            mapped_property="other_value",
            mapped_property_02="other_value_02",
            mapped_property_03="other_value_03",
            unmapped_property2="unmapped_value"))

        # then
        assert_that(mapped_object_rev).is_instance_of(dict)
        assert_that(mapped_object_rev['some_property']).is_equal_to("other_value")
        assert_that(mapped_object_rev['some_property_02']).is_equal_to("other_value_02")
        assert_that(mapped_object_rev['some_property_03']).is_equal_to("other_value_03")
        assert_that(mapped_object_rev).does_not_contain_key('unmapped_property1')
Example #18
0
    def test_map_implicit_with_prototype_obj(self):
        # given
        mapper = ObjectMapper.from_prototype(TestClassSomeProperty1(None), TestClassSomeProperty2(None).__dict__)

        # when
        mapped_object = mapper.map(TestClassSomeProperty1(
            some_property="some_value",
            some_property_02=None,
            some_property_03="some_value_03",
            unmapped_property1="unmapped_value"))

        # then
        assert_that(mapped_object).is_instance_of(dict)
        assert_that(mapped_object['some_property']).is_equal_to("some_value")
        assert_that(mapped_object['some_property_02']).is_none()
        assert_that(mapped_object['some_property_03']).is_equal_to("some_value_03")
        assert_that(mapped_object).does_not_contain_key('unmapped_property2')

        # when
        mapped_object_rev = mapper.map(dict(
            some_property="some_value",
            some_property_02="some_value_02",
            some_property_03="some_value_03",
            unmapped_property2="unmapped_value"))

        # then
        assert_that(mapped_object_rev).is_instance_of(TestClassSomeProperty1)
        assert_that(mapped_object_rev.some_property).is_equal_to("some_value")
        assert_that(mapped_object_rev.some_property_02).is_equal_to("some_value_02")
        assert_that(mapped_object_rev.some_property_03).is_equal_to("some_value_03")
        assert_that(mapped_object_rev.unmapped_property1).is_none()
Example #19
0
    def test_for_dict(self):
        # given
        mapper = ObjectMapper.for_dict(TestClassSomeProperty1(None))

        # when
        mapped_object = mapper.map(TestClassSomeProperty1(
            some_property="some_value",
            some_property_02="some_value_02",
            some_property_03="some_value_03"))

        # then
        assert_that(mapped_object).is_instance_of(dict)
        assert_that(mapped_object['some_property']).is_equal_to("some_value")
        assert_that(mapped_object['some_property_02']).is_equal_to("some_value_02")
        assert_that(mapped_object['some_property_03']).is_equal_to("some_value_03")

        # when
        mapped_object_rev = mapper.map(dict(
            some_property="some_value",
            some_property_02="some_value_02",
            some_property_03="some_value_03"))

        # then
        assert_that(mapped_object_rev).is_instance_of(TestClassSomeProperty1)
        assert_that(mapped_object_rev.some_property).is_equal_to("some_value")
        assert_that(mapped_object_rev.some_property_02).is_equal_to("some_value_02")
        assert_that(mapped_object_rev.some_property_03).is_equal_to("some_value_03")
        assert_that(mapped_object_rev.unmapped_property1).is_none()
Example #20
0
    def test_map_attr_name_for_implicit_mapping(self):
        # given
        mapper = ObjectMapper.from_class(TestClassSomePropertyEmptyInit1,
                                         TestClassSomePropertyEmptyInit2)

        # then
        assert_that(
            mapper.map_attr_name("some_property")).is_equal_to("some_property")
        assert_that(mapper.map_attr_name("some_property_02")).is_equal_to(
            "some_property_02")
        assert_that(mapper.map_attr_name("some_property_03")).is_equal_to(
            "some_property_03")

        # when
        with self.assertRaises(ValueError) as context:
            mapper.map_attr_name("unmapped_property1")

        # then
        assert_that(context.exception.args[0]).contains("unmapped_property1")

        # when
        with self.assertRaises(ValueError) as context:
            mapper.map_attr_name("unmapped_property2")

        # then
        assert_that(context.exception.args[0]).contains("unmapped_property2")
Example #21
0
    def test_map_attr_value_rev(self):
        # when
        mapped_value = ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassMappedPropertyEmptyInit).\
            custom_mappings({"some_property_02": "mapped_property_02"}).\
            map_attr_value("mapped_property_02", "some_value", MappingDirection.right_to_left)

        # then
        assert_that(mapped_value).is_equal_to("some_value")
Example #22
0
    def test_map_attr_value_for_target_class_rev(self):
        # when
        mapped_value = ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassMappedPropertyEmptyInit).\
            custom_mappings({"some_property_02": "mapped_property_02"}).\
            map_attr_value("mapped_property_02", "some_value", target_class=TestClassSomePropertyEmptyInit1)

        # then
        assert_that(mapped_value).is_equal_to("some_value")
Example #23
0
    def test_map_attr_name_for_empty_classes_should_raise_exception(self):
        # given
        mapper = ObjectMapper.from_class(TestEmptyClass1, TestEmptyClass1)

        with self.assertRaises(ValueError) as context:
            # when
            mapper.map_attr_name("unmapped_property")

        # then
        assert_that(context.exception.args[0]).contains("unmapped_property")
Example #24
0
    def test_map_with_option_fail_on_get_attr(self):
        # given
        mapper = ObjectMapper.from_class(
            TestClassSomeProperty1, TestEmptyClass1).custom_mappings({
                "some_property":
                "non_existing_property"
            }).options(MapperOptions.fail_on_get_attr == False)

        # when
        mapped_object = mapper.map(TestEmptyClass1())

        # then
        assert_that(mapped_object).is_instance_of(TestClassSomeProperty1)
        assert_that(mapped_object.some_property).is_none()

        # given
        mapper_rev = ObjectMapper.from_class(
            TestEmptyClass1, TestClassSomeProperty1).custom_mappings({
                "non_existing_property":
                "some_property"
            }).options(MapperOptions.fail_on_get_attr == False)

        # when
        mapped_object_rev = mapper_rev.map(TestEmptyClass1())

        # then
        assert_that(mapped_object_rev).is_instance_of(TestClassSomeProperty1)
        assert_that(mapped_object_rev.some_property).is_none()

        # given
        mapper_strict = ObjectMapper.from_class(
            TestClassSomeProperty1, TestEmptyClass1).custom_mappings({
                "some_property":
                "non_existing_property"
            }).options(MapperOptions.fail_on_get_attr == True)

        try:
            # when
            mapper_strict.map(TestEmptyClass1())
            self.fail("Should raise AttributeError")
        except AttributeError as er:
            assert_that(er.args[0]).contains("non_existing_property")
    def test_other_to_enum_mapping_should_use_default_mapping(self):
        # given
        mapper = ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1(),
                                             TestClassSomePropertyEmptyInit2(some_property=SomeEnum.some_enum_02))

        # when
        mapped_object = mapper.map(TestClassSomePropertyEmptyInit1(some_property=TestEmptyClass1()))

        # then
        assert_that(mapped_object).is_type_of(TestClassSomePropertyEmptyInit2)
        assert_that(mapped_object.some_property).is_type_of(TestEmptyClass1)
    def test_int_to_unknown_mapping_should_map_to_int(self):
        # given
        mapper = ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1(), TestClassSomePropertyEmptyInit2())

        # when
        mapped_object = mapper.map(TestClassSomePropertyEmptyInit1(some_property=1))

        # then
        assert_that(mapped_object).is_type_of(TestClassSomePropertyEmptyInit2)
        assert_that(mapped_object.some_property).is_type_of(int)
        assert_that(mapped_object.some_property).is_equal_to(1)
    def test_enum_to_unknown_mapping_should_map_to_enum(self):
        # given
        mapper = ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1(), TestClassSomePropertyEmptyInit2())

        # when
        mapped_object = mapper.map(TestClassSomePropertyEmptyInit1(some_property=SomeEnum.some_enum_02))

        # then
        assert_that(mapped_object).is_type_of(TestClassSomePropertyEmptyInit2)
        self.assertTrue(type(mapped_object.some_property) == SomeEnum)
        assert_that(mapped_object.some_property).is_equal_to(SomeEnum.some_enum_02)
Example #28
0
    def test_nested_mapper_when_the_same_mapper_added_should_raise_exception(
            self):
        # given
        root_mapper = ObjectMapper.from_class(TestClassSomeProperty1,
                                              TestClassSomeProperty2)
        root_mapper.nested_mapper(
            ObjectMapper.from_class(TestClassSomePropertyEmptyInit1,
                                    TestClassSomePropertyEmptyInit2))

        # when
        with self.assertRaises(ConfigurationException) as context:
            root_mapper.nested_mapper(
                ObjectMapper.from_class(TestClassSomePropertyEmptyInit1,
                                        TestClassSomePropertyEmptyInit2))

        # then
        assert_that(context.exception.args[0]).contains(
            TestClassSomePropertyEmptyInit1.__name__)
        assert_that(context.exception.args[0]).contains(
            TestClassSomePropertyEmptyInit2.__name__)
    def test_enum_to_string_mapping_should_map_to_enum_name(self):
        # given
        mapper = ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1(),
                                             TestClassSomePropertyEmptyInit2(some_property=''))

        # when
        mapped_object = mapper.map(TestClassSomePropertyEmptyInit1(some_property=SomeEnum.some_enum_02))

        # then
        assert_that(mapped_object).is_type_of(TestClassSomePropertyEmptyInit2)
        assert_that(mapped_object.some_property).is_type_of(str)
        assert_that(mapped_object.some_property).is_equal_to(SomeEnum.some_enum_02.name)
    def test_map_unicode_string_to_enum(self):
        # given
        mapper = ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1(),
                                             TestClassSomePropertyEmptyInit2(some_property=SomeEnum.some_enum_02))

        # when
        mapped_object = mapper.map(TestClassSomePropertyEmptyInit1(some_property=str('some_enum_01')))

        # then
        assert_that(mapped_object).is_type_of(TestClassSomePropertyEmptyInit2)
        self.assertTrue(type(mapped_object.some_property) == SomeEnum)
        assert_that(mapped_object.some_property).is_equal_to(SomeEnum.some_enum_01)