-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement auto registration hook (#125)
* Implement auto registration hook * * Remove TypeDict * Use const value instead of getting class variable. * Fix Black errors * fix pylint * Lint + fix lint errors * add typedict and dataclass test cases Co-authored-by: Avihai <ayosef@paypal.com>
- Loading branch information
1 parent
a0151e0
commit 82464cc
Showing
2 changed files
with
137 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from dataclasses import dataclass as vanilla_dataclass | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
from typing_extensions import TypedDict | ||
|
||
from pydantic_factories import ModelFactory | ||
|
||
|
||
class A(BaseModel): | ||
a_text: str | ||
|
||
|
||
class B(BaseModel): | ||
b_text: str | ||
a: A | ||
|
||
|
||
class C(BaseModel): | ||
b: B | ||
b_list: List[B] | ||
|
||
|
||
def test_auto_register_model_factory() -> None: | ||
class AFactory(ModelFactory): | ||
a_text = "const value" | ||
__model__ = A | ||
|
||
class BFactory(ModelFactory): | ||
b_text = "const value" | ||
__model__ = B | ||
__auto_register__ = True | ||
|
||
class CFactory(ModelFactory): | ||
__model__ = C | ||
|
||
c = CFactory.build() | ||
|
||
assert c.b.b_text == BFactory.b_text | ||
assert c.b_list[0].b_text == BFactory.b_text | ||
assert c.b.a.a_text != AFactory.a_text | ||
|
||
|
||
def test_auto_register_model_factory_using_create_factory() -> None: | ||
const_value = "const value" | ||
ModelFactory.create_factory(model=A, a_text=const_value) | ||
ModelFactory.create_factory(model=B, b_text=const_value, __auto_register__=True) | ||
CFactory = ModelFactory.create_factory(model=C) | ||
|
||
c = CFactory.build() | ||
|
||
assert c.b.b_text == const_value | ||
assert c.b_list[0].b_text == const_value | ||
assert c.b.a.a_text != const_value | ||
|
||
|
||
def test_dataclass_model_factory_auto_registration() -> None: | ||
@vanilla_dataclass | ||
class DataClass: | ||
text: str | ||
|
||
class UpperModel(BaseModel): | ||
nested_field: DataClass | ||
nested_list_field: List[DataClass] | ||
|
||
class UpperModelFactory(ModelFactory): | ||
__model__ = UpperModel | ||
|
||
class DataClassFactory(ModelFactory): | ||
text = "const value" | ||
__model__ = DataClass | ||
__auto_register__ = True | ||
|
||
upper = UpperModelFactory.build() | ||
|
||
assert upper.nested_field.text == DataClassFactory.text | ||
assert upper.nested_list_field[0].text == DataClassFactory.text | ||
|
||
|
||
def test_typeddict_model_factory_auto_registration() -> None: | ||
class TypedDictModel(TypedDict): | ||
text: str | ||
|
||
class UpperSchema(BaseModel): | ||
nested_field: TypedDictModel | ||
nested_list_field: List[TypedDictModel] | ||
|
||
class UpperModelFactory(ModelFactory): | ||
__model__ = UpperSchema | ||
|
||
class TypedDictFactory(ModelFactory): | ||
text = "const value" | ||
__model__ = TypedDictModel | ||
__auto_register__ = True | ||
|
||
upper = UpperModelFactory.build() | ||
|
||
assert upper.nested_field["text"] == TypedDictFactory.text | ||
assert upper.nested_list_field[0]["text"] == TypedDictFactory.text |