1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #31235 -- Made assertQuerysetEqual() compare querysets directly.

This also replaces assertQuerysetEqual() to
assertSequenceEqual()/assertCountEqual() where appropriate.

Co-authored-by: Peter Inglesby <peter.inglesby@gmail.com>
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
This commit is contained in:
Hasan Ramezani
2020-10-18 18:29:52 +02:00
committed by Mariusz Felisiak
parent 13b6fff117
commit 3f7b327562
39 changed files with 1447 additions and 1801 deletions

View File

@@ -161,13 +161,11 @@ class ModelTest(TestCase):
Article(headline=headline, pub_date=some_pub_date).save()
self.assertQuerysetEqual(
Article.objects.all().order_by('headline'),
["<Article: Amazing article>",
"<Article: An article>",
"<Article: Article One>",
"<Article: Boring article>"]
sorted(headlines),
transform=lambda a: a.headline,
)
Article.objects.filter(headline__startswith='A').delete()
self.assertQuerysetEqual(Article.objects.all().order_by('headline'), ["<Article: Boring article>"])
self.assertEqual(Article.objects.get().headline, 'Boring article')
def test_not_equal_and_equal_operators_behave_as_expected_on_instances(self):
some_pub_date = datetime(2014, 5, 16, 12, 1)
@@ -208,17 +206,17 @@ class ModelTest(TestCase):
def test_year_lookup_edge_case(self):
# Edge-case test: A year lookup should retrieve all objects in
# the given year, including Jan. 1 and Dec. 31.
Article.objects.create(
a11 = Article.objects.create(
headline='Article 11',
pub_date=datetime(2008, 1, 1),
)
Article.objects.create(
a12 = Article.objects.create(
headline='Article 12',
pub_date=datetime(2008, 12, 31, 23, 59, 59, 999999),
)
self.assertQuerysetEqual(
self.assertSequenceEqual(
Article.objects.filter(pub_date__year=2008),
["<Article: Article 11>", "<Article: Article 12>"]
[a11, a12],
)
def test_unicode_data(self):
@@ -442,7 +440,7 @@ class ModelLookupTest(TestCase):
self.a.save()
# Article.objects.all() returns all the articles in the database.
self.assertQuerysetEqual(Article.objects.all(), ['<Article: Parrot programs in Python>'])
self.assertSequenceEqual(Article.objects.all(), [self.a])
def test_rich_lookup(self):
# Django provides a rich database lookup API.
@@ -458,24 +456,24 @@ class ModelLookupTest(TestCase):
self.assertEqual(Article.objects.get(id=self.a.id), self.a)
self.assertEqual(Article.objects.get(headline='Swallow programs in Python'), self.a)
self.assertQuerysetEqual(
self.assertSequenceEqual(
Article.objects.filter(pub_date__year=2005),
['<Article: Swallow programs in Python>'],
[self.a],
)
self.assertQuerysetEqual(
self.assertSequenceEqual(
Article.objects.filter(pub_date__year=2004),
[],
)
self.assertQuerysetEqual(
self.assertSequenceEqual(
Article.objects.filter(pub_date__year=2005, pub_date__month=7),
['<Article: Swallow programs in Python>'],
[self.a],
)
self.assertQuerysetEqual(
self.assertSequenceEqual(
Article.objects.filter(pub_date__week_day=5),
['<Article: Swallow programs in Python>'],
[self.a],
)
self.assertQuerysetEqual(
self.assertSequenceEqual(
Article.objects.filter(pub_date__week_day=6),
[],
)
@@ -499,7 +497,7 @@ class ModelLookupTest(TestCase):
self.assertEqual(Article.objects.get(pk=self.a.id), self.a)
# pk can be used as a shortcut for the primary key name in any query.
self.assertQuerysetEqual(Article.objects.filter(pk__in=[self.a.id]), ["<Article: Swallow programs in Python>"])
self.assertSequenceEqual(Article.objects.filter(pk__in=[self.a.id]), [self.a])
# Model instances of the same type and same ID are considered equal.
a = Article.objects.get(pk=self.a.id)