Fixed linter errors, refactored delete.

This commit is contained in:
Simon Prickett 2022-02-15 18:46:50 +00:00
parent 885b24b35b
commit 6d009a0d72
3 changed files with 7 additions and 11 deletions

View file

@ -1114,8 +1114,10 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
pk = getattr(self, self._meta.primary_key.field.name)
return self.make_primary_key(pk)
async def delete(self):
return await self.db().delete(self.key())
@classmethod
async def delete(cls, pk: Any) -> int:
"""Delete data at this key."""
return await cls.db().delete(cls.make_primary_key(pk))
@classmethod
async def get(cls, pk: Any) -> "RedisModel":
@ -1335,10 +1337,6 @@ 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:
@ -1507,10 +1505,6 @@ 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,7 @@ 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(
@ -382,7 +383,7 @@ async def test_delete(m):
email="s@example.com",
join_date=today,
age=97,
bio="This is a test use to be deleted.",
bio="This is a test user to be deleted.",
)
await member.save()

View file

@ -186,6 +186,7 @@ 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(