From ab0cd3e664fa35834ba405a6a8bd840ec3bdc0d1 Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Fri, 28 Apr 2023 21:11:48 -0500 Subject: [PATCH] add tests --- pyproject.toml | 7 ++- tests/models.py | 31 ++++++++- tests/test_person.py | 147 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 170 insertions(+), 15 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ddcdf44..64d7c90 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,6 +52,7 @@ dependencies = [ "pytest", "pytest-cov", "pytest-mock", + 'polyfactory', "ruff", 'pyannotate', "black", @@ -96,10 +97,11 @@ testpaths = ["tests"] fail-under=80 [tool.ruff] -ignore = ["E501", "D211", "D213"] +ignore = ["E501", "D211", "D212", "D213"] target-version = "py37" + select = [ "F", # Pyflakes "E", # Error @@ -159,3 +161,6 @@ max-complexity = 13 [tool.ruff.pylint] max-branches = 13 + +[tool.ruff.per-file-ignores] +'tests/**' = ["D100", "D101", "D102", "D103", "D104", "D105", "S101"] diff --git a/tests/models.py b/tests/models.py index 86e0422..8c02bb4 100644 --- a/tests/models.py +++ b/tests/models.py @@ -11,6 +11,7 @@ Classes: from typing import Optional +from polyfactory.factories.pydantic_factory import ModelFactory from pydantic import BaseModel, Field @@ -68,7 +69,7 @@ class Person(BaseModel): ..., description="The name of the person.", ) - other_name: Optional[str] = Field( + alias: Optional[str] = Field( None, description="An optional other name for the person.", ) @@ -92,3 +93,31 @@ class Person(BaseModel): ..., description="The person's hair.", ) + + +class AlphaFactory(ModelFactory[Alpha]): + + """A class for generating an alpha value.""" + + __model__ = Alpha + + +class ColorFactory(ModelFactory[Color]): + + """A class for generating a color.""" + + __model__ = Color + + +class HairFactory(ModelFactory[Hair]): + + """A class for generating hair.""" + + __model__ = Hair + + +class PersonFactory(ModelFactory[Person]): + + """A class for generating a person.""" + + __model__ = Person diff --git a/tests/test_person.py b/tests/test_person.py index e932ebc..e0ce69e 100644 --- a/tests/test_person.py +++ b/tests/test_person.py @@ -7,6 +7,8 @@ SPDX-License-Identifier: MIT import inspect +import pytest + from pydantic_typer import expand_pydantic_args from . import models @@ -16,16 +18,6 @@ from . import models # @expand_pydantic_args() # def get_person(alpha) -> None: # """Mydocstring.""" -# from rich import print - -# print(str(thing)) -# print(person) - -# sig = inspect.signature(get_person) -# params = sig.parameters - -# for field in models.Alpha.__fields__.values(): -# assert field.name in params def test_single_signature() -> None: @@ -36,8 +28,137 @@ def test_single_signature() -> None: sig = inspect.signature(get_person) params = sig.parameters + assert "a" in params - for field in models.Alpha.__fields__.values(): - assert field.name in params + assert "alpha" not in params - assert get_person(a=1) == models.Alpha(a=1) + +@pytest.mark.parametrize( + "alpha", + models.AlphaFactory().batch(size=5), +) +def test_single_instance(alpha: models.Alpha) -> None: + @expand_pydantic_args() + def get_person(alpha: models.Alpha) -> None: + """Mydocstring.""" + return alpha + + assert get_person(**alpha.dict()) == alpha + # this should maybe work + # assert get_person(models.Alpha(a=1)) == models.Alpha(a=1) + + +def test_one_nest_signature() -> None: + @expand_pydantic_args() + def get_person(color: models.Color) -> None: + """Mydocstring.""" + return color + + sig = inspect.signature(get_person) + params = sig.parameters + assert "r" in params + assert "g" in params + assert "b" in params + assert "a" in params + + assert "color" not in params + assert "alpha" not in params + + +@pytest.mark.parametrize( + "color", + models.ColorFactory().batch(size=5), +) +def test_one_nest_instance(color: models.Color) -> None: + @expand_pydantic_args() + def get_person(color: models.Color) -> None: + """Mydocstring.""" + return color + + assert get_person(**color.dict(exclude={"alpha"}), **color.alpha.dict()) == color + + +def test_two_nest_signature() -> None: + @expand_pydantic_args() + def get_person(hair: models.Hair) -> None: + """Mydocstring.""" + return hair + + sig = inspect.signature(get_person) + params = sig.parameters + assert "length" in params + assert "r" in params + assert "g" in params + assert "b" in params + assert "a" in params + + assert "hair" not in params + assert "color" not in params + assert "alpha" not in params + + +@pytest.mark.parametrize( + "hair", + models.HairFactory().batch(size=5), +) +def test_two_nest_instance(hair: models.Hair) -> None: + @expand_pydantic_args() + def get_person(hair: models.Hair) -> None: + """Mydocstring.""" + return hair + + assert ( + get_person( + **hair.dict(exclude={"color"}), + **hair.color.dict(exclude={"alpha"}), + **hair.color.alpha.dict() + ) == + hair + ) + + +def test_three_nest_signature() -> None: + @expand_pydantic_args() + def get_person(person: models.Person) -> None: + """Mydocstring.""" + return person + + sig = inspect.signature(get_person) + params = sig.parameters + assert "name" in params + assert "alias" in params + assert "age" in params + assert "email" in params + assert "pet" in params + assert "address" in params + assert "length" in params + assert "r" in params + assert "g" in params + assert "b" in params + assert "a" in params + + assert "person" not in params + assert "hair" not in params + assert "color" not in params + assert "alpha" not in params + + +@pytest.mark.parametrize( + "person", + models.PersonFactory().batch(size=5), +) +def test_three_nest_instance(person: models.Person) -> None: + @expand_pydantic_args() + def get_person(person: models.Person) -> None: + """Mydocstring.""" + return person + + assert ( + get_person( + **person.dict(exclude={"hair"}), + **person.hair.dict(exclude={"color"}), + **person.hair.color.dict(exclude={"alpha"}), + **person.hair.color.alpha.dict() + ) == + person + )