From 885b24b35b7662f3073839ccb9d5b470696b6d85 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Mon, 14 Feb 2022 20:44:49 +0000 Subject: [PATCH 1/2] Adds delete function at a class level. --- aredis_om/model/model.py | 8 ++++++++ tests/test_hash_model.py | 15 +++++++++++++++ tests/test_json_model.py | 15 +++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 8a1fac2..0d9ea05 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -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="")) diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index 9a9291a..a48999f 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -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): diff --git a/tests/test_json_model.py b/tests/test_json_model.py index 8b360f7..ce240e0 100644 --- a/tests/test_json_model.py +++ b/tests/test_json_model.py @@ -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): From 6d009a0d7218494f56243b1a50847534502411ef Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Tue, 15 Feb 2022 18:46:50 +0000 Subject: [PATCH 2/2] Fixed linter errors, refactored delete. --- aredis_om/model/model.py | 14 ++++---------- tests/test_hash_model.py | 3 ++- tests/test_json_model.py | 1 + 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 0d9ea05..0c2ef60 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -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="")) diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index a48999f..133e43a 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -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() diff --git a/tests/test_json_model.py b/tests/test_json_model.py index ce240e0..d920a40 100644 --- a/tests/test_json_model.py +++ b/tests/test_json_model.py @@ -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(