4

I have multi-tenant applications in django with postgresql schemas. I tried to write test code inside a tenant folder "t002" as:

from django.test import TestCase
from path.customers.models import Client
# TestClass to test tenant setup
class TestTenantSetup(TestCase):
    def setUp(self):
        Client.objects.create(
            name='t002',
            schema_name='t002', 
            domain_url='t002.cmpny.com'
            )

    def test_tenant_exists(self):
        client = Client.objects.get(name='t002')
        self.assertEquals(client.schema_name, "t002")

But when I ran the test with python manage.py test path/t002/tests it was creating a test database for my actual database. So let's say I would follow this SO answer (django unit tests without a db) and avoid creating database, but I am not sure if I'm following the right path to test a multi-tenant django project. When I looked into the django-tenant-schemas docs (https://django-tenant-schemas.readthedocs.io/en/latest/test.html) I couldn't get my hands on it easily. Can anybody tell how start doing test for django multi-tenant applications ? And please do review the above code too whether it is right or wrong.

Specs - python==2.7, django==1.11, postgres==9.6

2
  • Could you please add additional information to your question. It seems you don't want a test database to be created. What would you like instead? Let the test use the real data base and create new rows there? Commented Dec 29, 2019 at 23:22
  • Yes that part, I already checked an SO answer, but basically how to start unittest for a django multi-tenant application is my question. Above snippet is the one I had tried to test for tenant setup, is that the right way ? or if I should follow some other ? I mainly ask for multi-tenancy @gelonida Commented Dec 30, 2019 at 5:01

1 Answer 1

3
+25

Depending on what soft of isolation do you use in your multi-tenant application.

If you use different database settings or any other settings you can override their test version in you tests with @django.test.override_settings.

For example:

from django.test import override_settings

@override_settings(DATABASES=<...>)  # you can also load and/or override other settings for a specific application
class TestTenantSetup(TestCase):
    def setUp(self):
        Client.objects.create(
            name='t002',
            schema_name='t002', 
            domain_url='t002.cmpny.com'
            )

    def test_tenant_exists(self):
        client = Client.objects.get(name='t002')
        self.assertEquals(client.schema_name, "t002")

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I mentioned that there's a solution for overriding DB creation. But what I asked for is, whether we should proceed like normal Django unit testing or is there some other way to do testing for multi-tenant system ?
Yes, treat it as a normal Django app. In general it's better to keep the tests simple and isolated. If you need to test some interactions between the apps inside you Django app then create a test settings. it should be close to what you use in reality but doesn't require dependencies that are out of the scope of what you are testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.