Adds delete function at a class level.

This commit is contained in:
Simon Prickett 2022-02-14 20:44:49 +00:00
parent 2be01743a3
commit 885b24b35b
3 changed files with 38 additions and 0 deletions

View file

@ -1335,6 +1335,10 @@ class HashModel(RedisModel, abc.ABC):
result = cls.parse_obj(document)
return result
@classmethod
async def delete(cls, pk: Any) -> int:
return await cls.db().delete(cls.make_primary_key(pk))
@classmethod
@no_type_check
def _get_value(cls, *args, **kwargs) -> Any:
@ -1503,6 +1507,10 @@ class JsonModel(RedisModel, abc.ABC):
raise NotFoundError
return cls.parse_raw(document)
@classmethod
async def delete(cls, pk: Any) -> int:
return await cls.db().delete(cls.make_primary_key(pk))
@classmethod
def redisearch_schema(cls):
key_prefix = cls.make_key(cls._meta.primary_key_pattern.format(pk=""))

View file

@ -374,6 +374,21 @@ async def test_saves_model_and_creates_pk(m):
member2 = await m.Member.get(member.pk)
assert member2 == member
@pytest.mark.asyncio
async def test_delete(m):
member = m.Member(
first_name="Simon",
last_name="Prickett",
email="s@example.com",
join_date=today,
age=97,
bio="This is a test use to be deleted.",
)
await member.save()
response = await m.Member.delete(member.pk)
assert response == 1
def test_raises_error_with_embedded_models(m):
class Address(m.BaseHashModel):

View file

@ -186,6 +186,21 @@ async def test_saves_model_and_creates_pk(address, m, redis):
assert member2 == member
assert member2.address == address
@pytest.mark.asyncio
async def test_delete(address, m, redis):
member = m.Member(
first_name="Simon",
last_name="Prickett",
email="s@example.com",
join_date=today,
age=38,
address=address,
)
await member.save()
response = await m.Member.delete(member.pk)
assert response == 1
@pytest.mark.asyncio
async def test_saves_many_implicit_pipeline(address, m):