From ffce30706ff70935565d7dc08ea46dd690ed6ad1 Mon Sep 17 00:00:00 2001 From: Daniel Tom Date: Fri, 2 Aug 2024 11:45:44 +0200 Subject: [PATCH 1/3] Add cov report --- .gitignore | 2 ++ README.md | 2 +- pyproject.toml | 3 ++- requirements-dev.lock | 4 ++++ sadel/base.py | 11 +---------- tests/test_base.py | 20 +++++++++++++++++++- 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 287edb4..4222d64 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ dist/ wheels/ *.egg-info .pytest_cache +.coverage +coverage.xml # venv .venv diff --git a/README.md b/README.md index da21253..5091985 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ from sqlmodel import Field, create_engine, select, or_ from sqlmodel.ext.asyncio.session import AsyncSession class Hero(Sadel, table=True): - __tablename__ = "hero" # type: ignore + __tablename__ = "hero" _upsert_index_elements = {"id"} id: int | None = Field(default=None, primary_key=True) diff --git a/pyproject.toml b/pyproject.toml index 132d98a..8813aae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,13 +25,14 @@ dev-dependencies = [ "pytest-asyncio~=0.23.8", "aiosqlite~=0.20.0", "greenlet~=3.0.3", + "pytest-cov~=5.0.0", ] [tool.rye.scripts] fmt = "rye fmt" lint = "rye lint --fix" check = "pyright" -test = "rye test" +test = "rye test -- --cov sadel --cov-report term-missing --cov-report xml:coverage.xml" all = { chain = ["fmt", "lint", "check", "test"] } diff --git a/requirements-dev.lock b/requirements-dev.lock index 4bac043..d54e330 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -12,6 +12,8 @@ aiosqlite==0.20.0 annotated-types==0.7.0 # via pydantic +coverage==7.6.0 + # via pytest-cov greenlet==3.0.3 iniconfig==2.0.0 # via pytest @@ -28,7 +30,9 @@ pydantic-core==2.20.1 pyright==1.1.374 pytest==8.3.2 # via pytest-asyncio + # via pytest-cov pytest-asyncio==0.23.8 +pytest-cov==5.0.0 setuptools==72.1.0 sqlalchemy==2.0.31 # via sqlmodel diff --git a/sadel/base.py b/sadel/base.py index 575e4d2..c73d311 100644 --- a/sadel/base.py +++ b/sadel/base.py @@ -6,7 +6,6 @@ import sqlalchemy as sa from pydantic import ConfigDict -from pydantic.v1 import validate_model from sqlalchemy.dialects.postgresql import Insert, insert from sqlmodel import Field, SQLModel @@ -14,7 +13,7 @@ class Sadel(SQLModel): """Base class for SQL models.""" - model_config = ConfigDict(validate_assignment=True) # type: ignore + model_config = ConfigDict(validate_assignment=True) # pyright: ignore created_on: datetime | None = Field( default=None, sa_type=sa.DateTime(timezone=True), # type: ignore @@ -37,14 +36,6 @@ class Sadel(SQLModel): ), ) - def __init__(self, **data: Any) -> None: - super().__init__(**data) - # Workaround to validate the model on init, which is not supported by SQLModel - if hasattr(self, "__config__"): - _, _, validation_error = validate_model(self.__class__, data) # pyright: ignore - if validation_error: - raise validation_error - # Specifies the set of index elements which represent the ON CONFLICT target _upsert_index_elements: ClassVar[set[str]] = set() diff --git a/tests/test_base.py b/tests/test_base.py index bb557c8..7eb1e5a 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -12,7 +12,7 @@ class Hero(Sadel, table=True): - __tablename__ = "hero" # type: ignore + __tablename__ = "hero" _upsert_index_elements = {"id"} id: Optional[int] = Field(default=None, primary_key=True) @@ -90,3 +90,21 @@ async def test_modified_on_upsert(temp_db): async def test_validate(): with pytest.raises(ValidationError, match="Input should be a valid integer"): Hero(name="Deadpond", secret_name="Dive Wilson", age=datetime.now()) + + +@pytest.mark.asyncio() +async def test_missing_upsert_identifier(temp_db): + class Woops(Sadel, table=True): + __tablename__ = "woops" + + id: Optional[int] = Field(default=None, primary_key=True) + name: str + secret_name: str + age: Optional[int] = None + + sqlite_url_async = f"sqlite+aiosqlite:///{temp_db}" + async_engine = create_async_engine(sqlite_url_async, echo=True, future=True) + loser_1 = Woops(id=1, name="Deadpond", secret_name="Dive Wilson") + with pytest.raises(ValueError, match="No upsert index elements specified for the model."): + async with AsyncSession(async_engine) as session: + await Woops.upsert(loser_1, session) From dc0adec212cf031a4db4c202a8f2d18d9f352d65 Mon Sep 17 00:00:00 2001 From: Daniel Tom Date: Fri, 2 Aug 2024 12:40:10 +0200 Subject: [PATCH 2/3] Add junit report --- .gitignore | 1 + pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4222d64..64c4382 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ wheels/ .pytest_cache .coverage coverage.xml +junit/ # venv .venv diff --git a/pyproject.toml b/pyproject.toml index 8813aae..7ef31a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,7 @@ dev-dependencies = [ fmt = "rye fmt" lint = "rye lint --fix" check = "pyright" -test = "rye test -- --cov sadel --cov-report term-missing --cov-report xml:coverage.xml" +test = "rye test -- --cov sadel --cov-report term-missing --cov-report xml:coverage.xml --junitxml=junit/report.xml" all = { chain = ["fmt", "lint", "check", "test"] } From 8c500ae2aa706ec1af2044c8b914266ec74db3f1 Mon Sep 17 00:00:00 2001 From: Daniel Tom Date: Fri, 2 Aug 2024 12:49:37 +0200 Subject: [PATCH 3/3] Rename pr.yaml and make availabe for main --- .github/workflows/{pr.yaml => test.yml} | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) rename .github/workflows/{pr.yaml => test.yml} (75%) diff --git a/.github/workflows/pr.yaml b/.github/workflows/test.yml similarity index 75% rename from .github/workflows/pr.yaml rename to .github/workflows/test.yml index 6139fb6..67d886c 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/test.yml @@ -1,6 +1,9 @@ -name: pr +name: test on: + push: + branches: [ main ] pull_request: + branches: [ main ] types: [opened, reopened, synchronize] jobs: test: @@ -13,15 +16,6 @@ jobs: - uses: eifinger/setup-rye@v3 - run: rye pin ${{ matrix.py }} # pin Python version - run: rye sync -# - run: | -# diff=$(git diff requirements.lock) -# if [[ -n $diff ]]; then -# echo "requirements.lock file has changed:" -# echo "$diff" -# exit 1 -# else -# echo "requirements.lock file has not changed" -# fi - run: rye fmt --check # check formatting is correct - run: rye lint # and linting - run: rye run check # typecheck too