diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 8271d2e..162c7a0 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -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 - return await self.model.db().delete(*[m.key() for m in await self.all()]) + 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: diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index b2bd30a..572213d 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -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