# Path: tests/utils.py
# class SharedSchemaTenantsTestCase(TestCase):
# 
#     def setUp(self):
#         self.tenant = mommy.make('shared_schema_tenants.Tenant')
#         set_current_tenant(self.tenant.slug)
#         self.user = User.objects.create_user(
#             first_name='test', last_name='test',
#             username='test', email='test@sharedschematenants.com',
#             password='test')
#         self.relationship = create_relationship(self.tenant, self.user,
#                                                 groups=create_default_tenant_groups())
#         self.tenant_site = mommy.make('shared_schema_tenants.TenantSite',
#                                       tenant=self.tenant)
# 
# Path: shared_schema_tenants_custom_data/models.py
# class TenantSpecificTable(SingleTenantModelMixin):
#     name = models.CharField(max_length=255)
# 
#     class Meta:
#         unique_together = [('tenant', 'name')]
# 
#     def __str__(self):
#         return '%s/%s' % (self.tenant.slug, self.name)
# 
#     @property
#     def fields_definitions(self):
#         return TenantSpecificFieldDefinition.objects.filter(
#             table_content_type=ContentType.objects.get_for_model(TenantSpecificTable),
#             table_id=self.id)
# 
#     def save(self, *args, **kwargs):
#         super(TenantSpecificTable, self).save(*args, **kwargs)
#         tstgroup = TenantSpecificTablesGroup.objects.get(group__name='tenant_owner')
#         tstgroup.permissions.add(TenantSpecificTablesPermission.objects.create(
#             name="add", table=self, codename="add_" + self.name))
#         tstgroup.permissions.add(TenantSpecificTablesPermission.objects.create(
#             name="change", table=self, codename="change_" + self.name))
#         tstgroup.permissions.add(TenantSpecificTablesPermission.objects.create(
#             name="delete", table=self, codename="delete_" + self.name))
# 
# class TenantSpecificFieldDefinition(SingleTenantModelMixin):
#     name = models.CharField(max_length=255)
#     DATA_TYPES = Choices('char', 'text', 'integer', 'float', 'datetime', 'date')
#     data_type = StatusField(choices_name='DATA_TYPES')
#     is_required = models.BooleanField(default=False)
#     default_value = models.TextField()
#     validators = models.ManyToManyField('TenantSpecificFieldsValidator', blank=True)
# 
#     table_content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
#     table_id = models.PositiveIntegerField(blank=True, null=True)
# 
#     class Meta:
#         unique_together = [('tenant', 'table_id', 'table_content_type', 'name')]
# 
#     def __str__(self):
#         content_type = '%s/%s' % (self.tenant.slug, str(self.table_content_type))
#         if content_type == 'shared_schema_tenants.TenantSpecificTable':
#             content_type = str(self.table)
# 
#         return '%s.%s' % (content_type, self.name)
# 
# Path: shared_schema_tenants_custom_data/helpers/custom_tables_helpers.py
# def get_custom_table_manager(table_name):
#     from shared_schema_tenants_custom_data.models import TenantSpecificTable, TenantSpecificTableRow
#     from shared_schema_tenants_custom_data.managers import TenantSpecificTableRowManager
#     table = TenantSpecificTable.objects.get(name=table_name)
#     manager = TenantSpecificTableRowManager()
#     manager.model = TenantSpecificTableRow
#     manager.table_id = table.id
#     return manager
# 
# def _get_pivot_table_class_for_data_type(data_type):
#     from shared_schema_tenants_custom_data.models import (
#         TenantSpecificFieldIntegerPivot,
#         TenantSpecificFieldCharPivot,
#         TenantSpecificFieldTextPivot,
#         TenantSpecificFieldFloatPivot,
#         TenantSpecificFieldDatePivot,
#         TenantSpecificFieldDateTimePivot
#     )
# 
#     if data_type == 'integer':
#         return TenantSpecificFieldIntegerPivot
#     elif data_type == 'char':
#         return TenantSpecificFieldCharPivot
#     elif data_type == 'text':
#         return TenantSpecificFieldTextPivot
#     elif data_type == 'float':
#         return TenantSpecificFieldFloatPivot
#     elif data_type == 'date':
#         return TenantSpecificFieldDatePivot
#     elif data_type == 'datetime':
#         return TenantSpecificFieldDateTimePivot


