Skip to content

Commit

Permalink
Fixed #23567 -- Made assertQuerysetEqual check Counter equality when …
Browse files Browse the repository at this point in the history
…ordered=False
  • Loading branch information
Thomas Chaumeny authored and kswiat committed Oct 7, 2014
1 parent 66a850d commit ec2a8f6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion django/test/testcases.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

from collections import Counter
from copy import copy
import difflib
import errno
Expand Down Expand Up @@ -871,7 +872,7 @@ def _fixture_teardown(self):
def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True, msg=None):
items = six.moves.map(transform, qs)
if not ordered:
return self.assertEqual(set(items), set(values), msg=msg)
return self.assertEqual(Counter(items), Counter(values), msg=msg)
values = list(values)
# For example qs.iterator() could be passed as qs, but it does not
# have 'ordered' attribute.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_utils/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class Car(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name

@python_2_unicode_compatible
class Person(models.Model):
name = models.CharField(max_length=100)
cars = models.ManyToManyField(Car, through='PossessedCar')

def __str__(self):
return self.name

@python_2_unicode_compatible
class PossessedCar(models.Model):
car = models.ForeignKey(Car)
belongs_to = models.ForeignKey(Person)

def __str__(self):
return self.color
29 changes: 28 additions & 1 deletion tests/test_utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.test.utils import CaptureQueriesContext, override_settings
from django.utils import six

from .models import Person
from .models import Car, Person, PossessedCar
from .views import empty_response


Expand Down Expand Up @@ -178,6 +178,33 @@ def test_undefined_order(self):
[repr(self.p1)]
)

def test_repeated_values(self):
"""
Test that assertQuerysetEqual checks the number of appearance of each item
when used with option ordered=False.
"""
batmobile = Car.objects.create(name='Batmobile')
k2000 = Car.objects.create(name='K 2000')
PossessedCar.objects.bulk_create([
PossessedCar(car=batmobile, belongs_to=self.p1),
PossessedCar(car=batmobile, belongs_to=self.p1),
PossessedCar(car=k2000, belongs_to=self.p1),
PossessedCar(car=k2000, belongs_to=self.p1),
PossessedCar(car=k2000, belongs_to=self.p1),
PossessedCar(car=k2000, belongs_to=self.p1),
])
with self.assertRaises(AssertionError):
self.assertQuerysetEqual(
self.p1.cars.all(),
[repr(batmobile), repr(k2000)],
ordered=False
)
self.assertQuerysetEqual(
self.p1.cars.all(),
[repr(batmobile)] * 2 + [repr(k2000)] * 4,
ordered=False
)


@override_settings(ROOT_URLCONF='test_utils.urls')
class CaptureQueriesContextManagerTests(TestCase):
Expand Down

0 comments on commit ec2a8f6

Please sign in to comment.