| 1 |
import copy |
| 2 |
import datetime |
| 3 |
import os |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
from django.db import DEFAULT_DB_ALIAS, connection, connections |
| 7 |
from django.db.backends.base.creation import TEST_DATABASE_PREFIX, BaseDatabaseCreation |
| 8 |
from django.test import SimpleTestCase, TransactionTestCase |
| 9 |
from django.test.utils import override_settings |
| 10 |
|
| 11 |
from ..models import ( |
| 12 |
CircularA, |
| 13 |
CircularB, |
| 14 |
Object, |
| 15 |
ObjectReference, |
| 16 |
ObjectSelfReference, |
| 17 |
SchoolBus, |
| 18 |
SchoolClass, |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
def get_connection_copy(): |
| 23 |
# Get a copy of the default connection. (Can't use django.db.connection |
| 24 |
# because it'll modify the default connection itself.) |
| 25 |
test_connection = copy.copy(connections[DEFAULT_DB_ALIAS]) |
| 26 |
test_connection.settings_dict = copy.deepcopy( |
| 27 |
connections[DEFAULT_DB_ALIAS].settings_dict |
| 28 |
) |
| 29 |
return test_connection |
| 30 |
|
| 31 |
|
| 32 |
class TestDbSignatureTests(SimpleTestCase): |
| 33 |
def test_default_name(self): |
| 34 |
# A test db name isn't set. |
| 35 |
prod_name = "hodor" |
| 36 |
test_connection = get_connection_copy() |
| 37 |
test_connection.settings_dict["NAME"] = prod_name |
| 38 |
test_connection.settings_dict["TEST"] = {"NAME": None} |
| 39 |
signature = BaseDatabaseCreation(test_connection).test_db_signature() |
| 40 |
self.assertEqual(signature[3], TEST_DATABASE_PREFIX + prod_name) |
| 41 |
|
| 42 |
def test_custom_test_name(self): |
| 43 |
# A regular test db name is set. |
| 44 |
test_name = "hodor" |
| 45 |
test_connection = get_connection_copy() |
| 46 |
test_connection.settings_dict["TEST"] = {"NAME": test_name} |
| 47 |
signature = BaseDatabaseCreation(test_connection).test_db_signature() |
| 48 |
self.assertEqual(signature[3], test_name) |
| 49 |
|
| 50 |
def test_custom_test_name_with_test_prefix(self): |
| 51 |
# A test db name prefixed with TEST_DATABASE_PREFIX is set. |
| 52 |
test_name = TEST_DATABASE_PREFIX + "hodor" |
| 53 |
test_connection = get_connection_copy() |
| 54 |
test_connection.settings_dict["TEST"] = {"NAME": test_name} |
| 55 |
signature = BaseDatabaseCreation(test_connection).test_db_signature() |
| 56 |
self.assertEqual(signature[3], test_name) |
| 57 |
|
| 58 |
|
| 59 |
@override_settings(INSTALLED_APPS=["backends.base.app_unmigrated"]) |
| 60 |
@mock.patch.object(connection, "ensure_connection") |
| 61 |
@mock.patch.object(connection, "prepare_database") |
| 62 |
@mock.patch( |
| 63 |
"django.db.migrations.recorder.MigrationRecorder.has_table", return_value=False |
| 64 |
) |
| 65 |
@mock.patch("django.core.management.commands.migrate.Command.sync_apps") |
| 66 |
class TestDbCreationTests(SimpleTestCase): |
| 67 |
available_apps = ["backends.base.app_unmigrated"] |
| 68 |
|
| 69 |
@mock.patch("django.db.migrations.executor.MigrationExecutor.migrate") |
| 70 |
def test_migrate_test_setting_false( |
| 71 |
self, mocked_migrate, mocked_sync_apps, *mocked_objects |
| 72 |
): |
| 73 |
test_connection = get_connection_copy() |
| 74 |
test_connection.settings_dict["TEST"]["MIGRATE"] = False |
| 75 |
creation = test_connection.creation_class(test_connection) |
| 76 |
if connection.vendor == "oracle": |
| 77 |
# Don't close connection on Oracle. |
| 78 |
creation.connection.close = mock.Mock() |
| 79 |
old_database_name = test_connection.settings_dict["NAME"] |
| 80 |
try: |
| 81 |
with mock.patch.object(creation, "_create_test_db"): |
| 82 |
creation.create_test_db(verbosity=0, autoclobber=True, serialize=False) |
| 83 |
# Migrations don't run. |
| 84 |
mocked_migrate.assert_called() |
| 85 |
args, kwargs = mocked_migrate.call_args |
| 86 |
self.assertEqual(args, ([],)) |
| 87 |
self.assertEqual(kwargs["plan"], []) |
| 88 |
# App is synced. |
| 89 |
mocked_sync_apps.assert_called() |
| 90 |
mocked_args, _ = mocked_sync_apps.call_args |
| 91 |
self.assertEqual(mocked_args[1], {"app_unmigrated"}) |
| 92 |
finally: |
| 93 |
with mock.patch.object(creation, "_destroy_test_db"): |
| 94 |
creation.destroy_test_db(old_database_name, verbosity=0) |
| 95 |
|
| 96 |
@mock.patch("django.db.migrations.executor.MigrationRecorder.ensure_schema") |
| 97 |
def test_migrate_test_setting_false_ensure_schema( |
| 98 |
self, |
| 99 |
mocked_ensure_schema, |
| 100 |
mocked_sync_apps, |
| 101 |
*mocked_objects, |
| 102 |
): |
| 103 |
test_connection = get_connection_copy() |
| 104 |
test_connection.settings_dict["TEST"]["MIGRATE"] = False |
| 105 |
creation = test_connection.creation_class(test_connection) |
| 106 |
if connection.vendor == "oracle": |
| 107 |
# Don't close connection on Oracle. |
| 108 |
creation.connection.close = mock.Mock() |
| 109 |
old_database_name = test_connection.settings_dict["NAME"] |
| 110 |
try: |
| 111 |
with mock.patch.object(creation, "_create_test_db"): |
| 112 |
creation.create_test_db(verbosity=0, autoclobber=True, serialize=False) |
| 113 |
# The django_migrations table is not created. |
| 114 |
mocked_ensure_schema.assert_not_called() |
| 115 |
# App is synced. |
| 116 |
mocked_sync_apps.assert_called() |
| 117 |
mocked_args, _ = mocked_sync_apps.call_args |
| 118 |
self.assertEqual(mocked_args[1], {"app_unmigrated"}) |
| 119 |
finally: |
| 120 |
with mock.patch.object(creation, "_destroy_test_db"): |
| 121 |
creation.destroy_test_db(old_database_name, verbosity=0) |
| 122 |
|
| 123 |
@mock.patch("django.db.migrations.executor.MigrationExecutor.migrate") |
| 124 |
def test_migrate_test_setting_true( |
| 125 |
self, mocked_migrate, mocked_sync_apps, *mocked_objects |
| 126 |
): |
| 127 |
test_connection = get_connection_copy() |
| 128 |
test_connection.settings_dict["TEST"]["MIGRATE"] = True |
| 129 |
creation = test_connection.creation_class(test_connection) |
| 130 |
if connection.vendor == "oracle": |
| 131 |
# Don't close connection on Oracle. |
| 132 |
creation.connection.close = mock.Mock() |
| 133 |
old_database_name = test_connection.settings_dict["NAME"] |
| 134 |
try: |
| 135 |
with mock.patch.object(creation, "_create_test_db"): |
| 136 |
creation.create_test_db(verbosity=0, autoclobber=True, serialize=False) |
| 137 |
# Migrations run. |
| 138 |
mocked_migrate.assert_called() |
| 139 |
args, kwargs = mocked_migrate.call_args |
| 140 |
self.assertEqual(args, ([("app_unmigrated", "0001_initial")],)) |
| 141 |
self.assertEqual(len(kwargs["plan"]), 1) |
| 142 |
# App is not synced. |
| 143 |
mocked_sync_apps.assert_not_called() |
| 144 |
finally: |
| 145 |
with mock.patch.object(creation, "_destroy_test_db"): |
| 146 |
creation.destroy_test_db(old_database_name, verbosity=0) |
| 147 |
|
| 148 |
@mock.patch.dict(os.environ, {"RUNNING_DJANGOS_TEST_SUITE": ""}) |
| 149 |
@mock.patch("django.db.migrations.executor.MigrationExecutor.migrate") |
| 150 |
@mock.patch.object(BaseDatabaseCreation, "mark_expected_failures_and_skips") |
| 151 |
def test_mark_expected_failures_and_skips_call( |
| 152 |
self, mark_expected_failures_and_skips, *mocked_objects |
| 153 |
): |
| 154 |
""" |
| 155 |
mark_expected_failures_and_skips() isn't called unless |
| 156 |
RUNNING_DJANGOS_TEST_SUITE is 'true'. |
| 157 |
""" |
| 158 |
test_connection = get_connection_copy() |
| 159 |
creation = test_connection.creation_class(test_connection) |
| 160 |
if connection.vendor == "oracle": |
| 161 |
# Don't close connection on Oracle. |
| 162 |
creation.connection.close = mock.Mock() |
| 163 |
old_database_name = test_connection.settings_dict["NAME"] |
| 164 |
try: |
| 165 |
with mock.patch.object(creation, "_create_test_db"): |
| 166 |
creation.create_test_db(verbosity=0, autoclobber=True, serialize=False) |
| 167 |
self.assertIs(mark_expected_failures_and_skips.called, False) |
| 168 |
finally: |
| 169 |
with mock.patch.object(creation, "_destroy_test_db"): |
| 170 |
creation.destroy_test_db(old_database_name, verbosity=0) |
| 171 |
|
| 172 |
|
| 173 |
class TestDeserializeDbFromString(TransactionTestCase): |
| 174 |
available_apps = ["backends"] |
| 175 |
|
| 176 |
def test_circular_reference(self): |
| 177 |
# deserialize_db_from_string() handles circular references. |
| 178 |
data = """ |
| 179 |
[ |
| 180 |
{ |
| 181 |
"model": "backends.object", |
| 182 |
"pk": 1, |
| 183 |
"fields": {"obj_ref": 1, "related_objects": []} |
| 184 |
}, |
| 185 |
{ |
| 186 |
"model": "backends.objectreference", |
| 187 |
"pk": 1, |
| 188 |
"fields": {"obj": 1} |
| 189 |
} |
| 190 |
] |
| 191 |
""" |
| 192 |
connection.creation.deserialize_db_from_string(data) |
| 193 |
obj = Object.objects.get() |
| 194 |
obj_ref = ObjectReference.objects.get() |
| 195 |
self.assertEqual(obj.obj_ref, obj_ref) |
| 196 |
self.assertEqual(obj_ref.obj, obj) |
| 197 |
|
| 198 |
def test_self_reference(self): |
| 199 |
# serialize_db_to_string() and deserialize_db_from_string() handles |
| 200 |
# self references. |
| 201 |
obj_1 = ObjectSelfReference.objects.create(key="X") |
| 202 |
obj_2 = ObjectSelfReference.objects.create(key="Y", obj=obj_1) |
| 203 |
obj_1.obj = obj_2 |
| 204 |
obj_1.save() |
| 205 |
# Serialize objects. |
| 206 |
with mock.patch("django.db.migrations.loader.MigrationLoader") as loader: |
| 207 |
# serialize_db_to_string() serializes only migrated apps, so mark |
| 208 |
# the backends app as migrated. |
| 209 |
loader_instance = loader.return_value |
| 210 |
loader_instance.migrated_apps = {"backends"} |
| 211 |
data = connection.creation.serialize_db_to_string() |
| 212 |
ObjectSelfReference.objects.all().delete() |
| 213 |
# Deserialize objects. |
| 214 |
connection.creation.deserialize_db_from_string(data) |
| 215 |
obj_1 = ObjectSelfReference.objects.get(key="X") |
| 216 |
obj_2 = ObjectSelfReference.objects.get(key="Y") |
| 217 |
self.assertEqual(obj_1.obj, obj_2) |
| 218 |
self.assertEqual(obj_2.obj, obj_1) |
| 219 |
|
| 220 |
def test_circular_reference_with_natural_key(self): |
| 221 |
# serialize_db_to_string() and deserialize_db_from_string() handles |
| 222 |
# circular references for models with natural keys. |
| 223 |
obj_a = CircularA.objects.create(key="A") |
| 224 |
obj_b = CircularB.objects.create(key="B", obj=obj_a) |
| 225 |
obj_a.obj = obj_b |
| 226 |
obj_a.save() |
| 227 |
# Serialize objects. |
| 228 |
with mock.patch("django.db.migrations.loader.MigrationLoader") as loader: |
| 229 |
# serialize_db_to_string() serializes only migrated apps, so mark |
| 230 |
# the backends app as migrated. |
| 231 |
loader_instance = loader.return_value |
| 232 |
loader_instance.migrated_apps = {"backends"} |
| 233 |
data = connection.creation.serialize_db_to_string() |
| 234 |
CircularA.objects.all().delete() |
| 235 |
CircularB.objects.all().delete() |
| 236 |
# Deserialize objects. |
| 237 |
connection.creation.deserialize_db_from_string(data) |
| 238 |
obj_a = CircularA.objects.get() |
| 239 |
obj_b = CircularB.objects.get() |
| 240 |
self.assertEqual(obj_a.obj, obj_b) |
| 241 |
self.assertEqual(obj_b.obj, obj_a) |
| 242 |
|
| 243 |
def test_serialize_db_to_string_base_manager(self): |
| 244 |
SchoolClass.objects.create(year=1000, last_updated=datetime.datetime.now()) |
| 245 |
with mock.patch("django.db.migrations.loader.MigrationLoader") as loader: |
| 246 |
# serialize_db_to_string() serializes only migrated apps, so mark |
| 247 |
# the backends app as migrated. |
| 248 |
loader_instance = loader.return_value |
| 249 |
loader_instance.migrated_apps = {"backends"} |
| 250 |
data = connection.creation.serialize_db_to_string() |
| 251 |
self.assertIn('"model": "backends.schoolclass"', data) |
| 252 |
self.assertIn('"year": 1000', data) |
| 253 |
|
| 254 |
def test_serialize_db_to_string_base_manager_with_prefetch_related(self): |
| 255 |
sclass = SchoolClass.objects.create( |
| 256 |
year=2000, last_updated=datetime.datetime.now() |
| 257 |
) |
| 258 |
bus = SchoolBus.objects.create(number=1) |
| 259 |
bus.schoolclasses.add(sclass) |
| 260 |
with mock.patch("django.db.migrations.loader.MigrationLoader") as loader: |
| 261 |
# serialize_db_to_string() serializes only migrated apps, so mark |
| 262 |
# the backends app as migrated. |
| 263 |
loader_instance = loader.return_value |
| 264 |
loader_instance.migrated_apps = {"backends"} |
| 265 |
data = connection.creation.serialize_db_to_string() |
| 266 |
self.assertIn('"model": "backends.schoolbus"', data) |
| 267 |
self.assertIn('"model": "backends.schoolclass"', data) |
| 268 |
self.assertIn(f'"schoolclasses": [{sclass.pk}]', data) |
| 269 |
|
| 270 |
|
| 271 |
class SkipTestClass: |
| 272 |
def skip_function(self): |
| 273 |
pass |
| 274 |
|
| 275 |
|
| 276 |
def skip_test_function(): |
| 277 |
pass |
| 278 |
|
| 279 |
|
| 280 |
def expected_failure_test_function(): |
| 281 |
pass |
| 282 |
|
| 283 |
|
| 284 |
class TestMarkTests(SimpleTestCase): |
| 285 |
def test_mark_expected_failures_and_skips(self): |
| 286 |
test_connection = get_connection_copy() |
| 287 |
creation = BaseDatabaseCreation(test_connection) |
| 288 |
creation.connection.features.django_test_expected_failures = { |
| 289 |
"backends.base.test_creation.expected_failure_test_function", |
| 290 |
} |
| 291 |
creation.connection.features.django_test_skips = { |
| 292 |
"skip test class": { |
| 293 |
"backends.base.test_creation.SkipTestClass", |
| 294 |
}, |
| 295 |
"skip test function": { |
| 296 |
"backends.base.test_creation.skip_test_function", |
| 297 |
}, |
| 298 |
} |
| 299 |
creation.mark_expected_failures_and_skips() |
| 300 |
self.assertIs( |
| 301 |
expected_failure_test_function.__unittest_expecting_failure__, |
| 302 |
True, |
| 303 |
) |
| 304 |
self.assertIs(SkipTestClass.__unittest_skip__, True) |
| 305 |
self.assertEqual( |
| 306 |
SkipTestClass.__unittest_skip_why__, |
| 307 |
"skip test class", |
| 308 |
) |
| 309 |
self.assertIs(skip_test_function.__unittest_skip__, True) |
| 310 |
self.assertEqual( |
| 311 |
skip_test_function.__unittest_skip_why__, |
| 312 |
"skip test function", |
| 313 |
) |