Skip to content
This repository was archived by the owner on Aug 8, 2020. It is now read-only.

Commit 14c748f

Browse files
authored
Merge pull request #12 from jackton1/develop
Added support for generating the model name.
2 parents be75149 + 7dc7cc6 commit 14c748f

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

check_constraint/models.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import django
2+
from django.apps import apps
13
from django.db import models
24

35
from django.db.models.sql import Query
@@ -6,10 +8,21 @@
68

79

810
class AnnotatedCheckConstraint(models.CheckConstraint):
9-
def __init__(self, *args, annotations=None, **kwargs):
11+
def __init__(self, *args, model=None, annotations=None, **kwargs):
12+
self._model = model
1013
self.annotations = annotations or {}
1114
super(AnnotatedCheckConstraint, self).__init__(*args, **kwargs)
1215

16+
@property
17+
def model(self):
18+
if self._model:
19+
return apps.get_model(self._model)
20+
return self._model
21+
22+
@model.setter
23+
def model(self, new_model):
24+
self._model = new_model
25+
1326
def _get_check_sql(self, model, schema_editor):
1427
query = Query(model=model)
1528

@@ -27,6 +40,14 @@ def _get_check_sql(self, model, schema_editor):
2740

2841
def deconstruct(self):
2942
path, args, kwargs = super(models.CheckConstraint, self).deconstruct()
43+
44+
if (2, 0) <= django.VERSION <= (3, 0) and self.model:
45+
# noinspection PyProtectedMember
46+
kwargs["name"] = kwargs["name"] % {
47+
"app_label": self.model._meta.app_label.lower(),
48+
"class": self.model.__name__.lower(),
49+
}
50+
3051
kwargs["check"] = self.check
3152
kwargs["annotations"] = self.annotations
3253

check_constraint/tests.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
11
from decimal import Decimal
2+
from unittest.mock import patch, PropertyMock
23

34
from django.conf import settings
45
from django.contrib.auth import get_user_model
5-
from django.db import IntegrityError, DatabaseError
6-
from django.test import TestCase
6+
from django.db import IntegrityError, DatabaseError, connections
7+
from django.test import TestCase, TransactionTestCase
78

89
from demo.models import Book
910

1011
# TODO: Fix sqlite
12+
1113
User = get_user_model()
1214

1315

14-
class AnnotateCheckConstraintTestCase(TestCase):
16+
class AnnotatedCheckConstraintNameTestCase(TransactionTestCase):
17+
databases = settings.TEST_ENV_DB
18+
19+
@patch("django.VERSION", new_callable=PropertyMock(return_value=(2, 2)))
20+
def test_correct_name_is_generated_for_django_less_than_30(self, version):
21+
for db_alias in self._databases_names(include_mirrors=False):
22+
connection = connections[db_alias]
23+
24+
connection.disable_constraint_checking()
25+
26+
for constraint in Book._meta.constraints:
27+
name = "%(app_label)s_%(class)s_optional_field_provided"
28+
constraint.model = "demo.Book"
29+
constraint.name = name
30+
31+
path, args, kwargs = constraint.deconstruct()
32+
33+
self.assertEqual(kwargs["name"], "demo_book_optional_field_provided")
34+
35+
36+
class AnnotatedCheckConstraintTestCase(TestCase):
1537
databases = settings.TEST_ENV_DB
1638

1739
@classmethod
1840
def setUpTestData(cls):
19-
for db_name in cls._databases_names(include_mirrors=False):
20-
cls.user = User.objects.db_manager(db_name).create_superuser(
41+
for db_alias in cls._databases_names(include_mirrors=False):
42+
cls.user = User.objects.db_manager(db_alias).create_superuser(
2143
username="Admin", email="[email protected]", password="test",
2244
)
2345

2446
def test_create_passes_with_annotated_check_constraint(self):
25-
for db_name in self._databases_names(include_mirrors=False):
26-
book = Book.objects.using(db_name).create(
47+
for db_alias in self._databases_names(include_mirrors=False):
48+
book = Book.objects.using(db_alias).create(
2749
name="Business of the 21st Century",
2850
created_by=self.user,
2951
amount=Decimal("50"),
@@ -34,17 +56,17 @@ def test_create_passes_with_annotated_check_constraint(self):
3456
self.assertEqual(book.created_by, self.user)
3557

3658
def test_create_is_invalid_with_annotated_check_constraint(self):
37-
for db_name in self._databases_names(include_mirrors=False):
38-
if db_name == "mysql":
59+
for db_alias in self._databases_names(include_mirrors=False):
60+
if db_alias == "mysql":
3961
with self.assertRaises(DatabaseError):
40-
Book.objects.using(db_name).create(
62+
Book.objects.using(db_alias).create(
4163
name="Business of the 21st Century",
4264
created_by=self.user,
4365
amount=Decimal("50"),
4466
)
4567
else:
4668
with self.assertRaises(IntegrityError):
47-
Book.objects.using(db_name).create(
69+
Book.objects.using(db_alias).create(
4870
name="Business of the 21st Century",
4971
created_by=self.user,
5072
amount=Decimal("50"),

django_check_constraint/settings.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import os
1414

1515
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
from django.db import DEFAULT_DB_ALIAS
17+
1618
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1719

1820

@@ -80,8 +82,12 @@
8082
"default": {
8183
"ENGINE": "django.db.backends.sqlite3",
8284
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
83-
"TEST": {"DEPENDENCIES": TEST_ENV_DB},
84-
}
85+
"TEST": {
86+
"DEPENDENCIES": [
87+
d for d in os.environ["ENV_DB"].split(",") if d != DEFAULT_DB_ALIAS
88+
]
89+
},
90+
},
8591
}
8692

8793
if "sqlite3" in TEST_ENV_DB:

0 commit comments

Comments
 (0)