Skip to content

Commit

Permalink
Merge pull request #390 from filipeximenes/fast-testcase
Browse files Browse the repository at this point in the history
Added FastTenantTestCase and its documentation
  • Loading branch information
bernardopires committed Oct 4, 2016
2 parents 906160f + 016e268 commit ba73de4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ Because django will not create tenants for you during your tests, we have packed
response = self.c.get(reverse('user_profile'))
self.assertEqual(response.status_code, 200)
Running tests faster
--------------------
Using the ``TenantTestCase`` can make running your tests really slow quite early in your project. This is due to the fact that it drops, recreates the test schema and runs migrations for every ``TenantTestCase`` you have. If you want to gain speed, there's a ``FastTenantTestCase`` where the test schema will be created and migrations ran only one time. The gain in speed is noticiable but be aware that by using this you will be perpertraiting state between your test cases, please make sure your they wont be affected by this.

Running tests using ``TenantTestCase`` can start being a bottleneck once the number of tests grow. If you do not care that the state between tests is kept, an alternative is to use the class ``FastTenantTestCase``. Unlike ``TenantTestCase``, the test schema and its migrations will only be created and ran once. This is a significant improvement in speed coming at the cost of shared state.

.. code-block:: python
from tenant_schemas.test.cases import FastTenantTestCase
.. _tox: https://tox.readthedocs.io/
21 changes: 21 additions & 0 deletions tenant_schemas/test/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ def sync_shared(cls):
schema_name=get_public_schema_name(),
interactive=False,
verbosity=0)


class FastTenantTestCase(TenantTestCase):

@classmethod
def setUpClass(cls):
cls.sync_shared()
tenant_domain = 'tenant.test.com'

TenantModel = get_tenant_model()
try:
cls.tenant = TenantModel.objects.get(domain_url=tenant_domain, schema_name='test')
except:
cls.tenant = TenantModel(domain_url=tenant_domain, schema_name='test')
cls.tenant.save(verbosity=0)

connection.set_tenant(cls.tenant)

@classmethod
def tearDownClass(cls):
connection.set_schema_to_public()

0 comments on commit ba73de4

Please sign in to comment.