Skip to content

Commit 4fc2530

Browse files
authored
Merge pull request #17 from renanvieira/fix/dict_classes_names
Fix type name inside mapper dict to avoid collision
2 parents b02c6d6 + b033f29 commit 4fc2530

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

mapper/object_mapper.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
"""
33
Copyright (C) 2015, marazt. All rights reserved.
44
"""
5-
from mapper.object_mapper_exception import ObjectMapperException
6-
from mapper.casedict import CaseDict
75
from inspect import getmembers, isroutine
86

7+
from mapper.casedict import CaseDict
8+
from mapper.object_mapper_exception import ObjectMapperException
9+
10+
911
class ObjectMapper(object):
1012
"""
1113
Base class for mapping class attributes from one class to another one
@@ -91,16 +93,18 @@ def create_map(self, type_from, type_to, mapping=None):
9193
9294
:return: None
9395
"""
94-
key_from = type_from.__name__
95-
key_to = type_to.__name__
96+
key_from = type_from
97+
key_to = type_to
9698

9799
if mapping is None:
98100
mapping = {}
99101

100102
if key_from in self.mappings:
101103
inner_map = self.mappings[key_from]
102104
if key_to in inner_map:
103-
raise ObjectMapperException("Mapping for {0} -> {1} already exists".format(key_from, key_to))
105+
raise ObjectMapperException(
106+
"Mapping for {0}.{1} -> {2}.{3} already exists".format(key_from.__module__, key_from.__name__,
107+
key_to.__module__, key_to.__name__))
104108
else:
105109
inner_map[key_to] = mapping
106110
else:
@@ -125,8 +129,8 @@ def map(self, from_obj, to_type, ignore_case=False, allow_none=False, excluded=N
125129
from_obj.__dict__
126130

127131
inst = to_type()
128-
key_from = from_obj.__class__.__name__
129-
key_to = to_type.__name__
132+
key_from = from_obj.__class__
133+
key_to = to_type
130134

131135
def not_private(s):
132136
return not s.startswith('_')
@@ -169,6 +173,8 @@ def not_excluded(s):
169173
setattr(inst, prop, from_props[prop])
170174
# case when target attribute is not mapped (can be extended)
171175
else:
172-
raise ObjectMapperException("No mapping defined for {0} -> {1}".format(key_from, key_to))
176+
raise ObjectMapperException(
177+
"No mapping defined for {0}.{1} -> {2}.{3}".format(key_from.__module__, key_from.__name__,
178+
key_to.__module__, key_to.__name__))
173179

174180
return inst

tests/object_mapper_test.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
Copyright (C) 2015, marazt. All rights reserved.
44
"""
55
import unittest
6-
76
from datetime import datetime
7+
88
from mapper.object_mapper import ObjectMapper
99
from mapper.object_mapper_exception import ObjectMapperException
1010

11+
NO_MAPPING_FOUND_EXCEPTION_MESSAGE = "No mapping defined for {0}.{1} -> {2}.{3}"
12+
MAPPING_ALREADY_EXISTS_EXCEPTION_MESSAGE = "Mapping for {0}.{1} -> {2}.{3} already exists"
13+
1114

1215
class ToTestClass(object):
1316
""" To Test Class """
17+
1418
def __init__(self):
1519
self.name = ""
1620
self.date = ""
@@ -19,19 +23,22 @@ def __init__(self):
1923

2024
class ToTestClassTwo(object):
2125
""" To Test Class Two """
26+
2227
def __init__(self):
2328
self.all = ""
2429
pass
2530

2631

2732
class ToTestClassEmpty(object):
2833
""" To Test Class Empty """
34+
2935
def __init__(self):
3036
pass
3137

3238

3339
class FromTestClass(object):
3440
""" From Test Class """
41+
3542
def __init__(self):
3643
self.name = "Igor"
3744
self.surname = "Hnizdo"
@@ -102,7 +109,8 @@ def test_mapping_creation_duplicate_mapping(self):
102109

103110
# Arrange
104111
exc = False
105-
msg = "Mapping for FromTestClass -> ToTestClass already exists"
112+
msg = MAPPING_ALREADY_EXISTS_EXCEPTION_MESSAGE.format(FromTestClass.__module__, FromTestClass.__name__,
113+
ToTestClass.__module__, ToTestClass.__name__)
106114
mapper = ObjectMapper()
107115

108116
mapper.create_map(FromTestClass, ToTestClass)
@@ -186,9 +194,9 @@ def test_mapping_creation_no_mapping_defined(self):
186194

187195
# Arrange
188196
exc = False
189-
msg = "No mapping defined for FromTestClass -> ToTestClass"
190197
from_class = FromTestClass()
191-
198+
msg = NO_MAPPING_FOUND_EXCEPTION_MESSAGE.format(from_class.__module__, from_class.__class__.__name__,
199+
ToTestClass.__module__, ToTestClass.__name__)
192200
mapper = ObjectMapper()
193201

194202
# Act
@@ -225,11 +233,13 @@ def test_mapping_with_case_insensitivity(self):
225233
# Arrange
226234
class ToTestClass2(object):
227235
""" To Test Class 2 """
236+
228237
def __init__(self):
229238
self.name = ""
230239

231240
class FromTestClass2(object):
232241
""" From Test Class 2 """
242+
233243
def __init__(self):
234244
self.Name = "Name"
235245

@@ -267,6 +277,7 @@ def test_mapping_creation_with_custom_dir(self):
267277

268278
# Arrange
269279
_propNames = ['name', 'date']
280+
270281
class ToCustomDirClass(object):
271282
def __dir__(self):
272283
props = list(self.__dict__.keys())
@@ -304,16 +315,16 @@ def __setattr__(self, name, value):
304315

305316
def test_mapping_excluded_field(self):
306317
"""Test mapping with excluded fields"""
307-
#Arrange
318+
# Arrange
308319
from_class = FromTestClass()
309320
mapper = ObjectMapper()
310321
mapper.create_map(FromTestClass, ToTestClass)
311322

312-
#Act
323+
# Act
313324
result = mapper.map(FromTestClass(), ToTestClass, excluded=['date'])
314325

315-
#Assert
316-
print(result)
326+
# Assert
327+
print(result)
317328
self.assertTrue(isinstance(result, ToTestClass), "Type must be ToTestClass")
318329
self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
319330
self.assertEqual(result.date, '', "Date mapping must be equal")

0 commit comments

Comments
 (0)