Skip to content

Commit 203627d

Browse files
committed
[Tests] add tests for entire copy_help_texts_to_database function
1 parent daff928 commit 203627d

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

django_db_comments/db_comments.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ def add_table_comments_to_database(table_comment_dict, using=DEFAULT_DB_ALIAS):
6767
cursor.execute(query_for_table_comment, [comment])
6868

6969

70+
def _check_app_config(app_config, using):
71+
app_label = app_config.label
72+
if not app_config.models_module:
73+
return False
74+
75+
if settings.DATABASES[using]["ENGINE"] not in ALLOWED_ENGINES:
76+
return False
77+
78+
if not router.allow_migrate(using, app_label):
79+
return False
80+
return True
81+
82+
7083
def copy_help_texts_to_database(
7184
app_config,
7285
verbosity=2,
@@ -78,21 +91,19 @@ def copy_help_texts_to_database(
7891
"""
7992
Create content types for models in the given app.
8093
"""
81-
if not app_config.models_module:
82-
return
83-
84-
if settings.DATABASES[using]["ENGINE"] not in ALLOWED_ENGINES:
85-
return
86-
87-
app_label = app_config.label
88-
if not router.allow_migrate(using, app_label):
94+
if not _check_app_config(app_config, using):
8995
return
9096

91-
app_config = apps.get_app_config(app_label)
9297
app_models = [
9398
app_model
9499
for app_model in app_config.get_models()
95-
if not any([app_model.abstract, app_model.proxy, not app_model.managed])
100+
if not any(
101+
[
102+
app_model._meta.abstract,
103+
app_model._meta.proxy,
104+
not app_model._meta.managed,
105+
]
106+
)
96107
]
97108

98109
columns_comments = {

tests/models.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django.db import models
2+
3+
4+
class TimestampedModel(models.Model):
5+
created_at = models.DateTimeField("model creation time")
6+
7+
class Meta:
8+
abstract = True
9+
10+
11+
class ExampleModel(TimestampedModel):
12+
class Meta:
13+
verbose_name = "This is an example for table comment"
14+
15+
16+
class ProxyModel(ExampleModel):
17+
class Meta:
18+
proxy = True
19+
20+
21+
class UnmanagedModel(models.Model):
22+
class Meta:
23+
managed = False

tests/settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"django.contrib.contenttypes",
2424
"django.contrib.sites",
2525
"django_db_comments",
26+
27+
"tests",
2628
]
2729

2830
SITE_ID = 1

tests/test_db_comments.py

+30-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
from mock import patch
1212
from psycopg2 import sql
1313

14+
from django.apps import apps
1415
from django.db import models, DEFAULT_DB_ALIAS
1516
from django.test import TestCase
17+
1618
try:
1719
from django.utils.translation import ugettext_lazy as _
1820
except ImportError:
@@ -25,6 +27,7 @@
2527
add_table_comments_to_database,
2628
POSTGRES_COMMENT_SQL,
2729
POSTGRES_COMMENT_ON_TABLE_SQL,
30+
copy_help_texts_to_database,
2831
)
2932

3033

@@ -38,7 +41,7 @@ class Model(models.Model):
3841
)
3942

4043
class Meta:
41-
app_label = "tests"
44+
app_label = "unit_test"
4245

4346
column_comments = get_comments_for_model(Model)
4447
self.assertDictEqual(
@@ -91,7 +94,7 @@ def test_add_table_comments_to_database(self, mock_connections):
9194
mock_cursor.execute.assert_called_once_with(query, [comment])
9295

9396
def test_ugettext_lazy_workaround(self):
94-
class Model(models.Model):
97+
class GettextLazyModel(models.Model):
9598
# Example from Django auth.User
9699
is_superuser = models.BooleanField(
97100
_("superuser status"),
@@ -102,13 +105,36 @@ class Model(models.Model):
102105
)
103106

104107
class Meta:
105-
app_label = "tests"
108+
app_label = "unit_test"
106109

107-
column_comments = get_comments_for_model(Model)
110+
column_comments = get_comments_for_model(GettextLazyModel)
108111
self.assertDictEqual(
109112
column_comments,
110113
{
111114
"is_superuser": "superuser status | Designates that this user has all "
112115
"permissions without explicitly assigning them."
113116
},
114117
)
118+
119+
def test_post_migrate_signal(self):
120+
app_config = apps.get_app_config("tests")
121+
with patch(
122+
"django_db_comments.db_comments._check_app_config", return_value=True
123+
):
124+
with patch(
125+
"django_db_comments.db_comments.add_column_comments_to_database",
126+
return_value=True,
127+
) as mock_column_comments:
128+
with patch(
129+
"django_db_comments.db_comments.add_table_comments_to_database",
130+
return_value=True,
131+
) as mock_table_comments:
132+
copy_help_texts_to_database(app_config)
133+
mock_table_comments.assert_called_once_with(
134+
{"tests_examplemodel": "This Is An Example For Table Comment"},
135+
"default",
136+
)
137+
mock_column_comments.assert_called_once_with(
138+
{"tests_examplemodel": {"created_at": "model creation time"}},
139+
"default",
140+
)

0 commit comments

Comments
 (0)