Ejemplo n.º 1
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)
Ejemplo n.º 2
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__)
Ejemplo n.º 3
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")
Ejemplo n.º 4
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")
Ejemplo n.º 5
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")
Ejemplo n.º 7
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")
Ejemplo n.º 8
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")
Ejemplo n.º 9
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")
Ejemplo n.º 10
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")
Ejemplo n.º 11
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')
Ejemplo n.º 12
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")
Ejemplo n.º 13
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")
Ejemplo n.º 14
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")
Ejemplo n.º 15
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")
Ejemplo n.º 16
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")
Ejemplo n.º 17
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__)
Ejemplo n.º 18
0
    def test_nested_mapper_when_wrong_param_type_should_raise_exception(self):
        # given
        root_mapper = ObjectMapper.from_class(TestClassSomeProperty1,
                                              TestClassSomeProperty2)

        # when
        with self.assertRaises(ValueError) as context:
            root_mapper.nested_mapper(object())

        # then
        assert_that(context.exception.args[0]).contains(ObjectMapper.__name__)
        assert_that(context.exception.args[0]).contains("object")
Ejemplo n.º 19
0
    def test_map_attr_name_for_explicit_mapping(self):
        # given
        mapper = ObjectMapper.from_class(
            TestClassSomeProperty1, TestClassMappedProperty).custom_mappings(
                {"some_property": "mapped_property"})

        # then
        assert_that(mapper.map_attr_name("some_property")).is_equal_to(
            "mapped_property")

        # then
        assert_that(mapper.map_attr_name("mapped_property")).is_equal_to(
            "some_property")
    def test_object_mapper_map_with_value_converter_and_default_mapping(self):
        # given
        mapper = ObjectMapper.from_class(
            TestClassSomePropertyEmptyInit1,
            TestClassSomePropertyEmptyInit2).value_converters(
                {"some_property_02": (lambda val: 7, lambda val: 9)})

        # then
        assert_that(mapper.map(TestClassSomePropertyEmptyInit1(some_property_02="some_val")).some_property_02).\
            is_equal_to(7)

        assert_that(mapper.map(TestClassSomePropertyEmptyInit2(some_property_02="some_other_val")).some_property_02). \
            is_equal_to(9)
Ejemplo n.º 21
0
    def test_map_attr_name_for_unmapped_explicit_property_should_raise_exception(
            self):
        # given
        mapper = ObjectMapper.from_class(
            TestClassSomeProperty1, TestClassMappedProperty).custom_mappings(
                {"some_property": "mapped_property"})

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

        # then
        assert_that(context.exception.args[0]).contains("unmapped_property")
Ejemplo n.º 22
0
    def test_map_one_explicit_property_rev(self):
        # given
        mapper = ObjectMapper.from_class(dict, TestClassSomeProperty1).custom_mappings(
            {"mapped_property": "some_property"})

        # when
        mapped_object = mapper.map(TestClassSomeProperty1(some_property="some_value"))

        # then
        assert_that(mapped_object).is_instance_of(dict)
        assert_that(mapped_object['mapped_property']).is_equal_to("some_value")

        # when
        mapped_object_rev = mapper.map(dict(mapped_property="other_value"))

        # then
        assert_that(mapped_object_rev).is_instance_of(TestClassSomeProperty1)
        assert_that(mapped_object_rev.some_property).is_equal_to("other_value")
