Skip to content

Commit 8794de5

Browse files
authored
Merge pull request #13 from ezzy1337/hacktoberfest/add-unit-tests
Hacktoberfest/add unit tests
2 parents 511a98b + 5a388cf commit 8794de5

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ target/
5757

5858
# Idea
5959
.idea
60+
.python-version

mapper/object_mapper.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def create_map(self, type_from, type_to, mapping=None):
107107
self.mappings[key_from] = {}
108108
self.mappings[key_from][key_to] = mapping
109109

110-
def map(self, from_obj, to_type, ignore_case=False, allow_none=False):
110+
def map(self, from_obj, to_type, ignore_case=False, allow_none=False, excluded=None):
111111
"""Method for creating target object instance
112112
113113
:param from_obj: source object to be mapped from
@@ -127,10 +127,15 @@ def map(self, from_obj, to_type, ignore_case=False, allow_none=False):
127127
key_from = from_obj.__class__.__name__
128128
key_to = to_type.__name__
129129

130-
def not_private(s): return not s.startswith('_')
130+
def not_private(s):
131+
return not s.startswith('_')
132+
133+
def not_excluded(s):
134+
return not (excluded and s in excluded)
131135

132136
from_obj_attributes = getmembers(from_obj, lambda a: not isroutine(a))
133-
from_obj_dict = {k: v for k, v in from_obj_attributes if not_private(k)}
137+
from_obj_dict = {k: v for k, v in from_obj_attributes
138+
if not_private(k) and not_excluded(k)}
134139

135140
to_obj_attributes = getmembers(inst, lambda a: not isroutine(a))
136141
to_obj_dict = {k: v for k, v in to_obj_attributes if not_private(k)}

tests/object_mapper_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,21 @@ def __setattr__(self, name, value):
300300
self.assertTrue(isinstance(result, ToCustomDirClass), "Target types must be same")
301301
self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
302302
self.assertEqual(result.date, from_class.date, "Date mapping must be equal")
303-
self.assertNotIn("surname", dir(result), "To class must not contain surname")
303+
self.assertNotIn("surname", dir(result), "To class must not contain surname")
304+
305+
def test_mapping_excluded_field(self):
306+
"""Test mapping with excluded fields"""
307+
#Arrange
308+
from_class = FromTestClass()
309+
mapper = ObjectMapper()
310+
mapper.create_map(FromTestClass, ToTestClass)
311+
312+
#Act
313+
result = mapper.map(FromTestClass(), ToTestClass, excluded=['date'])
314+
315+
#Assert
316+
print(result)
317+
self.assertTrue(isinstance(result, ToTestClass), "Type must be ToTestClass")
318+
self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
319+
self.assertEqual(result.date, '', "Date mapping must be equal")
320+
self.assertNotIn("surname", result.__dict__, "To class must not contain surname")

0 commit comments

Comments
 (0)