Ticket #13706: 13706_tests.diff

File 13706_tests.diff, 2.6 KB (added by Ramiro Morales, 13 years ago)

Patch for multi-db tests showing things work as expected

  • tests/regressiontests/multiple_database/models.py

    diff --git a/tests/regressiontests/multiple_database/models.py b/tests/regressiontests/multiple_database/models.py
    a b  
    7474
    7575    class Meta:
    7676        ordering = ('flavor',)
     77
     78class Pupil(models.Model):
     79    pass
     80
     81class Teacher(models.Model):
     82    pupils = models.ManyToManyField(Pupil)
  • tests/regressiontests/multiple_database/tests.py

    diff --git a/tests/regressiontests/multiple_database/tests.py b/tests/regressiontests/multiple_database/tests.py
    a b  
    1111from django.db.utils import ConnectionRouter
    1212from django.test import TestCase
    1313
    14 from models import Book, Person, Pet, Review, UserProfile
     14from models import Book, Person, Pet, Review, UserProfile, Pupil, Teacher
    1515
    1616try:
    1717    # we only have these models if the user is using multi-db, it's safe the
     
    17851785        b.authors.clear()
    17861786        self._write_to_default()
    17871787        self.assertEqual(receiver._database, "other")
     1788
     1789class 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
     1799class 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
     1809class 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)
Back to Top