forked from carltongibson/neapolitan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
351 lines (287 loc) · 11.9 KB
/
Copy pathtests.py
File metadata and controls
351 lines (287 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import os
from django.core.management import call_command
from django.http import HttpResponse
from django.test import RequestFactory, TestCase
from django.urls import reverse
from django.utils.html import escape
from neapolitan.views import CRUDView, Role, classonlymethod
from .models import Bookmark, NamedCollection
class BookmarkView(CRUDView):
model = Bookmark
fields = ["url", "title", "note"]
filterset_fields = [
"favourite",
]
class NamedCollectionView(CRUDView):
model = NamedCollection
fields = ["name", "code"]
lookup_field = "code"
path_converter = "uuid"
url_base = "named_collections"
class BookmarkListOnlyView(CRUDView):
model = Bookmark
fields = ["url", "title", "note"]
url_base = "bookmarklist"
@classonlymethod
def get_urls(cls, roles=None):
return super().get_urls(roles={Role.LIST})
urlpatterns = [
*BookmarkView.get_urls(),
*NamedCollectionView.get_urls(),
*BookmarkListOnlyView.get_urls(),
]
class BasicTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.homepage = Bookmark.objects.create(
url="https://noumenal.es/",
title="Noumenal • Dr Carlton Gibson",
note="Carlton Gibson's homepage. Blog, Contact and Project links.",
favourite=True,
)
cls.github = Bookmark.objects.create(
url="https://github.com/carltongibson",
title="Carlton Gibson - GitHub",
note="Carlton Gibson on GitHub",
)
cls.fosstodon = Bookmark.objects.create(
url="https://fosstodon.org/@carlton",
title="Carlton Gibson - Fosstodon",
note="Carlton Gibson on Fosstodon",
)
cls.main_collection = NamedCollection.objects.create(name="main")
def test_list(self):
response = self.client.get("/bookmark/")
self.assertEqual(response.status_code, 200)
self.assertIn("filterset", response.context)
self.assertContains(response, self.homepage.title)
self.assertContains(response, self.github.title)
self.assertContains(response, self.fosstodon.title)
self.assertContains(response, ">Add a new bookmark</a>")
def test_list_empty(self):
Bookmark.objects.all().delete()
response = self.client.get("/bookmark/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "There are no bookmarks. Create one now?")
self.assertContains(response, ">Add a new bookmark</a>")
def test_detail(self):
response = self.client.get(f"/bookmark/{self.homepage.pk}/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.homepage.title)
self.assertContains(response, escape(self.homepage.note))
def test_create(self):
create_url = reverse("bookmark-create")
# Load the form.
response = self.client.get(create_url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'action="/bookmark/new/"')
# Submit the form.
response = self.client.post(
create_url,
{
"url": "https://example.com/",
"title": "Example",
"note": "Example note",
},
follow=True,
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.resolver_match.url_name, "bookmark-detail")
def test_delete(self):
delete_url = reverse("bookmark-delete", args=[self.homepage.pk])
# Load the form.
response = self.client.get(delete_url)
self.assertEqual(response.status_code, 200)
# Submit the form.
response = self.client.post(delete_url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.resolver_match.url_name, "bookmark-list")
def test_update(self):
update_url = reverse("bookmark-update", args=[self.homepage.pk])
# Load the form.
response = self.client.get(update_url)
self.assertEqual(response.status_code, 200)
# Submit the form.
response = self.client.post(
update_url,
{
"url": "https://example.com/",
"title": "Example",
"note": "Example note",
},
follow=True,
)
self.assertEqual(response.status_code, 200)
self.assertRedirects(
response, reverse("bookmark-detail", args=[self.homepage.pk])
)
self.assertContains(response, "Example")
def test_filter(self):
response = self.client.get("/bookmark/?favourite=true")
self.assertEqual(response.status_code, 200)
self.assertSequenceEqual([self.homepage], response.context["bookmark_list"])
self.assertContains(response, self.homepage.title)
self.assertNotContains(response, self.github.title)
self.assertNotContains(response, self.fosstodon.title)
def test_custom_mount_url(self):
"""Test view URL base"""
response = self.client.get("/collection/")
self.assertEqual(response.status_code, 404)
response = self.client.get("/named_collections/")
self.assertEqual(response.status_code, 200)
def test_custom_lookup_field(self):
"""Test custom view.lookup_field"""
response = self.client.get(f"/named_collections/{self.main_collection.code}/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.main_collection.name)
def test_lookup_url_converter(self):
"""Test view.lookup_url_converter"""
response = self.client.get(f"/named_collections/{self.main_collection.id}/")
self.assertEqual(response.status_code, 404)
response = self.client.get(f"/named_collections/{self.main_collection.code}/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.main_collection.name)
def test_overriding_role_initkwargs(self):
"""as_view must prioritise initkwargs over Role extra_initkwargs."""
class InitKwargsCRUDView(CRUDView):
model = Bookmark
def detail(self, request, *args, **kwargs):
return HttpResponse(self.template_name_suffix)
view = InitKwargsCRUDView.as_view(
role=Role.DETAIL,
template_name_suffix='_test_suffix'
)
request = RequestFactory().get('/')
response = view(request)
self.assertContains(response, '_test_suffix')
class RoleTests(TestCase):
def test_overriding_url_base(self):
class AlternateCRUDView(CRUDView):
model = Bookmark
url_base = "something-else"
self.assertEqual(AlternateCRUDView.url_base, "something-else")
self.assertEqual(
Role.LIST.url_pattern(
AlternateCRUDView,
),
"something-else/",
)
def test_roles_provide_a_url_name_component(self):
# The URL name is constructed in part by the role value.
tests = [
(Role.LIST, "list"),
(Role.DETAIL, "detail"),
(Role.CREATE, "create"),
(Role.UPDATE, "update"),
(Role.DELETE, "delete"),
]
for role, name in tests:
with self.subTest(role=role):
self.assertEqual(role.url_name_component, name)
def test_url_pattern_generation(self):
tests = [
(Role.LIST, "bookmark/"),
(Role.DETAIL, "bookmark/<int:pk>/"),
(Role.CREATE, "bookmark/new/"),
(Role.UPDATE, "bookmark/<int:pk>/edit/"),
(Role.DELETE, "bookmark/<int:pk>/delete/"),
]
for role, pattern in tests:
with self.subTest(role=role):
self.assertEqual(
role.url_pattern(BookmarkView),
pattern,
)
def test_role_url_reversing(self):
bookmark = Bookmark.objects.create(
url="https://noumenal.es/",
title="Noumenal • Dr Carlton Gibson",
note="Carlton Gibson's homepage. Blog, Contact and Project links.",
favourite=True,
)
tests = [
(Role.LIST, "/bookmark/"),
(Role.DETAIL, f"/bookmark/{bookmark.pk}/"),
(Role.CREATE, "/bookmark/new/"),
(Role.UPDATE, f"/bookmark/{bookmark.pk}/edit/"),
(Role.DELETE, f"/bookmark/{bookmark.pk}/delete/"),
]
for role, url in tests:
with self.subTest(role=role):
self.assertEqual(
role.reverse(BookmarkView, bookmark),
url,
)
def test_routing_subset_of_roles(self):
urlpatterns = BookmarkView.get_urls(roles={Role.LIST, Role.DETAIL})
self.assertEqual(len(urlpatterns), 2)
def test_rendering_list_only_role(self):
bookmark = Bookmark.objects.create(
url="https://noumenal.es/",
title="Noumenal • Dr Carlton Gibson",
note="Carlton Gibson's homepage. Blog, Contact and Project links.",
favourite=True,
)
response = self.client.get('/bookmarklist/')
self.assertEqual(response.status_code, 200)
for lookup in ['View', 'Edit', 'Delete']:
self.assertNotContains(response, f'>{lookup}</a>')
def test_url_ordering_for_slug_path_converters(self):
# Ensures correct ordering of URL patterns when using str-based path converters
# https://github.com/carltongibson/neapolitan/issues/64
class BookmarkCRUDView(CRUDView):
model = Bookmark
path_converter = "slug"
lookup_url_kwarg = "title"
# Get the generated URLs
urls = BookmarkCRUDView.get_urls()
# Extract paths for the URLs to check ordering
url_paths = [url.pattern._route for url in urls]
# Expected order of URL paths
expected_paths = [
'bookmark/', # LIST
'bookmark/new/', # CREATE should come before any slug-based URLs
'bookmark/<slug:title>/', # DETAIL
'bookmark/<slug:title>/edit/', # UPDATE
'bookmark/<slug:title>/delete/', # DELETE
]
# Assert that the generated URL paths match the expected order
self.assertEqual(url_paths, expected_paths)
def test_role_equality(self):
"""
Role instances should be equal to themselves but not to other Role
instances.
Follows directly from Enum base class, but is preparatory to custom
roles.
"""
# Basic examples:
self.assertEqual(Role.LIST, Role.LIST)
self.assertNotEqual(Role.LIST, Role.DETAIL)
# Exhaustive check:
for role in Role:
self.assertEqual(role, role)
for other_role in (r for r in Role if r != role):
self.assertNotEqual(role, other_role)
class MktemplateCommandTest(TestCase):
def test_mktemplate_command(self):
# Run the command
call_command("mktemplate", "tests.Bookmark", "--list")
# Check if the file was created
file_path = "tests/templates/tests/bookmark_list.html"
self.assertTrue(os.path.isfile(file_path))
# Remove the created file
os.remove(file_path)
def test_mktemplate_all_command(self):
# Run the command with --all
call_command("mktemplate", "tests.Bookmark", "--all")
# Check if all files were created
expected_files = [
"tests/templates/tests/bookmark_list.html",
"tests/templates/tests/bookmark_detail.html",
"tests/templates/tests/bookmark_form.html",
"tests/templates/tests/bookmark_confirm_delete.html",
]
for file_path in expected_files:
self.assertTrue(os.path.isfile(file_path))
# Clean up
os.remove(file_path)