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

amrs-tech
  • 485
  • 2
  • 14
  • 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? – gelonida Dec 29 '19 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 – amrs-tech Dec 30 '19 at 05:01

1 Answers1

3

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")

Yann
  • 2,426
  • 1
  • 16
  • 33
  • 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 ? – amrs-tech Jan 02 '20 at 12:02
  • 1
    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. – Yann Jan 02 '20 at 12:14