Skip to content

Commit

Permalink
fixed issue with dictionary collection
Browse files Browse the repository at this point in the history
  • Loading branch information
anikolaienko committed Jul 24, 2022
1 parent fe732b9 commit 625ac9d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
1.0.3 - 2022/07/24
* Fixed issue with dictionary collection: https://github.com/anikolaienko/py-automapper/issues/4

1.0.2 - 2022/07/24
* Bug fix: pass parameters override in MappingWrapper.map
* Added support for mapping fields with different names
* Added support for mapping fields with different names: https://github.com/anikolaienko/py-automapper/issues/3

1.0.1 - 2022/01/05
* Bug fix
Expand Down
2 changes: 1 addition & 1 deletion automapper/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def _map_subobject(
k: self._map_subobject(
v, _visited_stack, skip_none_values=skip_none_values
)
for k, v in obj
for k, v in obj.items()
}
else:
result = type(obj)( # type: ignore [call-arg]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "py-automapper"
version = "1.0.0"
version = "1.0.3"
description = "Library for automatically mapping one object to another"
authors = ["Andrii Nikolaienko <anikolaienko14@gmail.com>"]
license = "MIT"
Expand Down
47 changes: 47 additions & 0 deletions tests/test_automapper_dict_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from copy import deepcopy
from typing import Any, Dict

from automapper import mapper


class Candy:
def __init__(self, name: str, brand: str):
self.name = name
self.brand = brand


class Shop:
def __init__(self, products: Dict[str, Any], annual_income: int):
self.products: Dict[str, Any] = deepcopy(products)
self.annual_income = annual_income


class ShopPublicInfo:
def __init__(self, products: Dict[str, Any]):
self.products: Dict[str, Any] = deepcopy(products)


def test_map__with_dict_field():
products = {
"magazines": ["Forbes", "Time", "The New Yorker"],
"candies": [
Candy("Reese's cups", "The Hershey Company"),
Candy("Snickers", "Mars, Incorporated"),
],
}
shop = Shop(products=products, annual_income=10000000)

public_info = mapper.to(ShopPublicInfo).map(shop)

assert public_info.products["magazines"] == shop.products["magazines"]
assert id(public_info.products["magazines"]) != id(shop.products["magazines"])

assert public_info.products["candies"] != shop.products["candies"]
assert public_info.products["candies"][0] != shop.products["candies"][0]
assert public_info.products["candies"][1] != shop.products["candies"][1]

assert public_info.products["candies"][0].name == "Reese's cups"
assert public_info.products["candies"][0].brand == "The Hershey Company"

assert public_info.products["candies"][1].name == "Snickers"
assert public_info.products["candies"][1].brand == "Mars, Incorporated"

0 comments on commit 625ac9d

Please sign in to comment.