from model_mommy import mommy
from django.contrib.contenttypes.models import ContentType
from tests.utils import SharedSchemaTenantsTestCase
from shared_schema_tenants_custom_data.models import (
    TenantSpecificTable, TenantSpecificFieldDefinition)
from shared_schema_tenants_custom_data.helpers.custom_tables_helpers import (
    get_custom_table_manager, _get_pivot_table_class_for_data_type)
            self.assertEqual(rows.count(), 0)

    def test_filter_by_tenant_specific_field_with_lookup(self):
        for i, field in enumerate(self.fields):
            rows = get_custom_table_manager(self.table.name).filter(**{field.name + '__gte': i})
            self.assertEqual(rows.count(), 1)

    def test_create_row_with_specific_fields_values(self):
        field_value_dict = {}
        for i, field in enumerate(self.fields):
            field_value_dict[field.name] = i + 50

        get_custom_table_manager(self.table.name).create(**field_value_dict)

        for i, field in enumerate(self.fields):
            rows = get_custom_table_manager(self.table.name).filter(**{field.name: i + 50})
            self.assertEqual(rows.count(), 1)

        self.assertEqual(get_custom_table_manager(self.table.name).all().count(), 2)

    def test_update_row_with_specific_fields_values(self):
        field_value_dict = {}
        for i, field in enumerate(self.fields):
            field_value_dict[field.name] = i + 50

        (get_custom_table_manager(self.table.name)
         .filter(table_id=self.table.id, id=self.row.id)
         .update(**field_value_dict))

        for i, field in enumerate(self.fields):

