Queries are constrained to a single database
(self)
| 121 | router.db_for_read.assert_called_once_with(Book, instance=book) |
| 122 | |
| 123 | def test_basic_queries(self): |
| 124 | "Queries are constrained to a single database" |
| 125 | dive = Book.objects.using("other").create( |
| 126 | title="Dive into Python", published=datetime.date(2009, 5, 4) |
| 127 | ) |
| 128 | |
| 129 | dive = Book.objects.using("other").get(published=datetime.date(2009, 5, 4)) |
| 130 | self.assertEqual(dive.title, "Dive into Python") |
| 131 | with self.assertRaises(Book.DoesNotExist): |
| 132 | Book.objects.using("default").get(published=datetime.date(2009, 5, 4)) |
| 133 | |
| 134 | dive = Book.objects.using("other").get(title__icontains="dive") |
| 135 | self.assertEqual(dive.title, "Dive into Python") |
| 136 | with self.assertRaises(Book.DoesNotExist): |
| 137 | Book.objects.using("default").get(title__icontains="dive") |
| 138 | |
| 139 | dive = Book.objects.using("other").get(title__iexact="dive INTO python") |
| 140 | self.assertEqual(dive.title, "Dive into Python") |
| 141 | with self.assertRaises(Book.DoesNotExist): |
| 142 | Book.objects.using("default").get(title__iexact="dive INTO python") |
| 143 | |
| 144 | dive = Book.objects.using("other").get(published__year=2009) |
| 145 | self.assertEqual(dive.title, "Dive into Python") |
| 146 | self.assertEqual(dive.published, datetime.date(2009, 5, 4)) |
| 147 | with self.assertRaises(Book.DoesNotExist): |
| 148 | Book.objects.using("default").get(published__year=2009) |
| 149 | |
| 150 | years = Book.objects.using("other").dates("published", "year") |
| 151 | self.assertEqual([o.year for o in years], [2009]) |
| 152 | years = Book.objects.using("default").dates("published", "year") |
| 153 | self.assertEqual([o.year for o in years], []) |
| 154 | |
| 155 | months = Book.objects.using("other").dates("published", "month") |
| 156 | self.assertEqual([o.month for o in months], [5]) |
| 157 | months = Book.objects.using("default").dates("published", "month") |
| 158 | self.assertEqual([o.month for o in months], []) |
| 159 | |
| 160 | def test_m2m_separation(self): |
| 161 | "M2M fields are constrained to a single database" |