def test_map_with_custom_target_initializers_when_initializer_not_function_should_raise_exception( self): with self.assertRaises(ValueError) as context: OneWayMapper.for_target_prototype( TestClassSomePropertyEmptyInit1()).target_initializers( {"some_property_02": 7}) assert_that(context.exception.message).contains("some_property_02")
def test_map_with_multiple_nested_mappings_when_no_matching_mapper_for_target_type_should_raise_exception( self): # given root_mapper = OneWayMapper.for_target_prototype( TestClassSomeProperty2( some_property=TestClassMappedPropertyEmptyInit())) root_mapper.nested_mapper( OneWayMapper.for_target_prototype(TestClassSomeProperty2(None)), TestClassSomePropertyEmptyInit1) root_mapper.nested_mapper( OneWayMapper.for_target_prototype( TestClassSomePropertyEmptyInit2()), TestClassSomePropertyEmptyInit1) with self.assertRaises(ConfigurationException) as context: root_mapper.map( TestClassSomeProperty1( some_property=TestClassSomePropertyEmptyInit1( some_property_02="nested_value_02"))) # then assert_that(context.exception.message).contains("some_property") assert_that(context.exception.message).contains( "TestClassSomePropertyEmptyInit1") assert_that(context.exception.message).contains( "TestClassMappedPropertyEmptyInit")
def test_map_with_multiple_nested_mappings_for_one_attribute_when_target_type_known( self): # given root_mapper = OneWayMapper.for_target_prototype( TestClassSomeProperty2( some_property=TestClassSomePropertyEmptyInit2())) root_mapper.nested_mapper( OneWayMapper.for_target_prototype(TestClassSomeProperty2(None)), TestClassSomePropertyEmptyInit1) root_mapper.nested_mapper( OneWayMapper.for_target_prototype( TestClassSomePropertyEmptyInit2()), TestClassSomePropertyEmptyInit1) # when mapped_object = root_mapper.map( TestClassSomeProperty1( some_property=TestClassSomePropertyEmptyInit1( 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_for_target_prototype_when_class_passed_should_raise_exception(self): # when with self.assertRaises(ValueError) as context: OneWayMapper.for_target_prototype(object) # then assert_that(context.exception.message).contains("object")
def test_map_when_ambiguous_nested_mapping_should_raise_exception(self): # given root_mapper = OneWayMapper.for_target_prototype(TestClassSomeProperty2(None)) root_mapper.nested_mapper( OneWayMapper.for_target_prototype(TestClassSomePropertyEmptyInit2()), TestClassSomePropertyEmptyInit1) root_mapper.nested_mapper( OneWayMapper.for_target_prototype(TestClassSomeProperty2(None)), TestClassSomePropertyEmptyInit1) # when with self.assertRaises(ConfigurationException) as context: root_mapper.map(TestClassSomeProperty1(some_property=TestClassSomePropertyEmptyInit1())) # then assert_that(context.exception.message).contains("some_property") assert_that(context.exception.message).contains("TestClassSomePropertyEmptyInit1")
def test_map_with_reversed_nested_mapper_should_not_use_nested_mapper(self): # given root_mapper = OneWayMapper.for_target_prototype(TestClassSomeProperty2(TestClassSomePropertyEmptyInit2())) root_mapper.nested_mapper( OneWayMapper.for_target_prototype(TestClassSomePropertyEmptyInit1()), TestClassSomePropertyEmptyInit2) # 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_attr_value_with_enum_to_int_conversion(self): # given mapper = OneWayMapper.for_target_prototype( TestClassSomePropertyEmptyInit1(some_property_02=7)) # then assert_that(mapper.map_attr_value("some_property_02", SomeEnum.some_enum_01)).is_equal_to(1)
def test_map_with_nested_explicit_mapper(self): # given root_mapper = OneWayMapper.for_target_prototype(TestClassSomeProperty2(None)) root_mapper.nested_mapper( OneWayMapper.for_target_class(TestClassSomePropertyEmptyInit2), TestClassSomePropertyEmptyInit1) # 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()
def test_map_attr_value_with_datetime_to_string_conversion(self): # given mapper = OneWayMapper.for_target_prototype(TestClassSomePropertyEmptyInit1(some_property_02="")) # then assert_that(mapper.map_attr_value("some_property_02", datetime(2015, 11, 2, 18, 14, 42, 123))).is_equal_to( "2015-11-02T18:14:42.000123")
def test_map_from_none_attribute(self): # given mapper = OneWayMapper.for_target_prototype(TestClassSomePropertyEmptyInit2(some_property_02="3")) # when mapped_object = mapper.map(TestClassSomePropertyEmptyInit1(some_property_02=None)) # then assert_that(mapped_object).is_instance_of(TestClassSomePropertyEmptyInit2) assert_that(mapped_object.some_property_02).is_none()
def test_map_from_string_to_datetime_wrong_format_should_raise_exception(self): # given mapper = OneWayMapper.for_target_prototype(TestClassSomePropertyEmptyInit2(some_property_02=datetime.now())) # when with self.assertRaises(ValueError) as context: mapper.map(TestClassSomePropertyEmptyInit1(some_property_02="wrong_date_format")) # then assert_that(context.exception.args[0]).contains("wrong_date_format")
def test_map_string_with_millis_to_datetime(self): # given mapper = OneWayMapper.for_target_prototype(TestClassSomePropertyEmptyInit2(some_property=datetime.now())) # when mapped_object = mapper.map(TestClassSomePropertyEmptyInit1( some_property="2015-11-02T18:14:42.000123")) # then assert_that(mapped_object).is_instance_of(TestClassSomePropertyEmptyInit2) assert_that(mapped_object.some_property).is_instance_of(datetime) assert_that(mapped_object.some_property).is_equal_to(datetime(2015, 11, 2, 18, 14, 42, 123))
def test_map_from_datetime_to_string(self): # given mapper = OneWayMapper.for_target_prototype(TestClassSomePropertyEmptyInit2(some_property_02="string")) test_datetime = datetime.now() # when mapped_object = mapper.map(TestClassSomePropertyEmptyInit1( some_property="some_value", some_property_02=test_datetime)) # 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(test_datetime.isoformat())
def test_map_implicit_with_prototype_obj(self): # given mapper = OneWayMapper.for_target_prototype(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()
def test_for_target_prototype(self): assert_that(OneWayMapper.for_target_prototype(object())).is_not_none()