| 1 |
from unittest import skipIf |
| 2 |
|
| 3 |
from django.conf import settings |
| 4 |
from django.db import DEFAULT_DB_ALIAS, connections, transaction |
| 5 |
from django.test import TransactionTestCase |
| 6 |
|
| 7 |
from .models import Test |
| 8 |
|
| 9 |
|
| 10 |
@skipIf(len(settings.DATABASES) == 1, |
| 11 |
'We can’t change the DB used since there’s only one configured') |
| 12 |
class MultiDatabaseTestCase(TransactionTestCase): |
| 13 |
multi_db = True |
| 14 |
databases = "__all__" |
| 15 |
|
| 16 |
def setUp(self): |
| 17 |
self.t1 = Test.objects.create(name='test1') |
| 18 |
self.t2 = Test.objects.create(name='test2') |
| 19 |
self.db_alias2 = next(alias for alias in settings.DATABASES |
| 20 |
if alias != DEFAULT_DB_ALIAS) |
| 21 |
connection2 = connections[self.db_alias2] |
| 22 |
self.is_sqlite2 = connection2.vendor == 'sqlite' |
| 23 |
self.is_mysql2 = connection2.vendor == 'mysql' |
| 24 |
if connection2.vendor in ('mysql', 'postgresql'): |
| 25 |
# We need to reopen the connection or Django |
| 26 |
# will execute an extra SQL request below. |
| 27 |
connection2.cursor() |
| 28 |
|
| 29 |
def test_read(self): |
| 30 |
with self.assertNumQueries(1): |
| 31 |
data1 = list(Test.objects.all()) |
| 32 |
self.assertListEqual(data1, [self.t1, self.t2]) |
| 33 |
|
| 34 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 35 |
data2 = list(Test.objects.using(self.db_alias2)) |
| 36 |
self.assertListEqual(data2, []) |
| 37 |
|
| 38 |
with self.assertNumQueries(0, using=self.db_alias2): |
| 39 |
data3 = list(Test.objects.using(self.db_alias2)) |
| 40 |
self.assertListEqual(data3, []) |
| 41 |
|
| 42 |
def test_invalidate_other_db(self): |
| 43 |
""" |
| 44 |
Tests if the non-default database is invalidated when modified. |
| 45 |
""" |
| 46 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 47 |
data1 = list(Test.objects.using(self.db_alias2)) |
| 48 |
self.assertListEqual(data1, []) |
| 49 |
|
| 50 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 51 |
t3 = Test.objects.using(self.db_alias2).create(name='test3') |
| 52 |
|
| 53 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 54 |
data2 = list(Test.objects.using(self.db_alias2)) |
| 55 |
self.assertListEqual(data2, [t3]) |
| 56 |
|
| 57 |
def test_invalidation_independence(self): |
| 58 |
""" |
| 59 |
Tests if invalidation doesn’t affect the unmodified databases. |
| 60 |
""" |
| 61 |
with self.assertNumQueries(1): |
| 62 |
data1 = list(Test.objects.all()) |
| 63 |
self.assertListEqual(data1, [self.t1, self.t2]) |
| 64 |
|
| 65 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 66 |
Test.objects.using(self.db_alias2).create(name='test3') |
| 67 |
|
| 68 |
with self.assertNumQueries(0): |
| 69 |
data2 = list(Test.objects.all()) |
| 70 |
self.assertListEqual(data2, [self.t1, self.t2]) |
| 71 |
|
| 72 |
def test_heterogeneous_atomics(self): |
| 73 |
""" |
| 74 |
Checks that an atomic block for a database nested inside |
| 75 |
another atomic block for another database has no impact on their |
| 76 |
caching. |
| 77 |
""" |
| 78 |
with transaction.atomic(): |
| 79 |
with transaction.atomic(self.db_alias2): |
| 80 |
with self.assertNumQueries(1): |
| 81 |
data1 = list(Test.objects.all()) |
| 82 |
self.assertListEqual(data1, [self.t1, self.t2]) |
| 83 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 84 |
data2 = list(Test.objects.using(self.db_alias2)) |
| 85 |
self.assertListEqual(data2, []) |
| 86 |
t3 = Test.objects.using(self.db_alias2).create(name='test3') |
| 87 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 88 |
data3 = list(Test.objects.using(self.db_alias2)) |
| 89 |
self.assertListEqual(data3, [t3]) |
| 90 |
|
| 91 |
with self.assertNumQueries(0): |
| 92 |
data4 = list(Test.objects.all()) |
| 93 |
self.assertListEqual(data4, [self.t1, self.t2]) |
| 94 |
|
| 95 |
with self.assertNumQueries(1): |
| 96 |
data5 = list(Test.objects.filter(name='test3')) |
| 97 |
self.assertListEqual(data5, []) |
| 98 |
|
| 99 |
def test_heterogeneous_atomics_independence(self): |
| 100 |
""" |
| 101 |
Checks that interrupting an atomic block after the commit of another |
| 102 |
atomic block for another database nested inside it |
| 103 |
correctly invalidates the cache for the committed transaction. |
| 104 |
""" |
| 105 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 106 |
data1 = list(Test.objects.using(self.db_alias2)) |
| 107 |
self.assertListEqual(data1, []) |
| 108 |
|
| 109 |
try: |
| 110 |
with transaction.atomic(): |
| 111 |
with transaction.atomic(self.db_alias2): |
| 112 |
t3 = Test.objects.using( |
| 113 |
self.db_alias2).create(name='test3') |
| 114 |
raise ZeroDivisionError |
| 115 |
except ZeroDivisionError: |
| 116 |
pass |
| 117 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 118 |
data2 = list(Test.objects.using(self.db_alias2)) |
| 119 |
self.assertListEqual(data2, [t3]) |