Skip to content

Commit 24decc5

Browse files
committed
Separate the checks tests
1 parent e4041e6 commit 24decc5

File tree

2 files changed

+83
-79
lines changed

2 files changed

+83
-79
lines changed

tests/testapp/test_checks.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from django.contrib import admin
2+
from django.core import checks
3+
from django.db import models
4+
from django.test.utils import isolate_apps
5+
6+
from content_editor.admin import ContentEditor, ContentEditorInline
7+
from testapp.models import Article, RichText
8+
9+
10+
@isolate_apps()
11+
def test_model_checks():
12+
class Model(models.Model):
13+
name = models.CharField()
14+
15+
def __str__(self):
16+
return self.name
17+
18+
class ModelAdmin(ContentEditor):
19+
model = Model
20+
inlines = []
21+
22+
assert ModelAdmin(Model, admin.AdminSite()).check() == [
23+
checks.Error(
24+
"ContentEditor models require a non-empty 'regions' attribute or property.",
25+
obj=ModelAdmin,
26+
id="content_editor.E002",
27+
)
28+
]
29+
30+
31+
def test_inline_checks():
32+
assert admin.ModelAdmin(Article, admin.AdminSite()).check() == []
33+
34+
class RichTextInline(ContentEditorInline):
35+
model = RichText
36+
# Purposefully construct an inline with missing region
37+
# and ordering fields
38+
fieldsets = [(None, {"fields": ("text",)})]
39+
40+
class InvalidRegionsStringInline(ContentEditorInline):
41+
model = RichText
42+
regions = "main"
43+
44+
class InvalidRegionsCallableInline(ContentEditorInline):
45+
model = RichText
46+
47+
def regions(self, all_regions):
48+
return "main"
49+
50+
class ValidRegionsGeneratorInline(ContentEditorInline):
51+
model = RichText
52+
53+
def regions(self, all_regions):
54+
yield "main"
55+
56+
class ArticleAdmin(ContentEditor):
57+
model = Article
58+
inlines = [
59+
RichTextInline,
60+
InvalidRegionsStringInline,
61+
InvalidRegionsCallableInline,
62+
ValidRegionsGeneratorInline,
63+
]
64+
65+
assert ArticleAdmin(Article, admin.AdminSite()).check() == [
66+
checks.Error(
67+
"fieldsets must contain both 'region' and 'ordering'.",
68+
obj=RichTextInline,
69+
id="content_editor.E001",
70+
),
71+
checks.Error(
72+
"regions must be 'None' or an iterable. Current value is 'main'.",
73+
obj=InvalidRegionsStringInline,
74+
id="content_editor.E003",
75+
),
76+
checks.Error(
77+
"regions must be 'None' or an iterable. Current value is 'main'.",
78+
obj=InvalidRegionsCallableInline,
79+
id="content_editor.E003",
80+
),
81+
]

tests/testapp/test_content_editor.py

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import pytest
2-
from django.contrib import admin
32
from django.contrib.auth.models import User
4-
from django.core import checks
53
from django.core.exceptions import ImproperlyConfigured
6-
from django.db import connection, models
7-
from django.test.utils import CaptureQueriesContext, isolate_apps
4+
from django.db import connection
5+
from django.test.utils import CaptureQueriesContext
86
from django.urls import reverse
97
from pytest_django.asserts import assertContains
108

11-
from content_editor.admin import ContentEditor, ContentEditorInline
129
from content_editor.contents import contents_for_item
1310
from content_editor.models import Region
1411
from testapp.models import Article, Download, RichText
@@ -93,80 +90,6 @@ def test_admin(client):
9390
assertContains(response, 'value="Test"', 1)
9491

9592

96-
@isolate_apps()
97-
def test_model_checks():
98-
class Model(models.Model):
99-
name = models.CharField()
100-
101-
def __str__(self):
102-
return self.name
103-
104-
class ModelAdmin(ContentEditor):
105-
model = Model
106-
inlines = []
107-
108-
assert ModelAdmin(Model, admin.AdminSite()).check() == [
109-
checks.Error(
110-
"ContentEditor models require a non-empty 'regions' attribute or property.",
111-
obj=ModelAdmin,
112-
id="content_editor.E002",
113-
)
114-
]
115-
116-
117-
def test_inline_checks():
118-
assert admin.ModelAdmin(Article, admin.AdminSite()).check() == []
119-
120-
class RichTextInline(ContentEditorInline):
121-
model = RichText
122-
# Purposefully construct an inline with missing region
123-
# and ordering fields
124-
fieldsets = [(None, {"fields": ("text",)})]
125-
126-
class InvalidRegionsStringInline(ContentEditorInline):
127-
model = RichText
128-
regions = "main"
129-
130-
class InvalidRegionsCallableInline(ContentEditorInline):
131-
model = RichText
132-
133-
def regions(self, all_regions):
134-
return "main"
135-
136-
class ValidRegionsGeneratorInline(ContentEditorInline):
137-
model = RichText
138-
139-
def regions(self, all_regions):
140-
yield "main"
141-
142-
class ArticleAdmin(ContentEditor):
143-
model = Article
144-
inlines = [
145-
RichTextInline,
146-
InvalidRegionsStringInline,
147-
InvalidRegionsCallableInline,
148-
ValidRegionsGeneratorInline,
149-
]
150-
151-
assert ArticleAdmin(Article, admin.AdminSite()).check() == [
152-
checks.Error(
153-
"fieldsets must contain both 'region' and 'ordering'.",
154-
obj=RichTextInline,
155-
id="content_editor.E001",
156-
),
157-
checks.Error(
158-
"regions must be 'None' or an iterable. Current value is 'main'.",
159-
obj=InvalidRegionsStringInline,
160-
id="content_editor.E003",
161-
),
162-
checks.Error(
163-
"regions must be 'None' or an iterable. Current value is 'main'.",
164-
obj=InvalidRegionsCallableInline,
165-
id="content_editor.E003",
166-
),
167-
]
168-
169-
17093
def test_invalid_region_objects():
17194
with pytest.raises(ImproperlyConfigured):
17295
Region(key="regions", title="regions")

0 commit comments

Comments
 (0)