|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT license. |
3 | 3 |
|
| 4 | +from django.db import connections, migrations, models |
| 5 | +from django.db.migrations.state import ProjectState |
4 | 6 | from django.db.utils import IntegrityError |
5 | | -from django.test import TestCase, skipUnlessDBFeature |
| 7 | +from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature |
6 | 8 |
|
| 9 | +from mssql.base import DatabaseWrapper |
7 | 10 | from ..models import ( |
8 | | - Author, Editor, Post, |
9 | | - TestUniqueNullableModel, TestNullableUniqueTogetherModel, |
| 11 | + Author, |
| 12 | + Editor, |
| 13 | + Post, |
| 14 | + TestUniqueNullableModel, |
| 15 | + TestNullableUniqueTogetherModel, |
10 | 16 | ) |
11 | 17 |
|
12 | 18 |
|
@@ -55,3 +61,52 @@ def test_after_type_change(self): |
55 | 61 | TestNullableUniqueTogetherModel.objects.create(a='aaa', b='bbb', c='ccc') |
56 | 62 | with self.assertRaises(IntegrityError): |
57 | 63 | TestNullableUniqueTogetherModel.objects.create(a='aaa', b='bbb', c='ccc') |
| 64 | + |
| 65 | + |
| 66 | +class TestUniqueConstraints(TransactionTestCase): |
| 67 | + def test_unsupportable_unique_constraint(self): |
| 68 | + # Only execute tests when running against SQL Server |
| 69 | + connection = connections['default'] |
| 70 | + if isinstance(connection, DatabaseWrapper): |
| 71 | + |
| 72 | + class TestMigration(migrations.Migration): |
| 73 | + initial = True |
| 74 | + |
| 75 | + operations = [ |
| 76 | + migrations.CreateModel( |
| 77 | + name='TestUnsupportableUniqueConstraint', |
| 78 | + fields=[ |
| 79 | + ( |
| 80 | + 'id', |
| 81 | + models.AutoField( |
| 82 | + auto_created=True, |
| 83 | + primary_key=True, |
| 84 | + serialize=False, |
| 85 | + verbose_name='ID', |
| 86 | + ), |
| 87 | + ), |
| 88 | + ('_type', models.CharField(max_length=50)), |
| 89 | + ('status', models.CharField(max_length=50)), |
| 90 | + ], |
| 91 | + ), |
| 92 | + migrations.AddConstraint( |
| 93 | + model_name='testunsupportableuniqueconstraint', |
| 94 | + constraint=models.UniqueConstraint( |
| 95 | + condition=models.Q( |
| 96 | + ('status', 'in_progress'), |
| 97 | + ('status', 'needs_changes'), |
| 98 | + _connector='OR', |
| 99 | + ), |
| 100 | + fields=('_type',), |
| 101 | + name='or_constraint', |
| 102 | + ), |
| 103 | + ), |
| 104 | + ] |
| 105 | + |
| 106 | + migration = TestMigration('testapp', 'test_unsupportable_unique_constraint') |
| 107 | + |
| 108 | + with connection.schema_editor(atomic=True) as editor: |
| 109 | + with self.assertRaisesRegex( |
| 110 | + NotImplementedError, "does not support OR conditions" |
| 111 | + ): |
| 112 | + return migration.apply(ProjectState(), editor) |
0 commit comments