Fix crash when trying to delete non-existent record (#372)

This commit is contained in:
dvora-h 2022-09-15 13:29:31 +03:00 committed by GitHub
parent 285f7c90ee
commit e9529e35be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -32,6 +32,7 @@ from pydantic.main import ModelMetaclass, validate_model
from pydantic.typing import NoArgAnyCallable
from pydantic.utils import Representation
from redis.commands.json.path import Path
from redis.exceptions import ResponseError
from typing_extensions import Protocol, get_args, get_origin
from ulid import ULID
@ -799,7 +800,10 @@ class FindQuery:
async def delete(self):
"""Delete all matching records in this query."""
# TODO: Better response type, error detection
try:
return await self.model.db().delete(*[m.key() for m in await self.all()])
except ResponseError:
return 0
async def __aiter__(self):
if self._model_cache:

View file

@ -140,6 +140,27 @@ async def test_exact_match_queries(members, m):
assert actual == [member1]
@py_test_mark_asyncio
async def test_delete_non_exist(members, m):
member1, member2, member3 = members
actual = await m.Member.find(
(m.Member.last_name == "Brookins") & ~(m.Member.first_name == "Andrew")
).all()
assert actual == [member2]
assert (
1
== await m.Member.find(
(m.Member.last_name == "Brookins") & ~(m.Member.first_name == "Andrew")
).delete()
)
assert (
0
== await m.Member.find(
(m.Member.last_name == "Brookins") & ~(m.Member.first_name == "Andrew")
).delete()
)
@py_test_mark_asyncio
async def test_full_text_search_queries(members, m):
member1, member2, member3 = members