11import pytest
2- from automapper import mapper
3- from automapper .path_mapper import ( # Replace 'your_module' with the actual module name
4- MapPath ,
5- )
2+ from automapper import MapPath , mapper
3+ from automapper .exceptions import MapPathMissMatchError
64
75
86class BasicUser :
@@ -28,8 +26,10 @@ def test_valid_map_path(self):
2826 """Test that MapPath correctly splits a valid path."""
2927 path = MapPath ("some.example.path" )
3028 assert path .path == "some.example.path"
31- assert path .attributes == ["example" , "path" ] # obj_prefix is excluded
32- assert path .obj_prefix == "some" # obj_prefix is correctly assigned
29+ assert path .attributes == ["some" , "example" , "path" ]
30+ assert (
31+ path .obj_prefix is None
32+ ) # this is set by automatic in the process of mapping.
3333
3434 def test_invalid_map_path_missing_dot (self ):
3535 """Test that MapPath raises ValueError for paths without a dot."""
@@ -41,25 +41,23 @@ def test_invalid_map_path_missing_dot(self):
4141 def test_callable_behavior (self ):
4242 """Test that calling an instance returns the correct split attributes."""
4343 path = MapPath ("one.two.three" )
44- assert path () == ["two" , "three" ]
44+ assert path () == ["one" , " two" , "three" ]
4545
4646 def test_repr (self ):
4747 """Test that __repr__ returns the expected string representation."""
4848 path = MapPath ("foo.bar" )
49- assert (
50- repr (path ) == "MapPath(['bar'])"
51- ) # Only attributes are shown, excluding obj_prefix
49+ assert repr (path ) == "MapPath(['foo', 'bar'])"
5250
5351
54- class TestMappingObjectAttributes :
52+ class TestAddMappingWithNestedObjectReference :
5553 def test_use_registered_mapping_with_map_path (self ):
5654 try :
5755 mapper .add (
5856 AdvancedUser ,
5957 BasicUser ,
6058 fields_mapping = {
61- "name" : MapPath ("AdvancedUser. user.name" ),
62- "city" : MapPath ("AdvancedUser. user.city" ),
59+ "name" : MapPath ("user.name" ),
60+ "city" : MapPath ("user.city" ),
6361 },
6462 )
6563
@@ -83,3 +81,15 @@ def test_map_object_directly_without_adding_map_path_cant_be_resolved(self):
8381 mapper .to (BasicUser ).map (advanced_user )
8482 finally :
8583 mapper ._mappings .clear ()
84+
85+ def test_cant_add_mapping_with_mixed_map_path_and_string_mapping (self ):
86+ """Cant mix MapPath for one field and another field be classic string mapping."""
87+ with pytest .raises (MapPathMissMatchError ):
88+ mapper .add (
89+ AdvancedUser ,
90+ BasicUser ,
91+ fields_mapping = {
92+ "name" : "AdvancedUser.user.name" ,
93+ "city" : MapPath ("AdvancedUser.user.city" ),
94+ },
95+ )
0 commit comments