Ejemplo n.º 23
0
    def test_map_override_implicit_mapping(self):
        # given
        mapper = ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassSomePropertyEmptyInit2).\
            custom_mappings(
                            {"some_property_02": "some_property_03",
                             "some_property_03": "some_property_02"})

        # when
        mapped_object = mapper.map(
            TestClassSomePropertyEmptyInit1(
                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(
            TestClassSomePropertyEmptyInit2)
        assert_that(mapped_object.some_property).is_equal_to("some_value")
        assert_that(
            mapped_object.some_property_02).is_equal_to("some_value_03")
        assert_that(
            mapped_object.some_property_03).is_equal_to("some_value_02")
        assert_that(mapped_object.unmapped_property2).is_none()

        # when
        mapped_object_rev = mapper.map(
            TestClassSomePropertyEmptyInit2(
                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(
            TestClassSomePropertyEmptyInit1)
        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_03")
        assert_that(
            mapped_object_rev.some_property_03).is_equal_to("some_value_02")
        assert_that(mapped_object_rev.unmapped_property1).is_none()
    def test_object_mapper_map_attr_value(self):
        # given
        mapper = ObjectMapper.from_class(
            TestClassSomePropertyEmptyInit1,
            TestClassMappedPropertyEmptyInit).custom_mappings({
                "some_property_02":
                "mapped_property_02"
            }).value_converters(
                {"some_property_02": (lambda val: 7, lambda val: 9)})

        # when
        assert_that(
            mapper.map_attr_value(
                "some_property_02",
                "val",
                target_class=TestClassMappedPropertyEmptyInit)).is_equal_to(7)
        assert_that(
            mapper.map_attr_value(
                "mapped_property_02",
                "some_value",
                target_class=TestClassSomePropertyEmptyInit1)).is_equal_to(9)
Ejemplo n.º 25
0
    def test_map_with_custom_right_initializers(self):
        # given
        mapper = ObjectMapper.from_class(TestClassSomePropertyEmptyInit1, TestClassMappedPropertyEmptyInit).\
            right_initializers(
            {
                "mapped_property": lambda obj: obj.some_property + obj.some_property_02,
                "unmapped_property2": lambda obj: "prefix_{}".format(obj.unmapped_property1)})

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

        assert_that(mapped_object).is_instance_of(
            TestClassMappedPropertyEmptyInit)
        assert_that(mapped_object.mapped_property).is_equal_to(
            "some_valuesome_value_02")
        assert_that(mapped_object.unmapped_property2).is_equal_to(
            "prefix_unmapped_value")
Ejemplo n.º 26
0
    def test_map_with_reversed_nested_mapper_should_not_use_nested_mapper(
            self):
        # given
        root_mapper = ObjectMapper.from_prototype(
            TestClassSomeProperty1(TestClassSomePropertyEmptyInit1()),
            TestClassSomeProperty2(TestClassSomePropertyEmptyInit2()))

        root_mapper.nested_mapper(
            ObjectMapper.from_class(TestClassSomePropertyEmptyInit2,
                                    TestClassSomePropertyEmptyInit1))

        # when
        mapped_object = root_mapper.map(
            TestClassSomeProperty1(
                some_property=TestClassSomePropertyEmptyInit1(
                    some_property="nested_value")))

        # then
        assert_that(mapped_object).is_instance_of(TestClassSomeProperty2)
        assert_that(mapped_object.some_property).is_instance_of(
            TestClassSomePropertyEmptyInit1)
        assert_that(mapped_object.some_property.some_property).is_equal_to(
            "nested_value")
Ejemplo n.º 27
0
 def test_map_empty_to_empty_dict(self):
     assert_that(ObjectMapper.from_class(TestEmptyClass1, dict).map(TestEmptyClass1())).is_instance_of(dict)
     assert_that(ObjectMapper.from_class(TestEmptyClass1, dict).map({})).is_instance_of(TestEmptyClass1)
Ejemplo n.º 28
0
    def test_map_with_nested_explicit_mapper(self):
        # given
        root_mapper = ObjectMapper.from_prototype(TestClassSomeProperty1(None),
                                                  TestClassSomeProperty2(None))

        root_mapper.nested_mapper(
            ObjectMapper.from_class(TestClassSomePropertyEmptyInit1,
                                    TestClassSomePropertyEmptyInit2))

        # 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(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()

        # when
        mapped_object_rev = root_mapper.map(
            TestClassSomeProperty2(
                some_property="some_value",
                some_property_02=TestClassSomePropertyEmptyInit2(
                    some_property="nested_value",
                    some_property_02="nested_value_02",
                    some_property_03="nested_value_03",
                    unmapped_property2="unmapped_nested_value"),
                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_03).is_equal_to("some_value_03")
        assert_that(mapped_object_rev.unmapped_property1).is_none()

        nested_mapped_obj_rev = mapped_object_rev.some_property_02
        assert_that(nested_mapped_obj_rev).is_instance_of(
            TestClassSomePropertyEmptyInit1)
        assert_that(
            nested_mapped_obj_rev.some_property).is_equal_to("nested_value")
        assert_that(nested_mapped_obj_rev.some_property_02).is_equal_to(
            "nested_value_02")
        assert_that(nested_mapped_obj_rev.some_property_03).is_equal_to(
            "nested_value_03")
        assert_that(nested_mapped_obj_rev.unmapped_property1).is_none()
Ejemplo n.º 29
0
 def test_map_empty_to_empty(self):
     assert_that(ObjectMapper.from_class(TestEmptyClass1, TestEmptyClass2).map(TestEmptyClass1())).\
         is_instance_of(TestEmptyClass2)
     assert_that(ObjectMapper.from_class(TestEmptyClass1, TestEmptyClass2).map(TestEmptyClass2())).\
         is_instance_of(TestEmptyClass1)