Skip to content

Commit 1ae2d52

Browse files
authored
test: refactor the tests to move tests to correct places (#495)
1 parent 9e34fa7 commit 1ae2d52

12 files changed

+849
-717
lines changed

tests/test_annotated_fields.py

Lines changed: 0 additions & 90 deletions
This file was deleted.

tests/test_attrs_factory.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime as dt
22
from decimal import Decimal
33
from enum import Enum
4-
from typing import Any, Dict, Generic, List, Tuple, TypeVar
4+
from typing import Any, Dict, Generic, List, Optional, Tuple, TypeVar, Union
55
from uuid import UUID
66

77
import attrs
@@ -219,3 +219,50 @@ class FooFactory(AttrsFactory[Foo]):
219219
foo = FooFactory.build()
220220

221221
assert foo.default_field == 10
222+
223+
224+
def test_union_types() -> None:
225+
@define
226+
class A:
227+
a: Union[List[str], List[int]]
228+
b: Union[str, List[int]]
229+
c: List[Union[Tuple[int, int], Tuple[str, int]]]
230+
231+
AFactory = AttrsFactory.create_factory(A)
232+
233+
assert AFactory.build()
234+
235+
236+
def test_collection_unions_with_models() -> None:
237+
@define
238+
class A:
239+
a: int
240+
241+
@define
242+
class B:
243+
a: str
244+
245+
@define
246+
class C:
247+
a: Union[List[A], List[B]]
248+
b: List[Union[A, B]]
249+
250+
CFactory = AttrsFactory.create_factory(C)
251+
252+
assert CFactory.build()
253+
254+
255+
@pytest.mark.parametrize("allow_none", (True, False))
256+
def test_optional_type(allow_none: bool) -> None:
257+
@define
258+
class A:
259+
a: Union[str, None]
260+
b: Optional[str]
261+
c: Optional[Union[str, int, List[int]]]
262+
263+
class AFactory(AttrsFactory[A]):
264+
__model__ = A
265+
266+
__allow_none_optionals__ = allow_none
267+
268+
assert AFactory.build()
File renamed without changes.

tests/test_build.py

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from uuid import uuid4
22

3-
import pytest
4-
5-
from pydantic import VERSION, BaseModel, Field, ValidationError
3+
from pydantic import BaseModel
64

75
from polyfactory.factories.pydantic_factory import ModelFactory
86
from tests.models import PersonFactoryWithDefaults, Pet, PetFactory
@@ -62,61 +60,6 @@ def test_builds_batch() -> None:
6260
assert isinstance(result.age, float)
6361

6462

65-
def test_factory_use_construct() -> None:
66-
# factory should pass values without validation
67-
invalid_age = "non_valid_age"
68-
non_validated_pet = PetFactory.build(factory_use_construct=True, age=invalid_age)
69-
assert non_validated_pet.age == invalid_age
70-
71-
with pytest.raises(ValidationError):
72-
PetFactory.build(age=invalid_age)
73-
74-
with pytest.raises(ValidationError):
75-
PetFactory.build(age=invalid_age)
76-
77-
78-
@pytest.mark.skipif(VERSION.startswith("2"), reason="pydantic 1 only test")
79-
def test_build_instance_by_field_alias_with_allow_population_by_field_name_flag_pydantic_v1() -> None:
80-
class MyModel(BaseModel):
81-
aliased_field: str = Field(..., alias="special_field")
82-
83-
class Config:
84-
allow_population_by_field_name = True
85-
86-
class MyFactory(ModelFactory):
87-
__model__ = MyModel
88-
89-
instance = MyFactory.build(aliased_field="some")
90-
assert instance.aliased_field == "some"
91-
92-
93-
@pytest.mark.skipif(VERSION.startswith("1"), reason="pydantic 2 only test")
94-
def test_build_instance_by_field_alias_with_populate_by_name_flag_pydantic_v2() -> None:
95-
class MyModel(BaseModel):
96-
model_config = {"populate_by_name": True}
97-
aliased_field: str = Field(..., alias="special_field")
98-
99-
class MyFactory(ModelFactory):
100-
__model__ = MyModel
101-
102-
instance = MyFactory.build(aliased_field="some")
103-
assert instance.aliased_field == "some"
104-
105-
106-
def test_build_instance_by_field_name_with_allow_population_by_field_name_flag() -> None:
107-
class MyModel(BaseModel):
108-
aliased_field: str = Field(..., alias="special_field")
109-
110-
class Config:
111-
allow_population_by_field_name = True
112-
113-
class MyFactory(ModelFactory):
114-
__model__ = MyModel
115-
116-
instance = MyFactory.build(special_field="some")
117-
assert instance.aliased_field == "some"
118-
119-
12063
def test_build_model_with_fields_named_like_factory_fields() -> None:
12164
class C(BaseModel):
12265
batch: int

tests/test_complex_types.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import pytest
2323

24-
from pydantic import VERSION, BaseModel
24+
from pydantic import BaseModel
2525

2626
from polyfactory.exceptions import ParameterException
2727
from polyfactory.factories import DataclassFactory
@@ -81,20 +81,6 @@ class MyFactory(ModelFactory):
8181
assert result.person_list[0].pets
8282

8383

84-
@pytest.mark.skipif(VERSION.startswith("2"), reason="pydantic 1 only test")
85-
def test_handles_complex_typing_with_custom_root_type() -> None:
86-
class MyModel(BaseModel):
87-
__root__: List[int]
88-
89-
class MyFactory(ModelFactory[MyModel]):
90-
__model__ = MyModel
91-
92-
result = MyFactory.build()
93-
94-
assert result.__root__
95-
assert isinstance(result.__root__, list)
96-
97-
9884
def test_raises_for_user_defined_types() -> None:
9985
class MyClass:
10086
def __init__(self, value: int) -> None:

0 commit comments

Comments
 (0)