Skip to content

Commit da5f2b5

Browse files
committed
fix: Fix queryset comparison for django 4.2
1 parent 2f77a4a commit da5f2b5

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

tests/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ class MongoTestCase(SimpleTestCase):
1010
TestCase class that clear the collection between the tests
1111
"""
1212

13-
assertQuerysetEqual = TransactionTestCase.__dict__['assertQuerysetEqual']
13+
try:
14+
assertQuerySetEqual = TransactionTestCase.__dict__['assertQuerySetEqual']
15+
except KeyError:
16+
# https://docs.djangoproject.com/en/4.2/topics/testing/tools/#django.test.TransactionTestCase.assertQuerySetEqual
17+
# Drop this after supporting only django > 4.2
18+
assertQuerySetEqual = TransactionTestCase.__dict__['assertQuerysetEqual']
1419

1520
def __init__(self, methodName='runtest'):
1621
from django.conf import settings

tests/views/edit.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
import unittest
42

53
from django.core.exceptions import ImproperlyConfigured
@@ -23,7 +21,12 @@ def test_initial_data(self):
2321
self.assertNotEqual(initial_1, initial_2)
2422

2523

26-
class CreateViewTests(MongoTestCase):
24+
class ReprComparisonMixin:
25+
def assertQuerySetEqual(self, qs, values, transform=repr, ordered=True, msg=None):
26+
return super().assertQuerySetEqual(qs, values, transform, ordered, msg)
27+
28+
29+
class CreateViewTests(ReprComparisonMixin, MongoTestCase):
2730
def setUp(self):
2831
Author.drop_collection()
2932

@@ -40,7 +43,7 @@ def test_create(self):
4043
)
4144
self.assertEqual(res.status_code, 302)
4245
self.assertRedirects(res, '/list/authors/')
43-
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
46+
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
4447

4548
def test_create_invalid(self):
4649
res = self.client.post(
@@ -56,7 +59,7 @@ def test_create_with_object_url(self):
5659
self.assertEqual(res.status_code, 302)
5760
artist = Artist.objects.get(name='Rene Magritte')
5861
self.assertRedirects(res, '/detail/artist/%s/' % artist.pk)
59-
self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
62+
self.assertQuerySetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
6063

6164
def test_create_with_redirect(self):
6265
res = self.client.post(
@@ -65,14 +68,14 @@ def test_create_with_redirect(self):
6568
)
6669
self.assertEqual(res.status_code, 302)
6770
self.assertRedirects(res, '/edit/authors/create/')
68-
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
71+
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
6972

7073
def test_create_with_interpolated_redirect(self):
7174
res = self.client.post(
7275
'/edit/authors/create/interpolate_redirect/',
7376
{'id': 1, 'name': 'Randall Munroe', 'slug': 'randall-munroe'},
7477
)
75-
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
78+
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
7679
self.assertEqual(res.status_code, 302)
7780
pk = Author.objects.all()[0].pk
7881
self.assertRedirects(res, '/edit/author/%s/update/' % pk)
@@ -92,7 +95,7 @@ def test_create_with_special_properties(self):
9295
self.assertEqual(res.status_code, 302)
9396
obj = Author.objects.get(slug='randall-munroe')
9497
self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
95-
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
98+
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
9699

97100
def test_create_without_redirect(self):
98101
try:
@@ -107,7 +110,7 @@ def test_create_without_redirect(self):
107110
pass
108111

109112

110-
class UpdateViewTests(TestCase):
113+
class UpdateViewTests(ReprComparisonMixin, TestCase):
111114
def setUp(self):
112115
Author.drop_collection()
113116

@@ -131,7 +134,7 @@ def test_update_post(self):
131134
)
132135
self.assertEqual(res.status_code, 302)
133136
self.assertRedirects(res, '/list/authors/')
134-
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
137+
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
135138

136139
@unittest.expectedFailure
137140
def test_update_put(self):
@@ -154,7 +157,7 @@ def test_update_put(self):
154157
# See also #12635
155158
self.assertEqual(res.status_code, 302)
156159
self.assertRedirects(res, '/list/authors/')
157-
self.assertQuerysetEqual(
160+
self.assertQuerySetEqual(
158161
Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']
159162
)
160163

@@ -171,7 +174,7 @@ def test_update_invalid(self):
171174
self.assertEqual(res.status_code, 200)
172175
self.assertTemplateUsed(res, 'views/author_form.html')
173176
self.assertEqual(len(res.context['form'].errors), 1)
174-
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
177+
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
175178

176179
def test_update_with_object_url(self):
177180
a = Artist.objects.create(id='1', name='Rene Magritte')
@@ -180,7 +183,7 @@ def test_update_with_object_url(self):
180183
)
181184
self.assertEqual(res.status_code, 302)
182185
self.assertRedirects(res, '/detail/artist/%s/' % a.pk)
183-
self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
186+
self.assertQuerySetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
184187

185188
def test_update_with_redirect(self):
186189
a = Author.objects.create(
@@ -194,7 +197,7 @@ def test_update_with_redirect(self):
194197
)
195198
self.assertEqual(res.status_code, 302)
196199
self.assertRedirects(res, '/edit/authors/create/')
197-
self.assertQuerysetEqual(
200+
self.assertQuerySetEqual(
198201
Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']
199202
)
200203

@@ -208,7 +211,7 @@ def test_update_with_interpolated_redirect(self):
208211
'/edit/author/%s/update/interpolate_redirect/' % a.pk,
209212
{'id': '1', 'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'},
210213
)
211-
self.assertQuerysetEqual(
214+
self.assertQuerySetEqual(
212215
Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']
213216
)
214217
self.assertEqual(res.status_code, 302)
@@ -235,7 +238,7 @@ def test_update_with_special_properties(self):
235238
)
236239
self.assertEqual(res.status_code, 302)
237240
self.assertRedirects(res, '/detail/author/%s/' % a.pk)
238-
self.assertQuerysetEqual(
241+
self.assertQuerySetEqual(
239242
Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']
240243
)
241244

@@ -271,7 +274,7 @@ def test_update_get_object(self):
271274
)
272275
self.assertEqual(res.status_code, 302)
273276
self.assertRedirects(res, '/list/authors/')
274-
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
277+
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
275278

276279

277280
class DeleteViewTests(MongoTestCase):
@@ -290,22 +293,22 @@ def test_delete_by_post(self):
290293
res = self.client.post('/edit/author/%s/delete/' % a.pk)
291294
self.assertEqual(res.status_code, 302)
292295
self.assertRedirects(res, '/list/authors/')
293-
self.assertQuerysetEqual(Author.objects.all(), [])
296+
self.assertQuerySetEqual(Author.objects.all(), [])
294297

295298
def test_delete_by_delete(self):
296299
# Deletion with browser compatible DELETE method
297300
a = Author.objects.create(**{'id': '1', 'name': 'Randall Munroe', 'slug': 'randall-munroe'})
298301
res = self.client.delete('/edit/author/%s/delete/' % a.pk)
299302
self.assertEqual(res.status_code, 302)
300303
self.assertRedirects(res, '/list/authors/')
301-
self.assertQuerysetEqual(Author.objects.all(), [])
304+
self.assertQuerySetEqual(Author.objects.all(), [])
302305

303306
def test_delete_with_redirect(self):
304307
a = Author.objects.create(**{'id': '1', 'name': 'Randall Munroe', 'slug': 'randall-munroe'})
305308
res = self.client.post('/edit/author/%s/delete/redirect/' % a.pk)
306309
self.assertEqual(res.status_code, 302)
307310
self.assertRedirects(res, '/edit/authors/create/')
308-
self.assertQuerysetEqual(Author.objects.all(), [])
311+
self.assertQuerySetEqual(Author.objects.all(), [])
309312

310313
def test_delete_with_special_properties(self):
311314
a = Author.objects.create(**{'id': '1', 'name': 'Randall Munroe', 'slug': 'randall-munroe'})
@@ -319,7 +322,7 @@ def test_delete_with_special_properties(self):
319322
res = self.client.post('/edit/author/%s/delete/special/' % a.pk)
320323
self.assertEqual(res.status_code, 302)
321324
self.assertRedirects(res, '/list/authors/')
322-
self.assertQuerysetEqual(Author.objects.all(), [])
325+
self.assertQuerySetEqual(Author.objects.all(), [])
323326

324327
def test_delete_without_redirect(self):
325328
try:

0 commit comments

Comments
 (0)