# Path: boardinghouse/models.py
# class Schema(AbstractSchema):
#     """
#     The default schema model.
# 
#     Unless you set `settings.BOARDINGHOUSE_SCHEMA_MODEL`, this model will
#     be used for storing the schema objects.
#     """
#     users = models.ManyToManyField(
#         settings.AUTH_USER_MODEL,
#         blank=True, related_name='schemata',
#         help_text=_(u'Which users may access data from this schema.')
#     )
# 
#     class Meta:
#         app_label = 'boardinghouse'
#         verbose_name_plural = 'schemata'
#         swappable = 'BOARDINGHOUSE_SCHEMA_MODEL'
# 
# Path: tests/models.py
# class AwareModel(models.Model):
#     name = models.CharField(max_length=10, unique=True)
#     status = models.BooleanField(default=False)
#     factor = models.PositiveSmallIntegerField(default=7)
# 
#     objects = MultiSchemaManager()
# 
# class ViewBackedModel(models.Model):
#     class Meta:
#         managed = False


from django.test import TestCase
from django.db.migrations.state import ProjectState
from django.db import connection, migrations, transaction
from boardinghouse.models import Schema
from ..models import AwareModel, ViewBackedModel
            sql=TRIGGER_TRIGGER,
            reverse_sql='DROP TRIGGER "test_trigger_this" BEFORE EACH UPDATE ON tests_awaremodel'
        )

        trigger_function_op.state_forwards('tests', new_state)
        trigger_op.state_forwards('tests', new_state)

        with connection.schema_editor() as editor:
            trigger_function_op.database_forwards('tests', editor, project_state, new_state)
            trigger_op.database_forwards('tests', editor, project_state, new_state)

        Schema.objects.mass_create('a', 'b', 'c')

        for schema in Schema.objects.all():
            schema.activate()
            with transaction.atomic():
                with self.assertRaises(Exception) as exc:
                    self.assertTrue('Trigger fired correctly' == exc.args[0])
                    AwareModel.objects.create(name='FAILCREATE')

    def test_cloning_template_with_view(self):
        schema = Schema.objects.create(name='a', schema='a')
        schema.activate()

        self.assertEqual(1000, ViewBackedModel.objects.count())

    def test_cloning_template_with_function(self):
        with connection.cursor() as cursor:
            cursor.execute("CREATE FUNCTION __template__.spam_and_eggs() RETURNS BOOLEAN AS 'SELECT true' LANGUAGE SQL")


# Path: boardinghouse/models.py
# class Schema(AbstractSchema):
#     """
#     The default schema model.
# 
#     Unless you set `settings.BOARDINGHOUSE_SCHEMA_MODEL`, this model will
#     be used for storing the schema objects.
#     """
#     users = models.ManyToManyField(
#         settings.AUTH_USER_MODEL,
#         blank=True, related_name='schemata',
#         help_text=_(u'Which users may access data from this schema.')
#     )
# 
#     class Meta:
#         app_label = 'boardinghouse'
#         verbose_name_plural = 'schemata'
#         swappable = 'BOARDINGHOUSE_SCHEMA_MODEL'
# 
# Path: tests/models.py
# class AwareModel(models.Model):
#     name = models.CharField(max_length=10, unique=True)
#     status = models.BooleanField(default=False)
#     factor = models.PositiveSmallIntegerField(default=7)
# 
#     objects = MultiSchemaManager()
# 
# class ViewBackedModel(models.Model):
#     class Meta:
#         managed = False


# Path: tests/tests/test_clone_schema_db_function.py
from django.test import TestCase
from django.db.migrations.state import ProjectState
from django.db import connection, migrations, transaction
from boardinghouse.models import Schema
from ..models import AwareModel, ViewBackedModel

            sql=TRIGGER_TRIGGER,
            reverse_sql='DROP TRIGGER "test_trigger_this" BEFORE EACH UPDATE ON tests_awaremodel'
        )

        trigger_function_op.state_forwards('tests', new_state)
        trigger_op.state_forwards('tests', new_state)

        with connection.schema_editor() as editor:
            trigger_function_op.database_forwards('tests', editor, project_state, new_state)
            trigger_op.database_forwards('tests', editor, project_state, new_state)

        Schema.objects.mass_create('a', 'b', 'c')

        for schema in Schema.objects.all():
            schema.activate()
            with transaction.atomic():
                with self.assertRaises(Exception) as exc:
                    self.assertTrue('Trigger fired correctly' == exc.args[0])
                    AwareModel.objects.create(name='FAILCREATE')

    def test_cloning_template_with_view(self):
        schema = Schema.objects.create(name='a', schema='a')
        schema.activate()

        self.assertEqual(1000, ViewBackedModel.objects.count())

    def test_cloning_template_with_function(self):
        with connection.cursor() as cursor:
            cursor.execute("CREATE FUNCTION __template__.spam_and_eggs() RETURNS BOOLEAN AS 'SELECT true' LANGUAGE SQL")


        schema = Schema.objects.create(name='func', schema='func')