Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuperiorStanislav committed Mar 19, 2024
1 parent fa036ad commit 56a96fd
Show file tree
Hide file tree
Showing 22 changed files with 2,018 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ jobs:
run: poetry install --no-interaction --all-extras
- name: Run checks
run: |
poetry run inv github-actions.set-up-hosts
poetry run inv docker.up
poetry run inv pre-commit.run-hooks
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ repos:
pass_filenames: false
types: [ file ]
stages: [ push ]

- id: pytest
name: Run pytest
entry: inv pytest.run
language: system
pass_filenames: false
types: [ file ]
stages: [ push ]
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version: '3.7'
name: "saritasa-sqlachemy-tools"
name: "saritasa-sqlalchemy-tools"

services:
postgres:
Expand Down
178 changes: 177 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 47 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ saritasa_invocations = "^1.1.0"
# https://mypy.readthedocs.io/en/stable/
mypy = "^1.9.0"

[tool.poetry.group.test.dependencies]
pytest = "^8.1.1"
# pytest-asyncio is a pytest plugin. It facilitates testing of code that
# uses the asyncio library.
# https://pytest-asyncio.readthedocs.io/en/latest/
pytest-asyncio = "^0.21.1"
# Database testing fixtures using the SQLAlchemy asyncio API
# https://pypi.org/project/pytest-async-sqlalchemy/
pytest-async-sqlalchemy = "^0.2.0"
# To prettify pytest output
pytest-sugar = "^1.0.0"
# asyncpg is a database interface library designed specifically for PostgreSQL
# and Python/asyncio.
# https://magicstack.github.io/asyncpg/current/
asyncpg = "^0.28.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down Expand Up @@ -171,7 +187,7 @@ section-order = [
sqlachemy = ["sqlachemy"]

[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = true
fixture-parentheses = false
parametrize-names-type = "list"
parametrize-values-type = "list"
parametrize-values-row-type = "list"
Expand Down Expand Up @@ -210,3 +226,33 @@ ignore = [
"**/*test_*.py",
"invocations/**"
]

[tool.pytest.ini_options]
# --capture=no
# allow use of ipdb during tests
# --ff
# run last failed tests first
addopts = [
"--capture=no",
"--ff",
"--cov=saritasa_sqlalchemy_tools",
"--cov-report=html",
]
# skip all files inside following dirs
norecursedirs = [
"venv",
".venv",
]
asyncio_mode = "auto"

[tool.coverage.run]
omit = [
"saritasa_sqlalchemy_tools/session.py",
]

# https://docformatter.readthedocs.io/en/latest/configuration.html#
[tool.docformatter]
wrap-descriptions=0
in-place=true
blank=true
black=true
2 changes: 1 addition & 1 deletion saritasa_sqlalchemy_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
Filter,
LazyLoaded,
LazyLoadedSequence,
OrderingClausesT,
OrderingClauses,
OrderingEnum,
OrderingEnumMeta,
SQLWhereFilter,
Expand Down
22 changes: 17 additions & 5 deletions saritasa_sqlalchemy_tools/auto_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pydantic
import pydantic_core
import sqlalchemy.dialects.postgresql.ranges
import sqlalchemy.orm

from . import models

Expand Down Expand Up @@ -148,7 +149,7 @@ def get_schema(
pydantic.field_validator(field)(validator)
)
continue
if isinstance(field, tuple):
if isinstance(field, tuple) and len(field) == 2:
field_name, field_type = field
generated_fields[field_name] = (
cls._generate_field_with_custom_type(
Expand All @@ -159,7 +160,7 @@ def get_schema(
)
)
for index, validator in enumerate(
extra_fields_validators.get(field, ()),
extra_fields_validators.get(field_name, ()),
):
validators[f"{field_name}_validator_{index}"] = (
pydantic.field_validator(field_name)(validator)
Expand Down Expand Up @@ -197,7 +198,14 @@ def _generate_field(
model_attribute,
extra_field_config,
)
if model_attribute.type.__class__ not in types_mapping:
if isinstance(model_attribute.property, sqlalchemy.orm.Relationship):
raise UnableProcessTypeError(
"Schema generation is not supported for relationship "
f"fields({field}), please use auto-schema or pydantic class",
)
if (
model_attribute.type.__class__ not in types_mapping
): # pragma: no cover
raise UnableProcessTypeError(
"Can't generate generate type for"
f" {model_attribute.type.__class__}"
Expand Down Expand Up @@ -443,7 +451,7 @@ def _generate_enum_field(
) -> PydanticFieldConfig:
"""Generate enum field."""
if model_type.enum_class is None: # type: ignore
raise UnableToExtractEnumClassError(
raise UnableToExtractEnumClassError( # pragma: no cover
f"Can't extract enum for {field} in {model}",
)
return (
Expand Down Expand Up @@ -517,7 +525,11 @@ def _generate_array_field(
model_type.item_type, # type: ignore
extra_field_config,
)
return list[list_type], pydantic_core.PydanticUndefined # type: ignore
return (
list[list_type] | None # type: ignore
if model_attribute.nullable
else list[list_type] # type: ignore
), pydantic_core.PydanticUndefined


ModelAutoSchemaT = typing.TypeVar(
Expand Down
2 changes: 1 addition & 1 deletion saritasa_sqlalchemy_tools/repositories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
WhereFilters,
transform_search_filter,
)
from .ordering import OrderingClausesT, OrderingEnum, OrderingEnumMeta
from .ordering import OrderingClauses, OrderingEnum, OrderingEnumMeta
from .types import (
Annotation,
AnnotationSequence,
Expand Down
Loading

0 comments on commit 56a96fd

Please sign in to comment.