# Path: tests/utils.py
# class SharedSchemaTenantsTestCase(TestCase):
# 
#     def setUp(self):
#         self.tenant = mommy.make('shared_schema_tenants.Tenant')
#         set_current_tenant(self.tenant.slug)
#         self.user = User.objects.create_user(
#             first_name='test', last_name='test',
#             username='test', email='test@sharedschematenants.com',
#             password='test')
#         self.relationship = create_relationship(self.tenant, self.user,
#                                                 groups=create_default_tenant_groups())
#         self.tenant_site = mommy.make('shared_schema_tenants.TenantSite',
#                                       tenant=self.tenant)
# 
# Path: shared_schema_tenants_custom_data/models.py
# class TenantSpecificTable(SingleTenantModelMixin):
#     name = models.CharField(max_length=255)
# 
#     class Meta:
#         unique_together = [('tenant', 'name')]
# 
#     def __str__(self):
#         return '%s/%s' % (self.tenant.slug, self.name)
# 
#     @property
#     def fields_definitions(self):
#         return TenantSpecificFieldDefinition.objects.filter(
#             table_content_type=ContentType.objects.get_for_model(TenantSpecificTable),
#             table_id=self.id)
# 
#     def save(self, *args, **kwargs):
#         super(TenantSpecificTable, self).save(*args, **kwargs)
#         tstgroup = TenantSpecificTablesGroup.objects.get(group__name='tenant_owner')
#         tstgroup.permissions.add(TenantSpecificTablesPermission.objects.create(
#             name="add", table=self, codename="add_" + self.name))
#         tstgroup.permissions.add(TenantSpecificTablesPermission.objects.create(
#             name="change", table=self, codename="change_" + self.name))
#         tstgroup.permissions.add(TenantSpecificTablesPermission.objects.create(
#             name="delete", table=self, codename="delete_" + self.name))
# 
# class TenantSpecificFieldDefinition(SingleTenantModelMixin):
#     name = models.CharField(max_length=255)
#     DATA_TYPES = Choices('char', 'text', 'integer', 'float', 'datetime', 'date')
#     data_type = StatusField(choices_name='DATA_TYPES')
#     is_required = models.BooleanField(default=False)
#     default_value = models.TextField()
#     validators = models.ManyToManyField('TenantSpecificFieldsValidator', blank=True)
# 
#     table_content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
#     table_id = models.PositiveIntegerField(blank=True, null=True)
# 
#     class Meta:
#         unique_together = [('tenant', 'table_id', 'table_content_type', 'name')]
# 
#     def __str__(self):
#         content_type = '%s/%s' % (self.tenant.slug, str(self.table_content_type))
#         if content_type == 'shared_schema_tenants.TenantSpecificTable':
#             content_type = str(self.table)
# 
#         return '%s.%s' % (content_type, self.name)
# 
# Path: shared_schema_tenants_custom_data/helpers/custom_tables_helpers.py
# def get_custom_table_manager(table_name):
#     from shared_schema_tenants_custom_data.models import TenantSpecificTable, TenantSpecificTableRow
#     from shared_schema_tenants_custom_data.managers import TenantSpecificTableRowManager
#     table = TenantSpecificTable.objects.get(name=table_name)
#     manager = TenantSpecificTableRowManager()
#     manager.model = TenantSpecificTableRow
#     manager.table_id = table.id
#     return manager
# 
# def _get_pivot_table_class_for_data_type(data_type):
#     from shared_schema_tenants_custom_data.models import (
#         TenantSpecificFieldIntegerPivot,
#         TenantSpecificFieldCharPivot,
#         TenantSpecificFieldTextPivot,
#         TenantSpecificFieldFloatPivot,
#         TenantSpecificFieldDatePivot,
#         TenantSpecificFieldDateTimePivot
#     )
# 
#     if data_type == 'integer':
#         return TenantSpecificFieldIntegerPivot
#     elif data_type == 'char':
#         return TenantSpecificFieldCharPivot
#     elif data_type == 'text':
#         return TenantSpecificFieldTextPivot
#     elif data_type == 'float':
#         return TenantSpecificFieldFloatPivot
#     elif data_type == 'date':
#         return TenantSpecificFieldDatePivot
#     elif data_type == 'datetime':
#         return TenantSpecificFieldDateTimePivot


# Path: shared_schema_tenants_custom_data/tests/test_models.py
from model_mommy import mommy
from django.contrib.contenttypes.models import ContentType
from tests.utils import SharedSchemaTenantsTestCase
from shared_schema_tenants_custom_data.models import (
    TenantSpecificTable, TenantSpecificFieldDefinition)
from shared_schema_tenants_custom_data.helpers.custom_tables_helpers import (
    get_custom_table_manager, _get_pivot_table_class_for_data_type)

            self.assertEqual(rows.count(), 0)

    def test_filter_by_tenant_specific_field_with_lookup(self):
        for i, field in enumerate(self.fields):
            rows = get_custom_table_manager(self.table.name).filter(**{field.name + '__gte': i})
            self.assertEqual(rows.count(), 1)

    def test_create_row_with_specific_fields_values(self):
        field_value_dict = {}
        for i, field in enumerate(self.fields):
            field_value_dict[field.name] = i + 50

        get_custom_table_manager(self.table.name).create(**field_value_dict)

        for i, field in enumerate(self.fields):
            rows = get_custom_table_manager(self.table.name).filter(**{field.name: i + 50})
            self.assertEqual(rows.count(), 1)

        self.assertEqual(get_custom_table_manager(self.table.name).all().count(), 2)

    def test_update_row_with_specific_fields_values(self):
        field_value_dict = {}
        for i, field in enumerate(self.fields):
            field_value_dict[field.name] = i + 50

        (get_custom_table_manager(self.table.name)
         .filter(table_id=self.table.id, id=self.row.id)
         .update(**field_value_dict))

        for i, field in enumerate(self.fields):

            rows = get_custom_table_manager(self.table.name).filter(**{field.name: i + 50})