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()
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")
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')
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")
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")
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())
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()
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)
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)
def test_map_from_unicode_string(self): # given mapper = ObjectMapper.from_prototype(TestClassSomePropertyEmptyInit1(), TestClassSomePropertyEmptyInit2(some_property_02=datetime.now())) test_datetime = datetime.now() # when mapped_object = mapper.map(TestClassSomePropertyEmptyInit1( some_property="some_value", some_property_02=unicode(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)
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")
def test_map_implicit_with_prototype_obj(self): # given mapper = ObjectMapper.from_prototype(TestClassSomeProperty1(None), TestClassSomeProperty2(None)) # when mapped_object = mapper.map( TestClassSomeProperty1(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(TestClassSomeProperty2) 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") assert_that(mapped_object.unmapped_property2).is_none() # when mapped_object_rev = mapper.map( TestClassSomeProperty2(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()
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()