Skip to content

Commit fa1a1ad

Browse files
committed
Fixing issue 25
1 parent 8a72f4c commit fa1a1ad

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

automapper/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collections
12
from enum import Enum
23
from typing import Any
34

@@ -6,7 +7,7 @@
67

78
def is_sequence(obj: Any) -> bool:
89
"""Check if object implements `__iter__` method"""
9-
return hasattr(obj, "__iter__")
10+
return isinstance(obj, collections.Sequence)
1011

1112

1213
def is_subscriptable(obj: Any) -> bool:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "py-automapper"
7-
version = "2.0.0"
7+
version = "2.1.0"
88
description = "Library for automatically mapping one object to another"
99
authors = [
1010
{name = "Andrii Nikolaienko", email = "[email protected]"}

tests/test_issue_25.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Optional
2+
3+
from automapper import mapper
4+
from pydantic import BaseModel
5+
6+
7+
class Address(BaseModel):
8+
street: Optional[str]
9+
number: Optional[int]
10+
zip_code: Optional[int]
11+
city: Optional[str]
12+
13+
14+
class PersonInfo(BaseModel):
15+
name: Optional[str] = None
16+
age: Optional[int] = None
17+
address: Optional[Address] = None
18+
19+
20+
class PublicPersonInfo(BaseModel):
21+
name: Optional[str] = None
22+
address: Optional[Address] = None
23+
24+
25+
def test_map__without_deepcopy_mapped_objects_should_be_the_same():
26+
address = Address(street="Main Street", number=1, zip_code=100001, city="Test City")
27+
info = PersonInfo(name="John Doe", age=35, address=address)
28+
29+
# default deepcopy behavior
30+
public_info = mapper.to(PublicPersonInfo).map(info)
31+
assert (
32+
public_info.address is not address
33+
), "Address mapped object is same as origin."
34+
35+
# disable deepcopy
36+
public_info = mapper.to(PublicPersonInfo).map(info, use_deepcopy=False)
37+
assert (
38+
public_info.address is info.address
39+
), "Address mapped object is not same as origin."

tests/test_try_get_field_values.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from dataclasses import dataclass
2+
3+
from automapper.mapper import _try_get_field_value
4+
5+
6+
@dataclass
7+
class TestClass:
8+
map_field: str
9+
10+
11+
def test_try_get_field_value__if_in_custom_mapping():
12+
is_found, mapped_value = _try_get_field_value("field1", None, {"field1": 123})
13+
14+
assert is_found
15+
assert mapped_value == 123
16+
17+
18+
def test_try_get_field_value__if_origin_has_same_field_attr():
19+
is_found, mapped_value = _try_get_field_value(
20+
"map_field", TestClass("Hello world"), None
21+
)
22+
23+
assert is_found
24+
assert mapped_value == "Hello world"
25+
26+
27+
def test_try_get_field_value__if_origin_contains_same_field_as_item():
28+
is_found, mapped_value = _try_get_field_value(
29+
"map_field", {"map_field": "Hello world. Again"}, None
30+
)
31+
32+
assert is_found
33+
assert mapped_value == "Hello world. Again"
34+
35+
36+
def test_try_get_field_value__if_field_not_found():
37+
is_found, mapped_value = _try_get_field_value("field1", None, None)
38+
39+
assert not is_found
40+
assert mapped_value is None

0 commit comments

Comments
 (0)