Add cache key helper functions and tests

This commit is contained in:
Carl Robben 2015-07-16 22:36:50 +12:00
parent 9d85507417
commit c5fa32f8e9
3 changed files with 34 additions and 0 deletions

20
tenant_schemas/cache.py Normal file
View File

@ -0,0 +1,20 @@
from django.db import connection
def make_key(key, key_prefix, version):
"""
Tenant aware function to generate a cache key.
Constructs the key used by all other methods. Prepends the tenant
`schema_name` and `key_prefix'.
"""
return '%s:%s:%s:%s' % (connection.schema_name, key_prefix, version, key)
def reverse_key(key):
"""
Tenant aware function to reverse a cache key.
Required for django-redis REVERSE_KEY_FUNCTION setting.
"""
return key.split(':', 3)[3]

View File

@ -1,2 +1,3 @@
from test_routes import *
from test_tenants import *
from test_cache import *

View File

@ -0,0 +1,13 @@
from tenant_schemas.cache import make_key, reverse_key
from tenant_schemas.test.cases import TenantTestCase
class CacheHelperTestCase(TenantTestCase):
def test_make_key(self):
key = make_key(key='foo', key_prefix='', version=1)
tenant_prefix = key.split(':')[0]
self.assertEqual(self.tenant.schema_name, tenant_prefix)
def test_reverse_key(self):
key = 'foo'
self.assertEqual(key, reverse_key(make_key(key=key, key_prefix='', version=1)))