| 1788 | |
| 1789 | class Ticket13706DefaultDbRouter(object): |
| 1790 | """ |
| 1791 | Read and write only to the 'default' database. |
| 1792 | """ |
| 1793 | def db_for_read(self, model, **hints): |
| 1794 | return 'default' |
| 1795 | |
| 1796 | def db_for_write(self, model, **hints): |
| 1797 | return 'default' |
| 1798 | |
| 1799 | class Ticket13706OtherDbRouter(object): |
| 1800 | """ |
| 1801 | Read and write only to the 'other' database. |
| 1802 | """ |
| 1803 | def db_for_read(self, model, **hints): |
| 1804 | return 'other' |
| 1805 | |
| 1806 | def db_for_write(self, model, **hints): |
| 1807 | return 'other' |
| 1808 | |
| 1809 | class Ticket13706TestCase(TestCase): |
| 1810 | multi_db = True |
| 1811 | |
| 1812 | def setUp(self): |
| 1813 | self.old_routers = router.routers |
| 1814 | router.routers = [Ticket13706OtherDbRouter(), Ticket13706DefaultDbRouter()] |
| 1815 | |
| 1816 | def tearDown(self): |
| 1817 | router.routers = self.old_routers |
| 1818 | |
| 1819 | def test_ticket13706(self): |
| 1820 | t = Teacher.objects.create() |
| 1821 | p = Pupil.objects.create() |
| 1822 | t.pupils.add(p) |
| 1823 | self.assertEquals(t._state.db, 'other') |
| 1824 | self.assertEquals(p._state.db, 'other') |
| 1825 | self.assertEquals(Teacher.objects.using('default').count(), 0) |
| 1826 | self.assertEquals(Pupil.objects.using('default').count(), 0) |
| 1827 | |
| 1828 | t1 = Teacher() |
| 1829 | t1.save(using='other') |
| 1830 | p1 = Pupil() |
| 1831 | p1.save(using='other') |
| 1832 | t1.pupils.add(p1) |
| 1833 | self.assertEquals(t1._state.db, 'other') |
| 1834 | self.assertEquals(p1._state.db, 'other') |
| 1835 | self.assertEquals(Teacher.objects.using('default').count(), 0) |
| 1836 | self.assertEquals(Pupil.objects.using('default').count(), 0) |