1

I have a Django app, and I'm trying to create unit tests for functions/methods that work with the cache. My tests are run using python manage.py test using the same Docker container that I use for running the app locally. The functions/methods I'm testing contain lines that are similar to the following:

from django.core.cache import cache
...
def some_function():
    ...
    cache.set('test', ('hello', 'world'))
    retrieved = cache.get('test')
    ...

I expect retrieved to be the value that I retrieved from the cache (in this case, ('hello', 'world')); however, retrieved is somehow always a datetime.datetime object. This happens for various different cache backends, including django.core.cache.backends.locmem.LocMemCache and django_redis.cache.RedisCache. How can I correct this cache behavior for tests?

sunw
  • 535
  • 5
  • 29
  • There's not enough information here. The local memory cache pickles values, so you'll get back what you put in. Most likely you're not putting what you think you are into the cache int he first place. You'll need to show the real code (or a minimal failing example) where you're adding something to the cache and then it comes out in an unexpected way. It could be you're reusing a cache key used by something else, for example. – Tom Carrick Mar 09 '22 at 14:11
  • The example I've provided shows me putting in `('hello', 'world')` but getting a datetime.datetime when I retrieve it instead of `('hello', 'world')`. This happened no matter what other values I put in too -- integers, strings, lists, etc. – sunw Mar 09 '22 at 22:13
  • 2
    But does this example work on its own? Without any of your project in there? Because in your question you say they are "similar to the following" and it doesn't seem like you've tried this without anything else installed (libraries, your own code, etc.) that could be interfering. – Tom Carrick Mar 10 '22 at 09:56
  • Take a look at the [official docs](https://docs.djangoproject.com/en/4.0/topics/cache/). Most of the times they provide thorough description of what we, as dev, are looking for. Ensure you have a correct setup and test manually through django shell before writing an actual automated test. – Gr3at Mar 15 '22 at 21:19
  • E.g. 1. setup cache according to docs. 2. `python manage.py shell` to get a shell. 3. `from django.core.cache import cache` 4. `cache.set('my_key', 'hello, world!', 30)` 5. `cache.get('my_key')`. These steps worked great for me as per the docs. (tested with *DatabaseCache* since i dont have redis sitting around. – Gr3at Mar 15 '22 at 21:25
  • You're using the low level API, so I'd stick to this docu: https://docs.djangoproject.com/en/4.0/topics/cache/#the-low-level-cache-api – David Mar 16 '22 at 07:02
  • Please post a [Short, Self Contained, Correct, Example](http://sscce.org/). If I run your code, I have a function which returns `None`, and no errors. To me it sounds like you are reusing a redis DB that already has a value for the key `"test"`, perhaps because you're not using a testing DB, or because you're running tests in parallel without proper isolation, but without being able to see the issue myself, it's a complete guess. – Adam Barnes Aug 23 '22 at 21:10

0 Answers0