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

        set_current_tenant(self.tenant_1.slug)
        self.tags_t1 = mommy.make(Tag, _quantity=5)
        self.shared_tags = mommy.make(Tag, tenants=[self.tenant_1, self.tenant_2],
                                      _quantity=7)
        set_current_tenant(self.tenant_2.slug)
        self.tags_t2 = mommy.make(Tag, _quantity=3)
        for tag in self.shared_tags:
            tag.save()

        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.assertIn(self.tenant_1, tag.tenants.all())

    def test_create_raise_exception_if_no_tenant_set_or_passed(self):
        clear_current_tenant()

# 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

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

        set_current_tenant(self.tenant_1.slug)
        self.tags_t1 = mommy.make(Tag, _quantity=5)
        self.shared_tags = mommy.make(Tag, tenants=[self.tenant_1, self.tenant_2],
                                      _quantity=7)
        set_current_tenant(self.tenant_2.slug)
        self.tags_t2 = mommy.make(Tag, _quantity=3)
        for tag in self.shared_tags:
            tag.save()

        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.assertIn(self.tenant_1, tag.tenants.all())

    def test_create_raise_exception_if_no_tenant_set_or_passed(self):
        clear_current_tenant()

        with self.assertRaises(TenantNotFoundError):