Python django.db.migrations.AddConstraint() Examples

The following are 2 code examples of django.db.migrations.AddConstraint(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.migrations , or try the search function .
Example #1
Source File: test_operations.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_add_constraint(self):
        project_state = self.set_up_test_model("test_addconstraint")
        gt_check = models.Q(pink__gt=2)
        gt_constraint = models.CheckConstraint(check=gt_check, name="test_add_constraint_pony_pink_gt_2")
        gt_operation = migrations.AddConstraint("Pony", gt_constraint)
        self.assertEqual(
            gt_operation.describe(), "Create constraint test_add_constraint_pony_pink_gt_2 on model Pony"
        )
        # Test the state alteration
        new_state = project_state.clone()
        gt_operation.state_forwards("test_addconstraint", new_state)
        self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 1)
        Pony = new_state.apps.get_model("test_addconstraint", "Pony")
        self.assertEqual(len(Pony._meta.constraints), 1)
        # Test the database alteration
        with connection.schema_editor() as editor:
            gt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
        with self.assertRaises(IntegrityError), transaction.atomic():
            Pony.objects.create(pink=1, weight=1.0)
        # Add another one.
        lt_check = models.Q(pink__lt=100)
        lt_constraint = models.CheckConstraint(check=lt_check, name="test_add_constraint_pony_pink_lt_100")
        lt_operation = migrations.AddConstraint("Pony", lt_constraint)
        lt_operation.state_forwards("test_addconstraint", new_state)
        self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 2)
        Pony = new_state.apps.get_model("test_addconstraint", "Pony")
        self.assertEqual(len(Pony._meta.constraints), 2)
        with connection.schema_editor() as editor:
            lt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
        with self.assertRaises(IntegrityError), transaction.atomic():
            Pony.objects.create(pink=100, weight=1.0)
        # Test reversal
        with connection.schema_editor() as editor:
            gt_operation.database_backwards("test_addconstraint", editor, new_state, project_state)
        Pony.objects.create(pink=1, weight=1.0)
        # Test deconstruction
        definition = gt_operation.deconstruct()
        self.assertEqual(definition[0], "AddConstraint")
        self.assertEqual(definition[1], [])
        self.assertEqual(definition[2], {'model_name': "Pony", 'constraint': gt_constraint}) 
Example #2
Source File: test_operations.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_add_partial_unique_constraint(self):
        project_state = self.set_up_test_model('test_addpartialuniqueconstraint')
        partial_unique_constraint = models.UniqueConstraint(
            fields=['pink'],
            condition=models.Q(weight__gt=5),
            name='test_constraint_pony_pink_for_weight_gt_5_uniq',
        )
        operation = migrations.AddConstraint('Pony', partial_unique_constraint)
        self.assertEqual(
            operation.describe(),
            'Create constraint test_constraint_pony_pink_for_weight_gt_5_uniq '
            'on model Pony'
        )
        # Test the state alteration
        new_state = project_state.clone()
        operation.state_forwards('test_addpartialuniqueconstraint', new_state)
        self.assertEqual(len(new_state.models['test_addpartialuniqueconstraint', 'pony'].options['constraints']), 1)
        Pony = new_state.apps.get_model('test_addpartialuniqueconstraint', 'Pony')
        self.assertEqual(len(Pony._meta.constraints), 1)
        # Test the database alteration
        with connection.schema_editor() as editor:
            operation.database_forwards('test_addpartialuniqueconstraint', editor, project_state, new_state)
        # Test constraint works
        Pony.objects.create(pink=1, weight=4.0)
        Pony.objects.create(pink=1, weight=4.0)
        Pony.objects.create(pink=1, weight=6.0)
        if connection.features.supports_partial_indexes:
            with self.assertRaises(IntegrityError), transaction.atomic():
                Pony.objects.create(pink=1, weight=7.0)
        else:
            Pony.objects.create(pink=1, weight=7.0)
        # Test reversal
        with connection.schema_editor() as editor:
            operation.database_backwards('test_addpartialuniqueconstraint', editor, new_state, project_state)
        # Test constraint doesn't work
        Pony.objects.create(pink=1, weight=7.0)
        # Test deconstruction
        definition = operation.deconstruct()
        self.assertEqual(definition[0], 'AddConstraint')
        self.assertEqual(definition[1], [])
        self.assertEqual(definition[2], {'model_name': 'Pony', 'constraint': partial_unique_constraint})