| 1573 | def test_related_writes_use_write_db_m2m_book_set(self): |
| 1574 | """Tests that queries on related managers use the write db when appropriate |
| 1575 | with a ManyToMany scenario. This is a book_set manager case""" |
| 1576 | # ian and his book get created in the default db |
| 1577 | ian = Person.objects.using(DEFAULT_DB_ALIAS).create(name="Ian Bicking") |
| 1578 | book = Book(title="Original Title", published=datetime.date(2009, 5, 4)) |
| 1579 | book.save(using=DEFAULT_DB_ALIAS) |
| 1580 | book.authors.db_manager(DEFAULT_DB_ALIAS).add(ian) |
| 1581 | |
| 1582 | # create a copy of ian and his book in the other db (as if we were in a master/slave relationship) |
| 1583 | other_ian = Person.objects.using('other').create(name="Ian Bicking") |
| 1584 | other_book = Book(title="Original Title", published=datetime.date(2009, 5, 4)) |
| 1585 | other_book.save(using='other') |
| 1586 | |
| 1587 | # force a relation on the other db |
| 1588 | Book.authors.through.objects.using('other').create(person=other_ian, book=other_book) |
| 1589 | |
| 1590 | self.assertEqual(book.authors.using('default').all().count(), 1) |
| 1591 | self.assertEqual(other_book.authors.using('other').all().count(), 1) |
| 1592 | self.assertEqual(ian.book_set.using('default').all().count(), 1) |
| 1593 | self.assertEqual(other_ian.book_set.using('other').all().count(), 1) |
| 1594 | |
| 1595 | # this is a ready so it should use the 'other' db |
| 1596 | related_manager = Person.objects.get(name="Ian Bicking").book_set |
| 1597 | ian_books = related_manager.all() |
| 1598 | self.assertEqual(ian_books.db, 'other') |
| 1599 | self.assertEqual(ian_books.count(), 1) |
| 1600 | |
| 1601 | ian_books.update(title='New Title') |
| 1602 | self.assertEqual(other_ian.book_set.using('other').get().title, 'Original Title') |
| 1603 | self.assertEqual(ian.book_set.using(DEFAULT_DB_ALIAS).get().title, 'New Title') |
| 1604 | ian_books.delete() |
| 1605 | self.assertEqual(other_ian.book_set.using('other').count(), 1) |
| 1606 | self.assertEqual(ian.book_set.using(DEFAULT_DB_ALIAS).count(), 0) |
| 1607 | |
| 1608 | def test_related_writes_use_write_db_m2m_authors(self): |
| 1609 | """Tests that queries on related managers use the write db when appropriate |
| 1610 | with a ManyToMany scenario. This is an authors manager case""" |
| 1611 | # ian and his book get created in the default db |
| 1612 | ian = Person.objects.using(DEFAULT_DB_ALIAS).create(name="Ian Bicking") |
| 1613 | book = Book(title="Original Title", published=datetime.date(2009, 5, 4)) |
| 1614 | book.save(using=DEFAULT_DB_ALIAS) |
| 1615 | book.authors.db_manager(DEFAULT_DB_ALIAS).add(ian) |
| 1616 | |
| 1617 | # create a copy of ian and his book in the other db (as if we were in a master/slave relationship) |
| 1618 | other_ian = Person.objects.using('other').create(name="Ian Bicking") |
| 1619 | other_book = Book(title="Original Title", published=datetime.date(2009, 5, 4)) |
| 1620 | other_book.save(using='other') |
| 1621 | |
| 1622 | # force a relation on the other db |
| 1623 | Book.authors.through.objects.using('other').create(person=other_ian, book=other_book) |
| 1624 | |
| 1625 | self.assertEqual(book.authors.using('default').all().count(), 1) |
| 1626 | self.assertEqual(other_book.authors.using('other').all().count(), 1) |
| 1627 | self.assertEqual(ian.book_set.using('default').all().count(), 1) |
| 1628 | self.assertEqual(other_ian.book_set.using('other').all().count(), 1) |
| 1629 | |
| 1630 | # this is a ready so it should use the 'other' db |
| 1631 | related_manager = Book.objects.get(title="Original Title").authors |
| 1632 | original_authors = related_manager.all() |
| 1633 | self.assertEqual(original_authors.db, 'other') |
| 1634 | self.assertEqual(original_authors.count(), 1) |
| 1635 | |
| 1636 | original_authors.update(name='Mark Pilgrim') |
| 1637 | self.assertEqual(other_book.authors.using('other').get().name, 'Ian Bicking') |
| 1638 | self.assertEqual(book.authors.using(DEFAULT_DB_ALIAS).get().name, 'Mark Pilgrim') |
| 1639 | original_authors.delete() |
| 1640 | self.assertEqual(other_book.authors.using('other').count(), 1) |
| 1641 | self.assertEqual(book.authors.using(DEFAULT_DB_ALIAS).count(), 0) |
| 1642 | |