Skip to content

Commit

Permalink
update with not found record now raise error as defined by warning
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbenav committed Dec 26, 2024
1 parent 86f1956 commit e76ddba
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 66 deletions.
8 changes: 1 addition & 7 deletions fastcrud/crud/fast_crud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any, Dict, Generic, Union, Optional, Callable
from datetime import datetime, timezone
import warnings

from pydantic import ValidationError
from sqlalchemy import (
Expand Down Expand Up @@ -2206,12 +2205,7 @@ async def update(
"""
total_count = await self.count(db, **kwargs)
if total_count == 0:
warnings.warn(
"Passing non-existing records to `update` will raise NoResultFound on version 0.15.3.",
DeprecationWarning,
stacklevel=2,
)
# raise NoResultFound("No record found to update.")
raise NoResultFound("No record found to update.")
if not allow_multiple and total_count > 1:
raise MultipleResultsFound(
f"Expected exactly one record to update, found {total_count}."
Expand Down
31 changes: 1 addition & 30 deletions tests/sqlalchemy/crud/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from sqlalchemy import select
from sqlalchemy.exc import MultipleResultsFound
from sqlalchemy.exc import MultipleResultsFound, NoResultFound

from fastcrud.crud.fast_crud import FastCRUD
from ...sqlalchemy.conftest import ModelTest, UpdateSchemaTest, ModelTestWithTimestamp
Expand Down Expand Up @@ -51,24 +51,11 @@ async def test_update_non_existent_record(async_session, test_data):
crud = FastCRUD(ModelTest)
non_existent_id = 99999
updated_data = {"name": "New Name"}
"""
In version 0.15.3, the `update` method will raise a `NoResultFound` exception:

```
with pytest.raises(NoResultFound) as exc_info:
await crud.update(db=async_session, object=updated_data, id=non_existent_id)

assert "No record found to update" in str(exc_info.value)
```
For 0.15.2, the test will check if the record is not updated.
"""
await crud.update(db=async_session, object=updated_data, id=non_existent_id)

record = await async_session.execute(
select(ModelTest).where(ModelTest.id == non_existent_id)
)
assert record.scalar_one_or_none() is None


@pytest.mark.asyncio
Expand All @@ -81,26 +68,10 @@ async def test_update_invalid_filters(async_session, test_data):
updated_data = {"name": "New Name"}

non_matching_filter = {"name": "NonExistingName"}
"""
In version 0.15.3, the `update` method will raise a `NoResultFound` exception:
```
with pytest.raises(NoResultFound) as exc_info:
await crud.update(db=async_session, object=updated_data, **non_matching_filter)

assert "No record found to update" in str(exc_info.value)
```
For 0.15.2, the test will check if the record is not updated.
"""
await crud.update(db=async_session, object=updated_data, **non_matching_filter)

for item in test_data:
record = await async_session.execute(
select(ModelTest).where(ModelTest.id == item["id"])
)
fetched_record = record.scalar_one()
assert fetched_record.name != "New Name"


@pytest.mark.asyncio
Expand Down
30 changes: 1 addition & 29 deletions tests/sqlmodel/crud/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from sqlalchemy import select
from sqlalchemy.exc import MultipleResultsFound
from sqlalchemy.exc import MultipleResultsFound, NoResultFound

from fastcrud.crud.fast_crud import FastCRUD
from ...sqlmodel.conftest import ModelTest, UpdateSchemaTest, ModelTestWithTimestamp
Expand Down Expand Up @@ -51,24 +51,11 @@ async def test_update_non_existent_record(async_session, test_data):
crud = FastCRUD(ModelTest)
non_existent_id = 99999
updated_data = {"name": "New Name"}
"""
In version 0.15.3, the `update` method will raise a `NoResultFound` exception:

```
with pytest.raises(NoResultFound) as exc_info:
await crud.update(db=async_session, object=updated_data, id=non_existent_id)

assert "No record found to update" in str(exc_info.value)
```
For 0.15.2, the test will check if the record is not updated.
"""
await crud.update(db=async_session, object=updated_data, id=non_existent_id)

record = await async_session.execute(
select(ModelTest).where(ModelTest.id == non_existent_id)
)
assert record.scalar_one_or_none() is None


@pytest.mark.asyncio
Expand All @@ -81,26 +68,11 @@ async def test_update_invalid_filters(async_session, test_data):
updated_data = {"name": "New Name"}

non_matching_filter = {"name": "NonExistingName"}
"""
In version 0.15.3, the `update` method will raise a `NoResultFound` exception:

```
with pytest.raises(NoResultFound) as exc_info:
await crud.update(db=async_session, object=updated_data, **non_matching_filter)

assert "No record found to update" in str(exc_info.value)
```
For 0.15.2, the test will check if the record is not updated.
"""
await crud.update(db=async_session, object=updated_data, **non_matching_filter)

for item in test_data:
record = await async_session.execute(
select(ModelTest).where(ModelTest.id == item["id"])
)
fetched_record = record.scalar_one()
assert fetched_record.name != "New Name"


@pytest.mark.asyncio
Expand Down

0 comments on commit e76ddba

Please sign in to comment.