Skip to content

Commit

Permalink
Add tests to pydantic schema and module_loading
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Jul 21, 2023
1 parent 283eb07 commit 622ad3e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cov: ## Run tests and coverage only for specific ones
ESMERALD_SETTINGS_MODULE='tests.settings.TestSettings' coverage report --show-missing
ESMERALD_SETTINGS_MODULE='tests.settings.TestSettings' coverage html


.PHONY: requirements
requirements: ## Install requirements for development
pip install -e .[dev,test,doc,templates,jwt,encoders,schedulers,ipython,ptpython]
Expand Down
7 changes: 3 additions & 4 deletions esmerald/utils/pydantic/schema.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from decimal import Decimal
from typing import TYPE_CHECKING, Any, TypeVar, cast
from typing import Any, TypeVar, cast

T = TypeVar("T", int, float, Decimal)
from pydantic.fields import FieldInfo

if TYPE_CHECKING:
from pydantic.fields import FieldInfo
T = TypeVar("T", int, float, Decimal)


def is_field_optional(field: "FieldInfo") -> bool:
Expand Down
26 changes: 26 additions & 0 deletions tests/utils/test_module_loading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from esmerald import EsmeraldAPISettings
from esmerald.utils.module_loading import import_string


def test_import_error_module_loading():
path = "tests"

with pytest.raises(ImportError):
import_string(path)


def test_attribute_error_module_loading():
path = "tests.settings.TestSetting"

with pytest.raises(ImportError):
import_string(path)


def test_imports_successfully():
path = "tests.settings.TestSettings"

settings = import_string(path)

assert issubclass(settings, EsmeraldAPISettings)
19 changes: 19 additions & 0 deletions tests/utils/test_pydantic_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Any

import pytest

from esmerald.params import Param
from esmerald.utils.pydantic.schema import is_any_type, is_field_optional


def test_is_any_type():
field = Param(annotation=Any)

assert is_any_type(field)


@pytest.mark.parametrize("allow_none,return_type", [(True, False), (False, False)])
def test_is_field_optional(allow_none, return_type):
field = Param(allow_none=allow_none)

assert is_field_optional(field) == return_type

0 comments on commit 622ad3e

Please sign in to comment.