Ticket #32089: 32089-test.diff

File 32089-test.diff, 2.2 KB (added by Mariusz Felisiak, 3 years ago)
  • tests/prefetch_related/tests.py

    diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
    index c368e040ea..b5a41b8881 100644
    a b class DirectPrefetchedObjectCacheReuseTests(TestCase):  
    15251525            self.assertEqual(book1.first_authors[1].happy_place, [])
    15261526            self.assertEqual(book2.first_authors[0].happy_place, [self.author2_address1])
    15271527
     1528    def test_detect_is_fetched_with_to_attr_multiple(self):
     1529        self.book1.authors.add(self.author11, self.author12)
     1530        self.book2.authors.add(self.author21)
     1531
     1532        with self.assertNumQueries(3):
     1533            books = Book.objects.filter(
     1534                title__in=['book1', 'book2'],
     1535                authors__name__startswith='Author',
     1536            ).prefetch_related(
     1537                Prefetch(
     1538                    'first_time_authors',
     1539                    Author.objects.prefetch_related(
     1540                        Prefetch(
     1541                            'addresses',
     1542                            AuthorAddress.objects.filter(address='Happy place'),
     1543                            to_attr='happy_place',
     1544                        )
     1545                    ),
     1546                    to_attr='first_authors',
     1547                ),
     1548            )
     1549            book1_1, book1_2, book2 = list(books)
     1550
     1551        with self.assertNumQueries(0):
     1552            self.assertEqual(book1_1.first_authors, [self.author11, self.author12])
     1553            self.assertEqual(book1_2.first_authors, [self.author11, self.author12])
     1554            self.assertEqual(book2.first_authors, [self.author21])
     1555
     1556            self.assertEqual(book1_1.first_authors[0].happy_place, [self.author1_address1])
     1557            self.assertEqual(book1_1.first_authors[1].happy_place, [])
     1558            self.assertEqual(book1_2.first_authors[0].happy_place, [self.author1_address1])
     1559            self.assertEqual(book1_2.first_authors[1].happy_place, [])
     1560            self.assertEqual(book2.first_authors[0].happy_place, [self.author2_address1])
     1561
    15281562    def test_prefetch_reverse_foreign_key(self):
    15291563        with self.assertNumQueries(2):
    15301564            bookwithyear1, = BookWithYear.objects.prefetch_related('bookreview_set')
Back to Top