Skip to content

Commit

Permalink
fix: Fix queryset comparison for django 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
last-partizan committed Aug 16, 2023
1 parent 2f77a4a commit da5f2b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
7 changes: 6 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ class MongoTestCase(SimpleTestCase):
TestCase class that clear the collection between the tests
"""

assertQuerysetEqual = TransactionTestCase.__dict__['assertQuerysetEqual']
try:
assertQuerySetEqual = TransactionTestCase.__dict__['assertQuerySetEqual']
except KeyError:
# https://docs.djangoproject.com/en/4.2/topics/testing/tools/#django.test.TransactionTestCase.assertQuerySetEqual
# Drop this after supporting only django > 4.2
assertQuerySetEqual = TransactionTestCase.__dict__['assertQuerysetEqual']

def __init__(self, methodName='runtest'):
from django.conf import settings
Expand Down
45 changes: 24 additions & 21 deletions tests/views/edit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

import unittest

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


class CreateViewTests(MongoTestCase):
class ReprComparisonMixin:
def assertQuerySetEqual(self, qs, values, transform=repr, ordered=True, msg=None):
return super().assertQuerySetEqual(qs, values, transform, ordered, msg)


class CreateViewTests(ReprComparisonMixin, MongoTestCase):
def setUp(self):
Author.drop_collection()

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

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

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

def test_create_with_interpolated_redirect(self):
res = self.client.post(
'/edit/authors/create/interpolate_redirect/',
{'id': 1, 'name': 'Randall Munroe', 'slug': 'randall-munroe'},
)
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
self.assertEqual(res.status_code, 302)
pk = Author.objects.all()[0].pk
self.assertRedirects(res, '/edit/author/%s/update/' % pk)
Expand All @@ -92,7 +95,7 @@ def test_create_with_special_properties(self):
self.assertEqual(res.status_code, 302)
obj = Author.objects.get(slug='randall-munroe')
self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
self.assertQuerySetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])

def test_create_without_redirect(self):
try:
Expand All @@ -107,7 +110,7 @@ def test_create_without_redirect(self):
pass


class UpdateViewTests(TestCase):
class UpdateViewTests(ReprComparisonMixin, TestCase):
def setUp(self):
Author.drop_collection()

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

@unittest.expectedFailure
def test_update_put(self):
Expand All @@ -154,7 +157,7 @@ def test_update_put(self):
# See also #12635
self.assertEqual(res.status_code, 302)
self.assertRedirects(res, '/list/authors/')
self.assertQuerysetEqual(
self.assertQuerySetEqual(
Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']
)

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

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

def test_update_with_redirect(self):
a = Author.objects.create(
Expand All @@ -194,7 +197,7 @@ def test_update_with_redirect(self):
)
self.assertEqual(res.status_code, 302)
self.assertRedirects(res, '/edit/authors/create/')
self.assertQuerysetEqual(
self.assertQuerySetEqual(
Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']
)

Expand All @@ -208,7 +211,7 @@ def test_update_with_interpolated_redirect(self):
'/edit/author/%s/update/interpolate_redirect/' % a.pk,
{'id': '1', 'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'},
)
self.assertQuerysetEqual(
self.assertQuerySetEqual(
Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']
)
self.assertEqual(res.status_code, 302)
Expand All @@ -235,7 +238,7 @@ def test_update_with_special_properties(self):
)
self.assertEqual(res.status_code, 302)
self.assertRedirects(res, '/detail/author/%s/' % a.pk)
self.assertQuerysetEqual(
self.assertQuerySetEqual(
Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']
)

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


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

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

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

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

def test_delete_without_redirect(self):
try:
Expand Down

0 comments on commit da5f2b5

Please sign in to comment.