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) pk = getattr(self, self._meta.primary_key.field.name)
return self.make_primary_key(pk) return self.make_primary_key(pk)
async def delete(self): @classmethod
return await self.db().delete(self.key()) async def delete(cls, pk: Any) -> int:
"""Delete data at this key."""
return await cls.db().delete(cls.make_primary_key(pk))
@classmethod @classmethod
async def get(cls, pk: Any) -> "RedisModel": async def get(cls, pk: Any) -> "RedisModel":
@ -1335,10 +1337,6 @@ 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:
@ -1507,10 +1505,6 @@ 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=""))

View file

@ -374,6 +374,7 @@ 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 @pytest.mark.asyncio
async def test_delete(m): async def test_delete(m):
member = m.Member( member = m.Member(
@ -382,7 +383,7 @@ async def test_delete(m):
email="s@example.com", email="s@example.com",
join_date=today, join_date=today,
age=97, age=97,
bio="This is a test use to be deleted.", bio="This is a test user to be deleted.",
) )
await member.save() 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 == member
assert member2.address == address assert member2.address == address
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_delete(address, m, redis): async def test_delete(address, m, redis):
member = m.Member( member = m.Member(