Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No module TTL and expire #2

Open
codeasashu opened this issue Dec 29, 2021 · 0 comments
Open

No module TTL and expire #2

codeasashu opened this issue Dec 29, 2021 · 0 comments

Comments

@codeasashu
Copy link

In my code, I am trying to use expire and TTL methods, which are natural to use when using redis cache backend. But they raise TypeError when unittesting with this library.

Steps to reproduce:

  • Create a new django project: pip install django django_fakeredis pytest, django-admin startproject fakeredisbug
  • Create example.py and example_test.py as provided below inside the application (path: fakeredisbug/fakeredisbug)
  • Add cache settings to django settings (you can use dummy cache for now)
  • run pytest: DJANGO_SETTINGS_MODULE=fakeredisbug.settings pytest. Observe the result

example.py

from django.core.cache import cache

def set_cache(key, val, expire):
   cache.set(key, val, expire)
   return cache.ttl(key)

example_test.py

from django_fakeredis.fakeredis import FakeRedis
from fakeredisbug.example import set_cache

@FakeRedis("fakeredisbug.example.cache")
def test_set_cache():
    expire = set_cache("a", "b", 300)
    assert expire == 300

Result:

============================================================= test session starts =============================================================
platform darwin -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/ashutosh/code/office/test
collected 1 item

fakeredisbug/fakeredisbug/example_test.py F                                                                                             [100%]

================================================================== FAILURES ===================================================================
_______________________________________________________________ test_set_cache ________________________________________________________________

    @FakeRedis("fakeredisbug.example.cache")
    def test_set_cache():
>       expire = set_cache("a", "b", 300)

fakeredisbug/fakeredisbug/example_test.py:7:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
fakeredisbug/fakeredisbug/example.py:6: in set_cache
    return cache.ttl(key)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.utils.connection.ConnectionProxy object at 0x1040def70>, item = 'ttl'

    def __getattr__(self, item):
>       return getattr(self._connections[self._alias], item)
E       AttributeError: 'LocMemCache' object has no attribute 'ttl'

venv/lib/python3.9/site-packages/django/utils/connection.py:15: AttributeError
=========================================================== short test summary info ===========================================================
FAILED fakeredisbug/fakeredisbug/example_test.py::test_set_cache - AttributeError: 'LocMemCache' object has no attribute 'ttl'
============================================================== 1 failed in 0.14s ==============================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant