# Path: exampleproject/articles/models.py
# class Article(SingleTenantModelMixin):
#     title = models.CharField(max_length=100)
#     text = models.TextField()
#     tags = models.ManyToManyField('Tag')
#     author = models.ForeignKey(settings.AUTH_USER_MODEL)
# 
#     def __str__(self):
#         return '%s - %s' % (self.title, str(self.author))
# 
# class Tag(MultipleTenantsModelMixin):
#     text = models.CharField(max_length=100)
# 
#     def __str__(self):
#         return self.text
# 
# Path: shared_schema_tenants/helpers/tenants.py
# def create_tenant(name, slug, extra_data, domains=[], user=None):
#     from shared_schema_tenants.models import Tenant
#     with transaction.atomic():
#         tenant = Tenant.objects.create(
#             name=name, slug=slug, extra_data=extra_data)
# 
#         if len(domains) > 0:
#             for domain in domains:
#                 site = Site.objects.create(name=name, domain=domain)
#                 tenant.tenant_sites.create(site=site)
# 
#         if user:
#             rel = tenant.relationships.create(user=user)
#             rel.groups.add(create_default_tenant_groups()[0])
# 
#         return tenant
# 
# def set_current_tenant(tenant_slug):
#     from shared_schema_tenants.middleware import TenantMiddleware
#     return TenantMiddleware.set_tenant(tenant_slug)
# 
# def clear_current_tenant():
#     from shared_schema_tenants.middleware import TenantMiddleware
#     return TenantMiddleware.clear_tenant()
# 
# Path: shared_schema_tenants/exceptions.py
# class TenantNotFoundError(Exception):
# 
#     def __init__(self, message="Tenant with this slug couldn't be found", errors=[]):
#         super(TenantNotFoundError, self).__init__(message)


from django.test import TestCase
from model_mommy import mommy
from exampleproject.articles.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
import django.utils.version
                                               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.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={})

# Path: exampleproject/articles/models.py
# class Article(SingleTenantModelMixin):
#     title = models.CharField(max_length=100)
#     text = models.TextField()
#     tags = models.ManyToManyField('Tag')
#     author = models.ForeignKey(settings.AUTH_USER_MODEL)
# 
#     def __str__(self):
#         return '%s - %s' % (self.title, str(self.author))
# 
# class Tag(MultipleTenantsModelMixin):
#     text = models.CharField(max_length=100)
# 
#     def __str__(self):
#         return self.text
# 
# Path: shared_schema_tenants/helpers/tenants.py
# def create_tenant(name, slug, extra_data, domains=[], user=None):
#     from shared_schema_tenants.models import Tenant
#     with transaction.atomic():
#         tenant = Tenant.objects.create(
#             name=name, slug=slug, extra_data=extra_data)
# 
#         if len(domains) > 0:
#             for domain in domains:
#                 site = Site.objects.create(name=name, domain=domain)
#                 tenant.tenant_sites.create(site=site)
# 
#         if user:
#             rel = tenant.relationships.create(user=user)
#             rel.groups.add(create_default_tenant_groups()[0])
# 
#         return tenant
# 
# def set_current_tenant(tenant_slug):
#     from shared_schema_tenants.middleware import TenantMiddleware
#     return TenantMiddleware.set_tenant(tenant_slug)
# 
# def clear_current_tenant():
#     from shared_schema_tenants.middleware import TenantMiddleware
#     return TenantMiddleware.clear_tenant()
# 
# Path: shared_schema_tenants/exceptions.py
# class TenantNotFoundError(Exception):
# 
#     def __init__(self, message="Tenant with this slug couldn't be found", errors=[]):
#         super(TenantNotFoundError, self).__init__(message)


# Path: shared_schema_tenants/tests/test_managers.py
from django.test import TestCase
from model_mommy import mommy
from exampleproject.articles.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
import django.utils.version

                                               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.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={})