diff options
author | Hasan Ramezani <hasan.r67@gmail.com> | 2020-10-18 18:29:52 +0200 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-11-06 09:24:50 +0100 |
commit | 3f7b3275627385f8f7531fca01cdda50d4ec6b6e (patch) | |
tree | fb082d40e73f6c877911eab92229ac21cdfaa5bc /tests/basic | |
parent | 13b6fff11703a694e155b84d41d02822bbc0aaa0 (diff) | |
download | django-3f7b3275627385f8f7531fca01cdda50d4ec6b6e.tar.gz |
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>
Diffstat (limited to 'tests/basic')
-rw-r--r-- | tests/basic/tests.py | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/tests/basic/tests.py b/tests/basic/tests.py index 173458f5ff..43c7ccdfa6 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -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) |