diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /tests/prefetch_related/test_prefetch_related_objects.py | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/prefetch_related/test_prefetch_related_objects.py')
| -rw-r--r-- | tests/prefetch_related/test_prefetch_related_objects.py | 76 |
1 files changed, 45 insertions, 31 deletions
diff --git a/tests/prefetch_related/test_prefetch_related_objects.py b/tests/prefetch_related/test_prefetch_related_objects.py index 20d4da5a0a..ca1f904c52 100644 --- a/tests/prefetch_related/test_prefetch_related_objects.py +++ b/tests/prefetch_related/test_prefetch_related_objects.py @@ -9,25 +9,26 @@ class PrefetchRelatedObjectsTests(TestCase): Since prefetch_related_objects() is just the inner part of prefetch_related(), only do basic tests to ensure its API hasn't changed. """ + @classmethod def setUpTestData(cls): - cls.book1 = Book.objects.create(title='Poems') - cls.book2 = Book.objects.create(title='Jane Eyre') - cls.book3 = Book.objects.create(title='Wuthering Heights') - cls.book4 = Book.objects.create(title='Sense and Sensibility') + cls.book1 = Book.objects.create(title="Poems") + cls.book2 = Book.objects.create(title="Jane Eyre") + cls.book3 = Book.objects.create(title="Wuthering Heights") + cls.book4 = Book.objects.create(title="Sense and Sensibility") - cls.author1 = Author.objects.create(name='Charlotte', first_book=cls.book1) - cls.author2 = Author.objects.create(name='Anne', first_book=cls.book1) - cls.author3 = Author.objects.create(name='Emily', first_book=cls.book1) - cls.author4 = Author.objects.create(name='Jane', first_book=cls.book4) + cls.author1 = Author.objects.create(name="Charlotte", first_book=cls.book1) + cls.author2 = Author.objects.create(name="Anne", first_book=cls.book1) + cls.author3 = Author.objects.create(name="Emily", first_book=cls.book1) + cls.author4 = Author.objects.create(name="Jane", first_book=cls.book4) cls.book1.authors.add(cls.author1, cls.author2, cls.author3) cls.book2.authors.add(cls.author1) cls.book3.authors.add(cls.author3) cls.book4.authors.add(cls.author4) - cls.reader1 = Reader.objects.create(name='Amy') - cls.reader2 = Reader.objects.create(name='Belinda') + cls.reader1 = Reader.objects.create(name="Amy") + cls.reader2 = Reader.objects.create(name="Belinda") cls.reader1.books_read.add(cls.book1, cls.book4) cls.reader2.books_read.add(cls.book2, cls.book4) @@ -35,20 +36,22 @@ class PrefetchRelatedObjectsTests(TestCase): def test_unknown(self): book1 = Book.objects.get(id=self.book1.id) with self.assertRaises(AttributeError): - prefetch_related_objects([book1], 'unknown_attribute') + prefetch_related_objects([book1], "unknown_attribute") def test_m2m_forward(self): book1 = Book.objects.get(id=self.book1.id) with self.assertNumQueries(1): - prefetch_related_objects([book1], 'authors') + prefetch_related_objects([book1], "authors") with self.assertNumQueries(0): - self.assertCountEqual(book1.authors.all(), [self.author1, self.author2, self.author3]) + self.assertCountEqual( + book1.authors.all(), [self.author1, self.author2, self.author3] + ) def test_m2m_reverse(self): author1 = Author.objects.get(id=self.author1.id) with self.assertNumQueries(1): - prefetch_related_objects([author1], 'books') + prefetch_related_objects([author1], "books") with self.assertNumQueries(0): self.assertCountEqual(author1.books.all(), [self.book1, self.book2]) @@ -56,7 +59,7 @@ class PrefetchRelatedObjectsTests(TestCase): def test_foreignkey_forward(self): authors = list(Author.objects.all()) with self.assertNumQueries(1): - prefetch_related_objects(authors, 'first_book') + prefetch_related_objects(authors, "first_book") with self.assertNumQueries(0): [author.first_book for author in authors] @@ -64,7 +67,7 @@ class PrefetchRelatedObjectsTests(TestCase): def test_foreignkey_reverse(self): books = list(Book.objects.all()) with self.assertNumQueries(1): - prefetch_related_objects(books, 'first_time_authors') + prefetch_related_objects(books, "first_time_authors") with self.assertNumQueries(0): [list(book.first_time_authors.all()) for book in books] @@ -73,7 +76,7 @@ class PrefetchRelatedObjectsTests(TestCase): """A m2m can be followed through another m2m.""" authors = list(Author.objects.all()) with self.assertNumQueries(2): - prefetch_related_objects(authors, 'books__read_by') + prefetch_related_objects(authors, "books__read_by") with self.assertNumQueries(0): self.assertEqual( @@ -82,38 +85,44 @@ class PrefetchRelatedObjectsTests(TestCase): for a in authors ], [ - [['Amy'], ['Belinda']], # Charlotte - Poems, Jane Eyre - [['Amy']], # Anne - Poems - [['Amy'], []], # Emily - Poems, Wuthering Heights - [['Amy', 'Belinda']], # Jane - Sense and Sense - ] + [["Amy"], ["Belinda"]], # Charlotte - Poems, Jane Eyre + [["Amy"]], # Anne - Poems + [["Amy"], []], # Emily - Poems, Wuthering Heights + [["Amy", "Belinda"]], # Jane - Sense and Sense + ], ) def test_prefetch_object(self): book1 = Book.objects.get(id=self.book1.id) with self.assertNumQueries(1): - prefetch_related_objects([book1], Prefetch('authors')) + prefetch_related_objects([book1], Prefetch("authors")) with self.assertNumQueries(0): - self.assertCountEqual(book1.authors.all(), [self.author1, self.author2, self.author3]) + self.assertCountEqual( + book1.authors.all(), [self.author1, self.author2, self.author3] + ) def test_prefetch_object_twice(self): book1 = Book.objects.get(id=self.book1.id) book2 = Book.objects.get(id=self.book2.id) with self.assertNumQueries(1): - prefetch_related_objects([book1], Prefetch('authors')) + prefetch_related_objects([book1], Prefetch("authors")) with self.assertNumQueries(1): - prefetch_related_objects([book1, book2], Prefetch('authors')) + prefetch_related_objects([book1, book2], Prefetch("authors")) with self.assertNumQueries(0): self.assertCountEqual(book2.authors.all(), [self.author1]) def test_prefetch_object_to_attr(self): book1 = Book.objects.get(id=self.book1.id) with self.assertNumQueries(1): - prefetch_related_objects([book1], Prefetch('authors', to_attr='the_authors')) + prefetch_related_objects( + [book1], Prefetch("authors", to_attr="the_authors") + ) with self.assertNumQueries(0): - self.assertCountEqual(book1.the_authors, [self.author1, self.author2, self.author3]) + self.assertCountEqual( + book1.the_authors, [self.author1, self.author2, self.author3] + ) def test_prefetch_object_to_attr_twice(self): book1 = Book.objects.get(id=self.book1.id) @@ -121,12 +130,12 @@ class PrefetchRelatedObjectsTests(TestCase): with self.assertNumQueries(1): prefetch_related_objects( [book1], - Prefetch('authors', to_attr='the_authors'), + Prefetch("authors", to_attr="the_authors"), ) with self.assertNumQueries(1): prefetch_related_objects( [book1, book2], - Prefetch('authors', to_attr='the_authors'), + Prefetch("authors", to_attr="the_authors"), ) with self.assertNumQueries(0): self.assertCountEqual(book2.the_authors, [self.author1]) @@ -136,7 +145,12 @@ class PrefetchRelatedObjectsTests(TestCase): with self.assertNumQueries(1): prefetch_related_objects( [book1], - Prefetch('authors', queryset=Author.objects.filter(id__in=[self.author1.id, self.author2.id])) + Prefetch( + "authors", + queryset=Author.objects.filter( + id__in=[self.author1.id, self.author2.id] + ), + ), ) with self.assertNumQueries(0): |
