| 1548 | def test_related_writes_use_write_db(self): |
| 1549 | """Tests that queries on related managers use the write db when appropriate""" |
| 1550 | |
| 1551 | # mark and his book get created in the default db |
| 1552 | mark = Person.objects.using(DEFAULT_DB_ALIAS).create(name="Mark Pilgrim") |
| 1553 | Book.objects.using(DEFAULT_DB_ALIAS).create(editor=mark, title="Original Title", |
| 1554 | published=datetime.date(2009, 5, 4)) |
| 1555 | self.assertEqual(mark.edited.using(DEFAULT_DB_ALIAS).count(), 1) |
| 1556 | # create a copy of mark and his book in the other db (as if we were in a master/slave relationship) |
| 1557 | other_mark = Person.objects.using('other').create(name="Mark Pilgrim") |
| 1558 | Book.objects.using('other').create(editor=other_mark, title="Original Title", |
| 1559 | published=datetime.date(2009, 5, 4)) |
| 1560 | self.assertEqual(other_mark.edited.using('other').count(), 1) |
| 1561 | # this is a ready so it should use the 'other' db |
| 1562 | marks_books = Person.objects.get(name="Mark Pilgrim").edited.all() |
| 1563 | # this alternative syntax for the above line makes the test pass: |
| 1564 | # marks_books = Book.objects.filter(editor__name="Mark Pilgrim") |
| 1565 | self.assertEqual(marks_books.db, 'other') |
| 1566 | # when we execute a write, it should switch to the 'default' db, |
| 1567 | # thereby updating or deleting the books in the 'default' db and leaving |
| 1568 | # the books in the 'other' db untouched |
| 1569 | marks_books = marks_books.update(title='New Title') |
| 1570 | self.assertEqual(other_mark.edited.using('other').get().title, 'Original Title') |
| 1571 | self.assertEqual(mark.edited.using(DEFAULT_DB_ALIAS).get().title, 'New Title') |
| 1572 | marks_books.delete() |
| 1573 | self.assertEqual(other_mark.edited.using('other').count(), 1) |
| 1574 | self.assertEqual(mark.edited.using(DEFAULT_DB_ALIAS).count(), 0) |
| 1575 | |
| 1576 | |