-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa1d367
commit 1f16978
Showing
4 changed files
with
75 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# coding: utf-8 | ||
|
||
from __future__ import unicode_literals | ||
try: | ||
from unittest import skipIf | ||
except ImportError: # For Python 2.6 | ||
from unittest2 import skipIf | ||
|
||
from django.conf import settings | ||
from django.db import DEFAULT_DB_ALIAS, connections | ||
from django.test import TransactionTestCase | ||
|
||
from .models import Test | ||
|
||
|
||
@skipIf(len(settings.DATABASES) == 1, | ||
'We can’t change the DB used since there’s only one configured') | ||
class MultiDatabaseTestCase(TransactionTestCase): | ||
multi_db = True | ||
|
||
def setUp(self): | ||
self.t1 = Test.objects.create(name='test1') | ||
self.t2 = Test.objects.create(name='test2') | ||
self.db_alias2 = [alias for alias in settings.DATABASES | ||
if alias != DEFAULT_DB_ALIAS][0] | ||
self.is_sqlite2 = connections[self.db_alias2].vendor == 'sqlite' | ||
|
||
def test_read(self): | ||
with self.assertNumQueries(1): | ||
data1 = list(Test.objects.all()) | ||
self.assertListEqual(data1, [self.t1, self.t2]) | ||
|
||
with self.assertNumQueries(1, using=self.db_alias2): | ||
data2 = list(Test.objects.using(self.db_alias2)) | ||
self.assertListEqual(data2, []) | ||
|
||
with self.assertNumQueries(0, using=self.db_alias2): | ||
data3 = list(Test.objects.using(self.db_alias2)) | ||
self.assertListEqual(data3, []) | ||
|
||
def test_invalidate_other_db(self): | ||
""" | ||
Tests if the non-default database is invalidated when modified. | ||
""" | ||
with self.assertNumQueries(1, using=self.db_alias2): | ||
data1 = list(Test.objects.using(self.db_alias2)) | ||
self.assertListEqual(data1, []) | ||
|
||
with self.assertNumQueries(2 if self.is_sqlite2 else 1, | ||
using=self.db_alias2): | ||
t3 = Test.objects.using(self.db_alias2).create(name='test3') | ||
|
||
with self.assertNumQueries(1, using=self.db_alias2): | ||
data2 = list(Test.objects.using(self.db_alias2)) | ||
self.assertListEqual(data2, [t3]) | ||
|
||
def test_invalidation_independence(self): | ||
""" | ||
Tests if invalidation doesn’t affect the unmodified databases. | ||
""" | ||
with self.assertNumQueries(1): | ||
data1 = list(Test.objects.all()) | ||
self.assertListEqual(data1, [self.t1, self.t2]) | ||
|
||
with self.assertNumQueries(2 if self.is_sqlite2 else 1, | ||
using=self.db_alias2): | ||
Test.objects.using(self.db_alias2).create(name='test3') | ||
|
||
with self.assertNumQueries(0): | ||
data2 = list(Test.objects.all()) | ||
self.assertListEqual(data2, [self.t1, self.t2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters