MCPcopy
hub / github.com/django/django / test_using_is_honored_m2m

Method test_using_is_honored_m2m

tests/prefetch_related/tests.py:1597–1645  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1595 databases = {"default", "other"}
1596
1597 def test_using_is_honored_m2m(self):
1598 B = Book.objects.using("other")
1599 A = Author.objects.using("other")
1600 book1 = B.create(title="Poems")
1601 book2 = B.create(title="Jane Eyre")
1602 book3 = B.create(title="Wuthering Heights")
1603 book4 = B.create(title="Sense and Sensibility")
1604
1605 author1 = A.create(name="Charlotte", first_book=book1)
1606 author2 = A.create(name="Anne", first_book=book1)
1607 author3 = A.create(name="Emily", first_book=book1)
1608 author4 = A.create(name="Jane", first_book=book4)
1609
1610 book1.authors.add(author1, author2, author3)
1611 book2.authors.add(author1)
1612 book3.authors.add(author3)
1613 book4.authors.add(author4)
1614
1615 # Forward
1616 qs1 = B.prefetch_related("authors")
1617 with self.assertNumQueries(2, using="other"):
1618 books = "".join(
1619 "%s (%s)\n"
1620 % (book.title, ", ".join(a.name for a in book.authors.all()))
1621 for book in qs1
1622 )
1623 self.assertEqual(
1624 books,
1625 "Poems (Charlotte, Anne, Emily)\n"
1626 "Jane Eyre (Charlotte)\n"
1627 "Wuthering Heights (Emily)\n"
1628 "Sense and Sensibility (Jane)\n",
1629 )
1630
1631 # Reverse
1632 qs2 = A.prefetch_related("books")
1633 with self.assertNumQueries(2, using="other"):
1634 authors = "".join(
1635 "%s: %s\n"
1636 % (author.name, ", ".join(b.title for b in author.books.all()))
1637 for author in qs2
1638 )
1639 self.assertEqual(
1640 authors,
1641 "Charlotte: Poems, Jane Eyre\n"
1642 "Anne: Poems\n"
1643 "Emily: Poems, Wuthering Heights\n"
1644 "Jane: Sense and Sensibility\n",
1645 )
1646
1647 def test_using_is_honored_fkey(self):
1648 B = Book.objects.using("other")

Callers

nothing calls this directly

Calls 7

assertNumQueriesMethod · 0.80
usingMethod · 0.45
createMethod · 0.45
addMethod · 0.45
prefetch_relatedMethod · 0.45
joinMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected