From b42e63606ac6de38883db91dd36cd95ed682a186 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Tue, 29 Mar 2022 22:15:05 +0100 Subject: [PATCH] Adds expire and test. --- aredis_om/model/model.py | 9 +++++++++ tests/test_hash_model.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 0c2ef60..dafd41a 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -1130,6 +1130,15 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta): async def save(self, pipeline: Optional[Pipeline] = None) -> "RedisModel": raise NotImplementedError + async def expire(self, num_seconds: int, pipeline: Optional[Pipeline] = None): + if pipeline is None: + db = self.db() + else: + db = pipeline + + # TODO: Wrap any Redis response errors in a custom exception? + await db.expire(self.make_primary_key(self.pk), num_seconds) + @validator("pk", always=True, allow_reuse=True) def validate_pk(cls, v): if not v: diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index 133e43a..ddcf905 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -391,6 +391,24 @@ async def test_delete(m): assert response == 1 +@pytest.mark.asyncio +async def test_expire(m): + member = m.Member( + first_name="Expire", + last_name="Test", + email="e@example.com", + join_date=today, + age=93, + bio="This is a test user for expiry", + ) + + await member.save() + await member.expire(60) + + ttl = await m.Member.db().ttl(member.key()) + assert ttl > 0 + + def test_raises_error_with_embedded_models(m): class Address(m.BaseHashModel): address_line_1: str