Skip to content

Commit

Permalink
Add multi tenant template tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikprabhu committed Aug 6, 2017
1 parent ac3c447 commit 9f59213
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tenant_schemas/template_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def __init__(self, engine, loaders):
def cache_key(template_name, template_dirs):
if connection.tenant:
if not template_dirs:
template_dirs = settings.MULTITENANT_TEMPLATE_DIRS
try:
template_dirs = settings.MULTITENANT_TEMPLATE_DIRS
except AttributeError:
raise ImproperlyConfigured('To use %s.%s you must define the MULTITENANT_TEMPLATE_DIRS' %
(__name__, CachedLoader.__name__))
return '-'.join([str(connection.tenant.pk), template_name,
hashlib.sha1(force_bytes('|'.join(template_dirs))).hexdigest()])
if template_dirs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Tenant! (Django templates)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from django.template.loader import get_template
from django.test import SimpleTestCase, override_settings
from django.db import connection

from tenant_schemas.tests.models import Tenant


@override_settings(
Expand All @@ -23,9 +26,18 @@
]
},
}
],
MULTITENANT_TEMPLATE_DIRS = [
os.path.join(os.path.dirname(__file__), "templates")
]
)
class CachedLoaderTests(SimpleTestCase):
def test_get_template(self):
template = get_template("hello.html")
self.assertEqual(template.render(), "Hello! (Django templates)\n")

def test_get_tenant_template(self):
connection.tenant = Tenant(domain_url="tenant", schema_name="tenant")
connection..tenant.save()
template = get_template("hello.html")
self.assertEqual(template.render(), "Hello Tenant! (Django templates)\n")

0 comments on commit 9f59213

Please sign in to comment.