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