Skip to content

Commit

Permalink
test: refactor the tests to move tests to correct places (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
guacs authored Feb 6, 2024
1 parent 9e34fa7 commit 1ae2d52
Show file tree
Hide file tree
Showing 12 changed files with 849 additions and 717 deletions.
90 changes: 0 additions & 90 deletions tests/test_annotated_fields.py

This file was deleted.

49 changes: 48 additions & 1 deletion tests/test_attrs_factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime as dt
from decimal import Decimal
from enum import Enum
from typing import Any, Dict, Generic, List, Tuple, TypeVar
from typing import Any, Dict, Generic, List, Optional, Tuple, TypeVar, Union
from uuid import UUID

import attrs
Expand Down Expand Up @@ -219,3 +219,50 @@ class FooFactory(AttrsFactory[Foo]):
foo = FooFactory.build()

assert foo.default_field == 10


def test_union_types() -> None:
@define
class A:
a: Union[List[str], List[int]]
b: Union[str, List[int]]
c: List[Union[Tuple[int, int], Tuple[str, int]]]

AFactory = AttrsFactory.create_factory(A)

assert AFactory.build()


def test_collection_unions_with_models() -> None:
@define
class A:
a: int

@define
class B:
a: str

@define
class C:
a: Union[List[A], List[B]]
b: List[Union[A, B]]

CFactory = AttrsFactory.create_factory(C)

assert CFactory.build()


@pytest.mark.parametrize("allow_none", (True, False))
def test_optional_type(allow_none: bool) -> None:
@define
class A:
a: Union[str, None]
b: Optional[str]
c: Optional[Union[str, int, List[int]]]

class AFactory(AttrsFactory[A]):
__model__ = A

__allow_none_optionals__ = allow_none

assert AFactory.build()
File renamed without changes.
59 changes: 1 addition & 58 deletions tests/test_build.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from uuid import uuid4

import pytest

from pydantic import VERSION, BaseModel, Field, ValidationError
from pydantic import BaseModel

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


def test_factory_use_construct() -> None:
# factory should pass values without validation
invalid_age = "non_valid_age"
non_validated_pet = PetFactory.build(factory_use_construct=True, age=invalid_age)
assert non_validated_pet.age == invalid_age

with pytest.raises(ValidationError):
PetFactory.build(age=invalid_age)

with pytest.raises(ValidationError):
PetFactory.build(age=invalid_age)


@pytest.mark.skipif(VERSION.startswith("2"), reason="pydantic 1 only test")
def test_build_instance_by_field_alias_with_allow_population_by_field_name_flag_pydantic_v1() -> None:
class MyModel(BaseModel):
aliased_field: str = Field(..., alias="special_field")

class Config:
allow_population_by_field_name = True

class MyFactory(ModelFactory):
__model__ = MyModel

instance = MyFactory.build(aliased_field="some")
assert instance.aliased_field == "some"


@pytest.mark.skipif(VERSION.startswith("1"), reason="pydantic 2 only test")
def test_build_instance_by_field_alias_with_populate_by_name_flag_pydantic_v2() -> None:
class MyModel(BaseModel):
model_config = {"populate_by_name": True}
aliased_field: str = Field(..., alias="special_field")

class MyFactory(ModelFactory):
__model__ = MyModel

instance = MyFactory.build(aliased_field="some")
assert instance.aliased_field == "some"


def test_build_instance_by_field_name_with_allow_population_by_field_name_flag() -> None:
class MyModel(BaseModel):
aliased_field: str = Field(..., alias="special_field")

class Config:
allow_population_by_field_name = True

class MyFactory(ModelFactory):
__model__ = MyModel

instance = MyFactory.build(special_field="some")
assert instance.aliased_field == "some"


def test_build_model_with_fields_named_like_factory_fields() -> None:
class C(BaseModel):
batch: int
Expand Down
16 changes: 1 addition & 15 deletions tests/test_complex_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import pytest

from pydantic import VERSION, BaseModel
from pydantic import BaseModel

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


@pytest.mark.skipif(VERSION.startswith("2"), reason="pydantic 1 only test")
def test_handles_complex_typing_with_custom_root_type() -> None:
class MyModel(BaseModel):
__root__: List[int]

class MyFactory(ModelFactory[MyModel]):
__model__ = MyModel

result = MyFactory.build()

assert result.__root__
assert isinstance(result.__root__, list)


def test_raises_for_user_defined_types() -> None:
class MyClass:
def __init__(self, value: int) -> None:
Expand Down
Loading

0 comments on commit 1ae2d52

Please sign in to comment.