-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathtest_example_10.py
More file actions
49 lines (32 loc) · 1.57 KB
/
Copy pathtest_example_10.py
File metadata and controls
49 lines (32 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from dataclasses import dataclass
from polyfactory.decorators import post_generated
from polyfactory.factories import DataclassFactory
from polyfactory.fields import Param
@dataclass
class Person:
name: str
age_next_year: int
class PersonFactoryWithParamValueSpecifiedInFactory(DataclassFactory[Person]):
"""In this factory, the next_years_age_from_calculator must be passed at build time."""
next_years_age_from_calculator = Param[int](lambda age: age + 1, is_callable=True, age=20)
@post_generated
@classmethod
def age_next_year(cls, next_years_age_from_calculator: int) -> int:
return next_years_age_from_calculator
def test_factory__in_factory() -> None:
person = PersonFactoryWithParamValueSpecifiedInFactory.build()
assert isinstance(person, Person)
assert not hasattr(person, "next_years_age_from_calculator")
assert person.age_next_year == 21
class PersonFactoryWithParamValueSetAtBuild(DataclassFactory[Person]):
"""In this factory, the next_years_age_from_calculator must be passed at build time."""
next_years_age_from_calculator = Param[int](is_callable=True, age=20)
@post_generated
@classmethod
def age_next_year(cls, next_years_age_from_calculator: int) -> int:
return next_years_age_from_calculator
def test_factory__build_time() -> None:
person = PersonFactoryWithParamValueSpecifiedInFactory.build(next_years_age_from_calculator=lambda age: age + 1)
assert isinstance(person, Person)
assert not hasattr(person, "next_years_age_from_calculator")
assert person.age_next_year == 21