-
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.
Added better description for using mapper with Pydancit and TortoiseO…
…RM, added better type casting, simplified test cases
- Loading branch information
1 parent
765937f
commit 04cd8c7
Showing
7 changed files
with
146 additions
and
44 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "py-automapper" | ||
version = "1.0.3" | ||
version = "1.0.4" | ||
description = "Library for automatically mapping one object to another" | ||
authors = ["Andrii Nikolaienko <[email protected]>"] | ||
license = "MIT" | ||
|
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 |
---|---|---|
@@ -1,40 +1,53 @@ | ||
from dataclasses import dataclass | ||
from typing import List | ||
from unittest import TestCase | ||
|
||
import pytest | ||
from pydantic import BaseModel | ||
|
||
from automapper import mapper as global_mapper, Mapper, MappingError | ||
from automapper import mapper as default_mapper, Mapper, MappingError | ||
|
||
|
||
@dataclass | ||
class SourceClass: | ||
class UserInfo(BaseModel): | ||
id: int | ||
name: str | ||
full_name: str | ||
public_name: str | ||
hobbies: List[str] | ||
|
||
|
||
class TargetModel(BaseModel): | ||
class PublicUserInfo(BaseModel): | ||
id: int | ||
name: str | ||
public_name: str | ||
hobbies: List[str] | ||
|
||
|
||
class PydanticExtensionTest(TestCase): | ||
"""These scenario are known for ORM systems. | ||
e.g. Model classes in Tortoise ORM | ||
""" | ||
"""These scenario are known for FastAPI Framework models and Pydantic models in general.""" | ||
|
||
def setUp(self) -> None: | ||
self.mapper = Mapper() | ||
|
||
def test_map__fails_for_tortoise_mapping(self): | ||
obj = SourceClass(15, "This is a test text") | ||
def test_map__fails_for_pydantic_mapping(self): | ||
obj = UserInfo( | ||
id=2, | ||
full_name="Danny DeVito", | ||
public_name="dannyd", | ||
hobbies=["acting", "comedy", "swimming"], | ||
) | ||
with pytest.raises(MappingError): | ||
self.mapper.to(TargetModel).map(obj) | ||
|
||
def test_map__global_mapper_works_with_provided_tortoise_extension(self): | ||
obj = SourceClass(17, "Test obj name") | ||
|
||
result = global_mapper.to(TargetModel).map(obj) | ||
|
||
assert result.id == 17 | ||
assert result.name == "Test obj name" | ||
self.mapper.to(PublicUserInfo).map(obj) | ||
|
||
def test_map__global_mapper_works_with_provided_pydantic_extension(self): | ||
obj = UserInfo( | ||
id=2, | ||
full_name="Danny DeVito", | ||
public_name="dannyd", | ||
hobbies=["acting", "comedy", "swimming"], | ||
) | ||
|
||
result = default_mapper.to(PublicUserInfo).map(obj) | ||
|
||
assert result.id == 2 | ||
assert result.public_name == "dannyd" | ||
assert set(result.hobbies) == set(["acting", "comedy", "swimming"]) | ||
with pytest.raises(AttributeError): | ||
getattr(result, "full_name") |
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