From 043871c4d6dd9e8c90e29faa288ba0b5db903ecb Mon Sep 17 00:00:00 2001 From: Alexander van Eck Date: Tue, 3 Jan 2017 17:51:45 +0100 Subject: [PATCH] #425: Added force_create and force_drop/force_create tests. --- tenant_schemas/models.py | 4 ++-- tenant_schemas/test/cases.py | 2 +- tenant_schemas/tests/test_tenants.py | 30 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/tenant_schemas/models.py b/tenant_schemas/models.py index 7907ac71..bc08208c 100644 --- a/tenant_schemas/models.py +++ b/tenant_schemas/models.py @@ -31,7 +31,7 @@ class TenantMixin(models.Model): class Meta: abstract = True - def save(self, verbosity=1, *args, **kwargs): + def save(self, verbosity=1, force_create=False, *args, **kwargs): is_new = self.pk is None if is_new and connection.schema_name != get_public_schema_name(): @@ -44,7 +44,7 @@ def save(self, verbosity=1, *args, **kwargs): super(TenantMixin, self).save(*args, **kwargs) - if is_new and self.auto_create_schema: + if is_new and (self.auto_create_schema or force_create): try: self.create_schema(check_if_exists=True, verbosity=verbosity) except: diff --git a/tenant_schemas/test/cases.py b/tenant_schemas/test/cases.py index a6b2e292..2f52abfa 100644 --- a/tenant_schemas/test/cases.py +++ b/tenant_schemas/test/cases.py @@ -12,7 +12,7 @@ def setUpClass(cls): cls.sync_shared() tenant_domain = 'tenant.test.com' cls.tenant = get_tenant_model()(domain_url=tenant_domain, schema_name='test') - cls.tenant.save(verbosity=0) # todo: is there any way to get the verbosity from the test command here? + cls.tenant.save(verbosity=0, force_create=True) # todo: is there any way to get the verbosity from the test command here? connection.set_tenant(cls.tenant) diff --git a/tenant_schemas/tests/test_tenants.py b/tenant_schemas/tests/test_tenants.py index 5dd27e73..ff7dc11d 100644 --- a/tenant_schemas/tests/test_tenants.py +++ b/tenant_schemas/tests/test_tenants.py @@ -55,6 +55,17 @@ def test_non_auto_sync_tenant(self): tenant.save(verbosity=BaseTestCase.get_verbosity()) self.assertFalse(schema_exists(tenant.schema_name)) + def test_tenant_schema_is_force_created(self): + """ + When saving a tenant with force_create=True and has auto_create_schema + as False, the schema should be created when saving the tenant. + """ + self.assertFalse(schema_exists('force_create_tenant')) + tenant = NonAutoSyncTenant(domain_url='something.test.com', + schema_name='force_create_tenant') + tenant.save(verbosity=BaseTestCase.get_verbosity(), force_create=True) + self.assertTrue(schema_exists(tenant.schema_name)) + def test_sync_tenant(self): """ When editing an existing tenant, all data should be kept. @@ -79,6 +90,25 @@ def test_sync_tenant(self): # test if data is still there self.assertEqual(DummyModel.objects.count(), 2) + def test_force_drop_schema(self): + """ + When deleting a tenant with force_drop=True, it should delete + the schema associated with the tenant. Regardless of auto_drop_schema + """ + self.assertFalse(schema_exists('auto_drop_tenant')) + Tenant.auto_drop_schema = False + tenant = Tenant(domain_url='something.test.com', + schema_name='auto_drop_tenant') + tenant.save(verbosity=BaseTestCase.get_verbosity()) + self.assertTrue(schema_exists(tenant.schema_name)) + cursor = connection.cursor() + + # Force pending trigger events to be executed + cursor.execute('SET CONSTRAINTS ALL IMMEDIATE') + + tenant.delete(force_drop=True) + self.assertFalse(schema_exists(tenant.schema_name)) + def test_auto_drop_schema(self): """ When deleting a tenant with auto_drop_schema=True, it should delete