-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtests.py
400 lines (323 loc) · 20 KB
/
tests.py
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import datetime
import shutil
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import override_settings, RequestFactory, SimpleTestCase, tag, TestCase
from django.urls import reverse
from pathlib import Path
from photo_gallery.settings import BASE_DIR
from .admin import PhotoAdmin
from .models import Collection, Photo, validate_lowercase
# Deleted at the end of full test runs via TestMediaCleanup()
TEST_MEDIA_DIR = BASE_DIR / 'test_media/'
def create_uploaded_file_object(image_path):
"""Create a `SimpleUploadedFile` object using a specified image path.
Used to mock the creation of a Photo object without manual upload.
https://docs.djangoproject.com/en/4.2/_modules/django/core/files/uploadedfile/
"""
with open(image_path, 'rb') as img_file:
file_content = img_file.read()
return SimpleUploadedFile(name='test_image.jpg',
content=file_content,
content_type='image/jpeg')
def create_photo(slug, title="Photo", description="Description", location="Location",
date_taken=datetime.date(2022, 1, 1), featured=False, published=True,
collections=None):
large_img_path = Path(__file__).resolve().parent / 'test_images/2500x1500.jpg'
mock_large_upload = create_uploaded_file_object(large_img_path)
photo = Photo.objects.create(slug=slug, title=title, description=description, location=location,
date_taken=date_taken, featured=featured, published=published,
large_image=mock_large_upload)
if collections is not None:
photo.collections.set(collections)
photo.save()
return photo
def create_published_photos(num):
for x in range(num):
create_photo(slug="test-slug-" + str(x+1), published=True)
@tag('photos', 'models')
@override_settings(MEDIA_ROOT=TEST_MEDIA_DIR, SECURE_SSL_REDIRECT=False)
class PhotoModelTests(TestCase):
def test_image_downsizing(self):
"""Test that ProcessedImageField and ImageSpecField downsize an uploaded image."""
photo = create_photo(slug="image-downsizing-test") # Original image: 2500x1500
self.assertEqual(photo.large_image.width, 2000)
self.assertEqual(photo.small_image.width, 550)
def test_image_upsizing(self):
"""Test that ProcessedImageField upsizes an uploaded image to a width of 2000px."""
small_img_path = Path(__file__).resolve().parent / 'test_images/200x100.jpg'
small_img = create_uploaded_file_object(small_img_path)
photo = Photo.objects.create(slug="image-upsizing-test", title="Photo", description="Desc",
location="Loc", date_taken=datetime.date(2022, 1, 1),
featured=False, published=True, large_image=small_img)
self.assertEqual(photo.large_image.width, 2000)
def test_photo_str(self):
"""Test the Photo __str__ method."""
photo = create_photo(title="Test Title", slug="test-slug")
self.assertEqual(photo.__str__(), "Test Title (test-slug)")
class MockPhotoAdmin(PhotoAdmin):
def __init__(self):
pass
@tag('photos', 'admin')
@override_settings(MEDIA_ROOT=TEST_MEDIA_DIR, SECURE_SSL_REDIRECT=False)
class PhotoAdminTests(TestCase):
request = RequestFactory()
def test_get_fields_add(self):
"""Test that `thumbnail_img_tag` is excluded from add view `fields`"""
photo_admin = MockPhotoAdmin()
fields = photo_admin.get_fields(self.request)
self.assertEqual(fields, ['large_image', 'title', 'slug', 'description', 'location',
'country', 'date_taken', 'collections', 'featured', 'published'])
def test_get_fields_change(self):
"""Test that `thumbnail_img_tag` is included in change view `fields`"""
photo_admin = MockPhotoAdmin()
fields = photo_admin.get_fields(self.request, obj=create_photo(slug="test"))
self.assertEqual(fields, ['large_image', 'thumbnail_img_tag', 'title', 'slug',
'description', 'location', 'country', 'date_taken',
'collections', 'featured', 'published'])
@tag('photos', 'views', 'photo_detail')
@override_settings(MEDIA_ROOT=TEST_MEDIA_DIR, SECURE_SSL_REDIRECT=False)
class PhotoDetailViewTests(TestCase):
def test_published_photo_status(self):
"""Test that a published Photo returns a 200 status code."""
test_slug = "published-photo"
create_photo(slug=test_slug, published=True)
response = self.client.get(reverse("photo_detail", kwargs={"slug": test_slug}))
self.assertEqual(response.status_code, 200)
def test_unpublished_photo_status(self):
"""Test that an unpublished Photo returns a 404 status code."""
test_slug = "unpublished-photo"
create_photo(slug=test_slug, published=False)
response = self.client.get(reverse("photo_detail", kwargs={"slug": test_slug}))
self.assertEqual(response.status_code, 404)
def test_404_photo_status(self):
"""Test that a slug without an associated Photo returns a 404 status code."""
test_slug = "404-slug"
response = self.client.get(reverse("photo_detail", kwargs={"slug": test_slug}))
self.assertEqual(response.status_code, 404)
def test_photo_image_in_response(self):
"""Test that a published photo response contains the photo image URLs."""
test_slug = "image-test"
photo = create_photo(slug=test_slug, published=True)
response = self.client.get(reverse("photo_detail", kwargs={"slug": test_slug}))
self.assertContains(response, photo.large_image.url)
self.assertContains(response, photo.small_image.url)
def test_photo_title_in_response(self):
"""Test that a published photo response contains the photo title."""
test_slug = "title-test"
test_title = "Test Title"
create_photo(slug=test_slug, title=test_title, published=True)
response = self.client.get(reverse("photo_detail", kwargs={"slug": test_slug}))
self.assertContains(response, test_title)
def test_photo_description_in_response(self):
"""Test that a published photo response contains the photo description."""
test_slug = "description-test"
test_description = "This is a test description."
create_photo(slug=test_slug, description=test_description, published=True)
response = self.client.get(reverse("photo_detail", kwargs={"slug": test_slug}))
self.assertContains(response, test_description)
def test_collections_in_response(self):
"""Test that the response of a photo in >0 collections contains collections text."""
col = Collection.objects.create(name="Published Col", slug="test-col", published=True)
test_slug = "has-collections"
create_photo(slug=test_slug, published=True, collections=[col])
response = self.client.get(reverse("photo_detail", kwargs={"slug": test_slug}))
self.assertContains(response, "Collections:")
def test_collections_not_in_response(self):
"""Test that the response of a photo in 0 collections doesn't contain collections text."""
Collection.objects.create(name="Published Col", slug="test-col", published=True)
test_slug = "no-collections"
create_photo(slug=test_slug, published=True, collections=None)
response = self.client.get(reverse("photo_detail", kwargs={"slug": test_slug}))
self.assertNotContains(response, "Collections:")
@tag('photos', 'views', 'photo_list')
@override_settings(MEDIA_ROOT=TEST_MEDIA_DIR, SECURE_SSL_REDIRECT=False)
class PhotoListViewTests(TestCase):
def test_qs_unpublished_filtering(self):
"""Test that only `published` Photos are included in the queryset."""
published_photo = create_photo(slug="published-photo", published=True)
create_photo(slug="unpublished-photo", published=False)
response = self.client.get(reverse("homepage"))
self.assertQuerySetEqual(response.context['photo_list'], [published_photo])
def test_qs_featured_ordering(self):
"""Test that `featured` Photos are ordered first in the queryset."""
create_photo(slug="unfeatured1", featured=False)
featured_photo = create_photo(slug="featured", featured=True)
create_photo(slug="unfeatured2", featured=False)
response = self.client.get(reverse("homepage"))
self.assertEqual(response.context['photo_list'][0], featured_photo)
def test_qs_date_taken_ordering(self):
"""Test that Photos are ordered by descending `date_taken` in the queryset."""
p_2021 = create_photo(slug="2021", date_taken=datetime.date(2021, 1, 1))
p_2022 = create_photo(slug="2022", date_taken=datetime.date(2022, 1, 1))
p_2020 = create_photo(slug="2020", date_taken=datetime.date(2020, 1, 1))
response = self.client.get(reverse("homepage"))
self.assertQuerySetEqual(response.context['photo_list'], [p_2022, p_2021, p_2020])
def test_qs_combined_ordering(self):
"""Test that Photos are ordered by descending `featured` then by descending `date_taken`"""
old_date = datetime.date(2020, 1, 1)
new_date = datetime.date(2022, 1, 1)
p_unfeatured_old = create_photo(slug="unfeatured-old", featured=False, date_taken=old_date)
p_featured_new = create_photo(slug="featured-new", featured=True, date_taken=new_date)
p_unfeatured_new = create_photo(slug="unfeatured-new", featured=False, date_taken=new_date)
p_featured_old = create_photo(slug="featured-old", featured=True, date_taken=old_date)
response = self.client.get(reverse("homepage"))
expected_qs = [p_featured_new, p_featured_old, p_unfeatured_new, p_unfeatured_old]
self.assertQuerySetEqual(response.context['photo_list'], expected_qs)
def test_qs_new_ordering(self):
"""Test that Photos are ordered by descending `date_taken` with the `new` query string"""
old_date = datetime.date(2020, 1, 1)
mid_date = datetime.date(2021, 1, 1)
new_date = datetime.date(2022, 1, 1)
p_unfeatured_old = create_photo(slug="unfeatured-old", featured=False, date_taken=old_date)
p_unfeatured_new = create_photo(slug="unfeatured-new", featured=False, date_taken=new_date)
p_featured_mid = create_photo(slug="featured-mid", featured=True, date_taken=mid_date)
response = self.client.get(reverse("homepage") + "?sort=new")
expected_qs = [p_unfeatured_new, p_featured_mid, p_unfeatured_old]
self.assertQuerySetEqual(response.context['photo_list'], expected_qs)
def test_qs_old_ordering(self):
"""Test that Photos are ordered by ascending `date_taken` with the `old` query string"""
old_date = datetime.date(2020, 1, 1)
mid_date = datetime.date(2021, 1, 1)
new_date = datetime.date(2022, 1, 1)
p_featured_mid = create_photo(slug="featured-mid", featured=True, date_taken=mid_date)
p_unfeatured_new = create_photo(slug="unfeatured-new", featured=False, date_taken=new_date)
p_unfeatured_old = create_photo(slug="unfeatured-old", featured=False, date_taken=old_date)
response = self.client.get(reverse("homepage") + "?sort=old")
expected_qs = [p_unfeatured_old, p_featured_mid, p_unfeatured_new]
self.assertQuerySetEqual(response.context['photo_list'], expected_qs)
def test_paginated_200_status(self):
"""Test that a paginated URL with at least 1 associated Photo returns a 200 status code."""
# `paginate_by = 6` (6 photos per page)
create_published_photos(7)
response = self.client.get(reverse("homepage"), {"page": 2})
self.assertEqual(response.status_code, 200)
def test_paginated_404_status(self):
"""Test that a paginated URL with no associated Photos returns a 404 status code."""
# `paginate_by = 6` (6 photos per page)
create_photo(slug="test", published=True)
response = self.client.get(reverse("homepage"), {"page": 2})
self.assertEqual(response.status_code, 404)
@tag('photos', 'views', 'collection')
@override_settings(MEDIA_ROOT=TEST_MEDIA_DIR, SECURE_SSL_REDIRECT=False)
class CollectionViewTests(TestCase):
def test_published_collection_status(self):
"""Test that a published Collection returns a 200 status code."""
col = Collection.objects.create(name="Published Col", slug="test-col", published=True)
response = self.client.get(reverse("collection", kwargs={"collection_slug": col.slug}))
self.assertEqual(response.status_code, 200)
def test_unpublished_collection_status(self):
"""Test that an unpublished Collection returns a 404 status code."""
col = Collection.objects.create(name="Unpublished Col", slug="test-col", published=False)
create_photo(slug="published-photo", published=True, collections=[col])
response = self.client.get(reverse("collection", kwargs={"collection_slug": col.slug}))
self.assertEqual(response.status_code, 404)
def test_404_collection_status(self):
"""Test that a slug without an associated Collection returns a 404 status code."""
response = self.client.get(reverse("collection", kwargs={"collection_slug": "404-slug"}))
self.assertEqual(response.status_code, 404)
def test_qs_unpublished_filtering(self):
"""Test that only `published` Photos are included in the collection queryset."""
col = Collection.objects.create(name="Test Col", slug="test-col", published=True)
published_photo = create_photo(slug="published-photo", published=True, collections=[col])
create_photo(slug="unpublished-photo", published=False, collections=[col])
response = self.client.get(reverse("collection", kwargs={"collection_slug": col.slug}))
self.assertQuerySetEqual(response.context['photo_list'], [published_photo])
def test_qs_collection_filtering(self):
"""Test that only Photos in a collection are included in the collection queryset."""
col1 = Collection.objects.create(name="Col 1", slug="col-1", published=True)
col2 = Collection.objects.create(name="Col 2", slug="col-2", published=True)
photo1 = create_photo(slug="photo-1", published=True, collections=[col1])
create_photo(slug="photo-2", published=True, collections=None)
create_photo(slug="photo-3", published=True, collections=[col2])
response = self.client.get(reverse("collection", kwargs={"collection_slug": col1.slug}))
self.assertQuerySetEqual(response.context['photo_list'], [photo1])
def test_qs_multiple_collection_filtering(self):
"""Test that Photos with multiple collections are included in the collection queryset."""
col1 = Collection.objects.create(name="Col 1", slug="col-1", published=True)
col2 = Collection.objects.create(name="Col 2", slug="col-2", published=True)
photo = create_photo(slug="photo-1", published=True, collections=[col2, col1])
response = self.client.get(reverse("collection", kwargs={"collection_slug": col1.slug}))
self.assertQuerySetEqual(response.context['photo_list'], [photo])
def test_collection_name_in_response(self):
"""Test that a published collection response contains the collection name."""
test_name = "Collection Test Name"
col = Collection.objects.create(name=test_name, slug="test-collection", published=True)
response = self.client.get(reverse("collection", kwargs={"collection_slug": col.slug}))
self.assertContains(response, test_name)
def test_collection_description_in_response(self):
"""Test that a published collection response contains the collection description."""
test_description = "This is a test description."
col = Collection.objects.create(name="Test Name",
description=test_description,
slug="test-col",
published=True)
response = self.client.get(reverse("collection", kwargs={"collection_slug": col.slug}))
self.assertContains(response, test_description)
def test_photo_link_in_response(self):
"""Test that a published collection response contains an associated photo URL."""
col = Collection.objects.create(name="Published Col", slug="test-col", published=True)
photo = create_photo(slug="test-photo-slug", published=True, collections=[col])
response = self.client.get(reverse("collection", kwargs={"collection_slug": col.slug}))
self.assertContains(response, photo.get_absolute_url())
def test_empty_qs_error_message(self):
"""Test that a response with an empty queryset includes an error message."""
col = Collection.objects.create(name="Empty Col", slug="empty-col", published=True)
response = self.client.get(reverse("collection", kwargs={"collection_slug": col.slug}))
self.assertContains(response, "no photos were found")
@tag('photos', 'views', 'search')
@override_settings(MEDIA_ROOT=TEST_MEDIA_DIR, SECURE_SSL_REDIRECT=False)
class SearchViewTests(TestCase):
def test_qs_search_query_filtering(self):
"""Test that only Photos that match a search criteria are included in the queryset."""
match1 = create_photo(slug="p1", title="TestSearch", description="desc", location="loc",
date_taken=datetime.date(2022, 1, 1))
create_photo(slug="p2", title="title", description="desc", location="loc",
date_taken=datetime.date(2021, 1, 1))
match2 = create_photo(slug="p3", title="title", description="testsearch", location="loc",
date_taken=datetime.date(2020, 1, 1))
match3 = create_photo(slug="p4", title="title", description="desc", location="testsearch",
date_taken=datetime.date(2019, 1, 1))
response = self.client.get(reverse("search") + "?query=testsearch")
expected_qs = [match1, match2, match3]
self.assertQuerySetEqual(response.context['photo_list'], expected_qs)
def test_200_status(self):
"""Test that a request with a non-empty query returns a 200 status."""
response = self.client.get(reverse("search") + "?query=testsearch")
self.assertEqual(response.status_code, 200)
def test_empty_query_redirect(self):
"""Test that a request with an empty query redirects to the homepage."""
response = self.client.get(reverse("search"))
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse("homepage"))
def test_empty_qs_error_message(self):
"""Test that a response with an empty queryset includes an error message."""
response = self.client.get(reverse("search") + "?query=testsearch")
self.assertContains(response, "no photos were found")
@tag('photos', 'validators')
class ValidatorTests(TestCase):
def test_lowercase_validates(self):
"""Test that `validate_lowercase()` doesn't incorrectly raise a ValidationError"""
lower_str = "lowercase-string-100! "
try:
validate_lowercase(lower_str)
except ValidationError:
self.fail("validate_lowercase() raised ValidationError unexpectedly "
"for string `{}`.".format(lower_str))
def test_lowercase_raises(self):
"""Test that `validate_lowercase()` correctly raises a ValidationError"""
mixed_case_str = "mixed-Case-string-100! "
with self.assertRaises(ValidationError):
validate_lowercase(mixed_case_str)
@tag('photos', 'cleanup')
class TestMediaCleanup(SimpleTestCase):
"""Delete the `TEST_MEDIA_DIR` directory after running `TestCase` subclass tests.
https://docs.djangoproject.com/en/4.2/topics/testing/overview/#order-in-which-tests-are-executed
"""
def test_dummy(self):
"""Trigger class setup and teardown."""
pass
@classmethod
def tearDownClass(cls):
shutil.rmtree(TEST_MEDIA_DIR)
super().tearDownClass()