-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed issue with dictionary collection
- Loading branch information
1 parent
fe732b9
commit 625ac9d
Showing
4 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |