3

Whats is the best way to check if I have setup caching correctly for TastyPie? I have followed the documentation on this:

In settings added this:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'TIMEOUT': 60
    },
    'resources': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'TIMEOUT': 60
    }
}

In my resource added:

class IncentiveResource(ModelResource):

    class Meta:
        queryset = Incentive.objects.all()
        resource_name = 'incentive'
        allowed_methods = ['get']
        always_return_data = True
        cache = SimpleCache(cache_name='resources', timeout=10)
Prometheus
  • 32,405
  • 54
  • 166
  • 302

2 Answers2

4

Just write and run a simple test to check that your resources cache is working:

from django.test import TestCase
from tastypie.cache import SimpleCache


class CacheTestCase(TestCase):
    cache_name = 'resources'

    def test_cache(self):
        simple_cache = SimpleCache(cache_name=self.cache_name)
        simple_cache.set('foo', 'bar', 60)
        simple_cache.set('moof', 'baz', 1)

        self.assertEqual(simple_cache.get('foo'), 'bar')
        self.assertEqual(simple_cache.get('moof'), 'baz')
        self.assertEqual(simple_cache.get(''), None)

Partially taken from the tastypie tests with some modifications.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • great answer, unit testing is definitely the correct concept to refer to here, and the example is very informative. – fr1tz Apr 22 '14 at 02:59
  • While the concept is good, the execution could be better: the test names are non-descriptive; they could be swapped without loss of any semantics... And there are too many assertions in one test. Like the `get('')` should be in its own test `test_get_non_existent_returns_none` or something similar. – Chris Wesseling Apr 23 '14 at 06:17
  • @ChrisWesseling yup, there is a space for improvement, actually, there always is :) Thank you for the point. – alecxe Apr 23 '14 at 10:02
  • Furthermore if these come from the tastypie suite, why add them to your own suite instead of just running the tastypie suite? Don't test the platform... Repeating tests from the Python, Django and TastyPie suites is a waste of resources that could be spent on your own code. Ofcourse if you *do* find gaps in the platform suites, contribute back upstream. – Chris Wesseling Apr 23 '14 at 10:22
  • Repeating the TastyPie unittests doesn't show that the config is good. It just shows the TastyPie units work. – Chris Wesseling Apr 23 '14 at 13:07
  • @ChrisWesseling `SimpleCache` uses the cache settings specified in `django.conf.settings` - this means that the actual cache settings are tested. – alecxe Apr 23 '14 at 13:10
  • Then shouldn't a `manage.py test` test all `INSTALLED_APPS` including the tastypie ones? No need to repeat it. – Chris Wesseling Apr 23 '14 at 13:17
  • @ChrisWesseling nope, it wouldn't test your particular `resources` cache, only the `default` one. – alecxe Apr 23 '14 at 13:21
  • After closer inspection, without extra discovery, it wouldn't run the test at all; it lives outside of the app module that gets installed. – Chris Wesseling Apr 23 '14 at 13:30
  • @ChrisWesseling ok, but anyway, making your own test like this really helps to test the `resources` cache. Thanks for the feedback, by the way. – alecxe Apr 23 '14 at 13:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51279/discussion-between-chris-wesseling-and-alecxe) – Chris Wesseling Apr 23 '14 at 13:32
0

Given you followed the cache docs to the letter And tastypie has an extensive testsuite When their CI builds succeed Then I would expect it to work.

To double check you could:

Pick a resource you think should be cached and pound its URL with a tool like ab. If your config is right, your database load shouldn't go up. You could put this in a last step of your deployment scripts together with the other tests that test your whole stack.

Or put it in a tiny test in your project, like alecxe suggests, that just asserts that your configured cache is indeed used.

Community
  • 1
  • 1
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72