Skip to content

Commit

Permalink
create tests for MultipleTenantModelManager and fixes some SingleTena…
Browse files Browse the repository at this point in the history
…ntModelManager tests
  • Loading branch information
hugobessa committed Aug 29, 2017
1 parent 81ceaaf commit 2ebad29
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
3 changes: 1 addition & 2 deletions shared_schema_tenants/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def create(self, tenant=None, *args, **kwargs):
else:
raise TenantNotFoundError()
else:
model_instance, created = super(
MultipleTenantModelManager, self).create(*args, **kwargs)
model_instance = super(MultipleTenantModelManager, self).create(*args, **kwargs)
model_instance.tenants.add(tenant)
return model_instance
8 changes: 5 additions & 3 deletions shared_schema_tenants/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class SingleTenantModelMixin(models.Model):
objects = models.Manager()
else:
objects = SingleTenantModelManager()
original_manager = models.Manager()

original_manager = models.Manager()
tenant_objects = SingleTenantModelManager()

class Meta:
Expand All @@ -32,9 +33,10 @@ class MultipleTenantsModelMixin(models.Model):
if django.utils.version.get_complete_version()[1] < 10:
objects = models.Manager()
else:
objects = SingleTenantModelManager()
original_manager = models.Manager()
objects = MultipleTenantModelManager()

tenant_objects = MultipleTenantModelManager()
original_manager = models.Manager()

class Meta:
abstract = True
Expand Down
89 changes: 85 additions & 4 deletions tests/test_managers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.test import TestCase
import django.utils.version
from model_mommy import mommy
from exampleproject.exampleapp.models import Article
from shared_schema_tenants.helpers.tenants import create_tenant, set_current_tenant
from exampleproject.exampleapp.models import Article, Tag
from shared_schema_tenants.helpers.tenants import create_tenant, set_current_tenant, clear_current_tenant
from shared_schema_tenants.exceptions import TenantNotFoundError


class SingleTenantModelManagerTests(TestCase):
Expand All @@ -14,7 +15,7 @@ def setUp(self):
self.articles_t1 = mommy.make(Article, tenant=self.tenant_1, _quantity=5)
self.articles_t2 = mommy.make(Article, tenant=self.tenant_2, _quantity=3)

set_current_tenant(self.tenant_1)
set_current_tenant(self.tenant_1.slug)

self.articles_manager = Article.objects
if django.utils.version.get_complete_version()[1] < 10:
Expand All @@ -26,18 +27,98 @@ def test_create(self):

self.assertEqual(article.tenant, self.tenant_1)

def test_create_raise_exception_if_no_tenant_set_or_passed(self):
clear_current_tenant()
user = mommy.make('auth.User')
with self.assertRaises(TenantNotFoundError):
self.articles_manager.create(title="Test Article", text="Test Article Description", author=user)

def test_create_passing_tenant(self):
user = mommy.make('auth.User')
article = self.articles_manager.create(tenant=self.tenant_1, title="Test Article",
text="Test Article Description", author=user)

self.assertEqual(article.tenant, self.tenant_1)

def test_list(self):
self.assertEqual(self.articles_manager.count(), self.tenant_1.article_set.count())
set_current_tenant(self.tenant_2)
set_current_tenant(self.tenant_2.slug)
self.assertEqual(self.articles_manager.count(), self.tenant_2.article_set.count())

def test_list_passing_tenant_to_get_queryset(self):
self.assertEqual(
self.articles_manager.get_queryset(tenant=self.tenant_1).all().count(),
self.tenant_1.article_set.all().count())
self.assertEqual(
self.articles_manager.get_queryset(tenant=self.tenant_2).all().count(),
self.articles_manager.get_queryset(tenant=self.tenant_2).all().count(),
self.tenant_2.article_set.all().count())

def test_list_original_queryset(self):
self.assertEqual(self.articles_manager.get_original_queryset().all().count(), 8)

def test_return_nothing_if_no_tenant_set_or_passed(self):
clear_current_tenant()
self.assertEqual(self.articles_manager.all().count(), 0)


class MultipleTenantModelManagerTests(TestCase):

def setUp(self):
self.tenant_1 = create_tenant(name='tenant_1', slug='tenant_1', extra_data={})
self.tenant_2 = create_tenant(name='tenant_2', slug='tenant_2', extra_data={})

self.tags_t1 = mommy.make(Tag, tenants=[self.tenant_1], _quantity=5)
self.tags_t2 = mommy.make(Tag, tenants=[self.tenant_2], _quantity=3)
self.shared_tags = mommy.make(Tag, tenants=[self.tenant_1, self.tenant_2],
_quantity=7)

set_current_tenant(self.tenant_1.slug)

self.tags_manager = Tag.objects
if django.utils.version.get_complete_version()[1] < 10:
self.tags_manager = Tag.tenant_objects

def test_create(self):
tag = self.tags_manager.create(text="Test tag")
self.assertTrue(self.tenant_1 in tag.tenants.all())

def test_create_same_tag_two_tenants(self):
tag = self.tags_manager.create(text="Test tag")

set_current_tenant(self.tenant_2.slug)
tag = self.tags_manager.create(text="Test tag")

self.assertEqual(tag.tenants.all().count(), 2)
self.assertEqual(Tag.original_manager.filter(text="Test tag").count(), 1)

def test_create_raise_exception_if_no_tenant_set_or_passed(self):
clear_current_tenant()
with self.assertRaises(TenantNotFoundError):
self.tags_manager.create(text="Test tag")

def test_create_passing_tenant(self):
tag = self.tags_manager.create(tenant=self.tenant_2, text="Test tag")
self.assertTrue(self.tenant_2 in tag.tenants.all())
self.assertEqual(tag.tenants.count(), 1)

def test_list(self):
self.assertEqual(self.tags_manager.count(), len(self.tags_t1) + len(self.shared_tags))
set_current_tenant(self.tenant_2)
self.assertEqual(self.tags_manager.count(), len(self.tags_t2) + len(self.shared_tags))

def test_list_passing_tenant_to_get_queryset(self):
self.assertEqual(
self.tags_manager.get_queryset(tenant=self.tenant_1).all().count(),
len(self.tags_t1) + len(self.shared_tags))
self.assertEqual(
self.tags_manager.get_queryset(tenant=self.tenant_2).all().count(),
len(self.tags_t2) + len(self.shared_tags))

def test_list_original_queryset(self):
self.assertEqual(self.tags_manager.get_original_queryset().all().count(),
len(self.tags_t1) + len(self.tags_t2) + len(self.shared_tags))

def test_return_nothing_if_no_tenant_set_or_passed(self):
clear_current_tenant()
self.assertEqual(self.tags_manager.all().count(), 0)

0 comments on commit 2ebad29

Please sign in to comment.