Adds delete function at a class level.
This commit is contained in:
parent
2be01743a3
commit
885b24b35b
3 changed files with 38 additions and 0 deletions
|
@ -1335,6 +1335,10 @@ class HashModel(RedisModel, abc.ABC):
|
||||||
result = cls.parse_obj(document)
|
result = cls.parse_obj(document)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def delete(cls, pk: Any) -> int:
|
||||||
|
return await cls.db().delete(cls.make_primary_key(pk))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@no_type_check
|
@no_type_check
|
||||||
def _get_value(cls, *args, **kwargs) -> Any:
|
def _get_value(cls, *args, **kwargs) -> Any:
|
||||||
|
@ -1503,6 +1507,10 @@ class JsonModel(RedisModel, abc.ABC):
|
||||||
raise NotFoundError
|
raise NotFoundError
|
||||||
return cls.parse_raw(document)
|
return cls.parse_raw(document)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def delete(cls, pk: Any) -> int:
|
||||||
|
return await cls.db().delete(cls.make_primary_key(pk))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def redisearch_schema(cls):
|
def redisearch_schema(cls):
|
||||||
key_prefix = cls.make_key(cls._meta.primary_key_pattern.format(pk=""))
|
key_prefix = cls.make_key(cls._meta.primary_key_pattern.format(pk=""))
|
||||||
|
|
|
@ -374,6 +374,21 @@ async def test_saves_model_and_creates_pk(m):
|
||||||
member2 = await m.Member.get(member.pk)
|
member2 = await m.Member.get(member.pk)
|
||||||
assert member2 == member
|
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):
|
def test_raises_error_with_embedded_models(m):
|
||||||
class Address(m.BaseHashModel):
|
class Address(m.BaseHashModel):
|
||||||
|
|
|
@ -186,6 +186,21 @@ async def test_saves_model_and_creates_pk(address, m, redis):
|
||||||
assert member2 == member
|
assert member2 == member
|
||||||
assert member2.address == address
|
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
|
@pytest.mark.asyncio
|
||||||
async def test_saves_many_implicit_pipeline(address, m):
|
async def test_saves_many_implicit_pipeline(address, m):
|
||||||
|
|
Loading…
Reference in a new issue