| 1 |
# Unittests for fixtures. |
| 2 |
import json |
| 3 |
import os |
| 4 |
import re |
| 5 |
import unittest |
| 6 |
from io import StringIO |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
from django.core import management, serializers |
| 10 |
from django.core.exceptions import ImproperlyConfigured |
| 11 |
from django.core.serializers.base import DeserializationError |
| 12 |
from django.db import IntegrityError, transaction |
| 13 |
from django.db.models import signals |
| 14 |
from django.test import ( |
| 15 |
TestCase, |
| 16 |
TransactionTestCase, |
| 17 |
override_settings, |
| 18 |
skipIfDBFeature, |
| 19 |
skipUnlessDBFeature, |
| 20 |
) |
| 21 |
|
| 22 |
from .models import ( |
| 23 |
Absolute, |
| 24 |
Animal, |
| 25 |
Article, |
| 26 |
Book, |
| 27 |
Child, |
| 28 |
Circle1, |
| 29 |
Circle2, |
| 30 |
Circle3, |
| 31 |
ExternalDependency, |
| 32 |
M2MCircular1ThroughAB, |
| 33 |
M2MCircular1ThroughBC, |
| 34 |
M2MCircular1ThroughCA, |
| 35 |
M2MCircular2ThroughAB, |
| 36 |
M2MComplexA, |
| 37 |
M2MComplexB, |
| 38 |
M2MComplexCircular1A, |
| 39 |
M2MComplexCircular1B, |
| 40 |
M2MComplexCircular1C, |
| 41 |
M2MComplexCircular2A, |
| 42 |
M2MComplexCircular2B, |
| 43 |
M2MSimpleA, |
| 44 |
M2MSimpleB, |
| 45 |
M2MSimpleCircularA, |
| 46 |
M2MSimpleCircularB, |
| 47 |
M2MThroughAB, |
| 48 |
NaturalKeyWithFKDependency, |
| 49 |
NKChild, |
| 50 |
Parent, |
| 51 |
Person, |
| 52 |
RefToNKChild, |
| 53 |
Store, |
| 54 |
Stuff, |
| 55 |
Thingy, |
| 56 |
Widget, |
| 57 |
) |
| 58 |
|
| 59 |
try: |
| 60 |
import yaml # NOQA |
| 61 |
|
| 62 |
HAS_YAML = True |
| 63 |
except ImportError: |
| 64 |
HAS_YAML = False |
| 65 |
|
| 66 |
_cur_dir = os.path.dirname(os.path.abspath(__file__)) |
| 67 |
|
| 68 |
|
| 69 |
class TestFixtures(TestCase): |
| 70 |
def animal_pre_save_check(self, signal, sender, instance, **kwargs): |
| 71 |
self.pre_save_checks.append( |
| 72 |
( |
| 73 |
"Count = %s (%s)" % (instance.count, type(instance.count)), |
| 74 |
"Weight = %s (%s)" % (instance.weight, type(instance.weight)), |
| 75 |
) |
| 76 |
) |
| 77 |
|
| 78 |
def test_duplicate_pk(self): |
| 79 |
""" |
| 80 |
This is a regression test for ticket #3790. |
| 81 |
""" |
| 82 |
# Load a fixture that uses PK=1 |
| 83 |
management.call_command( |
| 84 |
"loaddata", |
| 85 |
"sequence", |
| 86 |
verbosity=0, |
| 87 |
) |
| 88 |
|
| 89 |
# Create a new animal. Without a sequence reset, this new object |
| 90 |
# will take a PK of 1 (on Postgres), and the save will fail. |
| 91 |
|
| 92 |
animal = Animal( |
| 93 |
name="Platypus", |
| 94 |
latin_name="Ornithorhynchus anatinus", |
| 95 |
count=2, |
| 96 |
weight=2.2, |
| 97 |
) |
| 98 |
animal.save() |
| 99 |
self.assertGreater(animal.id, 1) |
| 100 |
|
| 101 |
def test_loaddata_not_found_fields_not_ignore(self): |
| 102 |
""" |
| 103 |
Test for ticket #9279 -- Error is raised for entries in |
| 104 |
the serialized data for fields that have been removed |
| 105 |
from the database when not ignored. |
| 106 |
""" |
| 107 |
test_fixtures = [ |
| 108 |
"sequence_extra", |
| 109 |
"sequence_extra_jsonl", |
| 110 |
] |
| 111 |
if HAS_YAML: |
| 112 |
test_fixtures.append("sequence_extra_yaml") |
| 113 |
for fixture_file in test_fixtures: |
| 114 |
with ( |
| 115 |
self.subTest(fixture_file=fixture_file), |
| 116 |
self.assertRaises(DeserializationError), |
| 117 |
): |
| 118 |
management.call_command( |
| 119 |
"loaddata", |
| 120 |
fixture_file, |
| 121 |
verbosity=0, |
| 122 |
) |
| 123 |
|
| 124 |
def test_loaddata_not_found_fields_ignore(self): |
| 125 |
""" |
| 126 |
Test for ticket #9279 -- Ignores entries in |
| 127 |
the serialized data for fields that have been removed |
| 128 |
from the database. |
| 129 |
""" |
| 130 |
management.call_command( |
| 131 |
"loaddata", |
| 132 |
"sequence_extra", |
| 133 |
ignore=True, |
| 134 |
verbosity=0, |
| 135 |
) |
| 136 |
self.assertEqual(Animal.specimens.all()[0].name, "Lion") |
| 137 |
|
| 138 |
def test_loaddata_not_found_fields_ignore_xml(self): |
| 139 |
""" |
| 140 |
Test for ticket #19998 -- Ignore entries in the XML serialized data |
| 141 |
for fields that have been removed from the model definition. |
| 142 |
""" |
| 143 |
management.call_command( |
| 144 |
"loaddata", |
| 145 |
"sequence_extra_xml", |
| 146 |
ignore=True, |
| 147 |
verbosity=0, |
| 148 |
) |
| 149 |
self.assertEqual(Animal.specimens.all()[0].name, "Wolf") |
| 150 |
|
| 151 |
def test_loaddata_not_found_fields_ignore_jsonl(self): |
| 152 |
management.call_command( |
| 153 |
"loaddata", |
| 154 |
"sequence_extra_jsonl", |
| 155 |
ignore=True, |
| 156 |
verbosity=0, |
| 157 |
) |
| 158 |
self.assertEqual(Animal.specimens.all()[0].name, "Eagle") |
| 159 |
|
| 160 |
@unittest.skipUnless(HAS_YAML, "No yaml library detected") |
| 161 |
def test_loaddata_not_found_fields_ignore_yaml(self): |
| 162 |
management.call_command( |
| 163 |
"loaddata", |
| 164 |
"sequence_extra_yaml", |
| 165 |
ignore=True, |
| 166 |
verbosity=0, |
| 167 |
) |
| 168 |
self.assertEqual(Animal.specimens.all()[0].name, "Cat") |
| 169 |
|
| 170 |
def test_loaddata_empty_lines_jsonl(self): |
| 171 |
management.call_command( |
| 172 |
"loaddata", |
| 173 |
"sequence_empty_lines_jsonl.jsonl", |
| 174 |
verbosity=0, |
| 175 |
) |
| 176 |
self.assertEqual(Animal.specimens.all()[0].name, "Eagle") |
| 177 |
|
| 178 |
@skipIfDBFeature("interprets_empty_strings_as_nulls") |
| 179 |
def test_pretty_print_xml(self): |
| 180 |
""" |
| 181 |
Regression test for ticket #4558 -- pretty printing of XML fixtures |
| 182 |
doesn't affect parsing of None values. |
| 183 |
""" |
| 184 |
# Load a pretty-printed XML fixture with Nulls. |
| 185 |
management.call_command( |
| 186 |
"loaddata", |
| 187 |
"pretty.xml", |
| 188 |
verbosity=0, |
| 189 |
) |
| 190 |
self.assertIsNone(Stuff.objects.all()[0].name) |
| 191 |
self.assertIsNone(Stuff.objects.all()[0].owner) |
| 192 |
|
| 193 |
@skipUnlessDBFeature("interprets_empty_strings_as_nulls") |
| 194 |
def test_pretty_print_xml_empty_strings(self): |
| 195 |
""" |
| 196 |
Regression test for ticket #4558 -- pretty printing of XML fixtures |
| 197 |
doesn't affect parsing of None values. |
| 198 |
""" |
| 199 |
# Load a pretty-printed XML fixture with Nulls. |
| 200 |
management.call_command( |
| 201 |
"loaddata", |
| 202 |
"pretty.xml", |
| 203 |
verbosity=0, |
| 204 |
) |
| 205 |
self.assertEqual(Stuff.objects.all()[0].name, "") |
| 206 |
self.assertIsNone(Stuff.objects.all()[0].owner) |
| 207 |
|
| 208 |
def test_absolute_path(self): |
| 209 |
""" |
| 210 |
Regression test for ticket #6436 -- |
| 211 |
os.path.join will throw away the initial parts of a path if it |
| 212 |
encounters an absolute path. |
| 213 |
This means that if a fixture is specified as an absolute path, |
| 214 |
we need to make sure we don't discover the absolute path in every |
| 215 |
fixture directory. |
| 216 |
""" |
| 217 |
load_absolute_path = os.path.join( |
| 218 |
os.path.dirname(__file__), "fixtures", "absolute.json" |
| 219 |
) |
| 220 |
management.call_command( |
| 221 |
"loaddata", |
| 222 |
load_absolute_path, |
| 223 |
verbosity=0, |
| 224 |
) |
| 225 |
self.assertEqual(Absolute.objects.count(), 1) |
| 226 |
|
| 227 |
def test_relative_path(self, path=["fixtures", "absolute.json"]): |
| 228 |
relative_path = os.path.join(*path) |
| 229 |
cwd = os.getcwd() |
| 230 |
try: |
| 231 |
os.chdir(_cur_dir) |
| 232 |
management.call_command( |
| 233 |
"loaddata", |
| 234 |
relative_path, |
| 235 |
verbosity=0, |
| 236 |
) |
| 237 |
finally: |
| 238 |
os.chdir(cwd) |
| 239 |
self.assertEqual(Absolute.objects.count(), 1) |
| 240 |
|
| 241 |
@override_settings(FIXTURE_DIRS=[os.path.join(_cur_dir, "fixtures_1")]) |
| 242 |
def test_relative_path_in_fixture_dirs(self): |
| 243 |
self.test_relative_path(path=["inner", "absolute.json"]) |
| 244 |
|
| 245 |
def test_path_containing_dots(self): |
| 246 |
management.call_command( |
| 247 |
"loaddata", |
| 248 |
"path.containing.dots.json", |
| 249 |
verbosity=0, |
| 250 |
) |
| 251 |
self.assertEqual(Absolute.objects.count(), 1) |
| 252 |
|
| 253 |
def test_unknown_format(self): |
| 254 |
""" |
| 255 |
Test for ticket #4371 -- Loading data of an unknown format should fail |
| 256 |
Validate that error conditions are caught correctly |
| 257 |
""" |
| 258 |
msg = ( |
| 259 |
"Problem installing fixture 'bad_fix.ture1': unkn is not a known " |
| 260 |
"serialization format." |
| 261 |
) |
| 262 |
with self.assertRaisesMessage(management.CommandError, msg): |
| 263 |
management.call_command( |
| 264 |
"loaddata", |
| 265 |
"bad_fix.ture1.unkn", |
| 266 |
verbosity=0, |
| 267 |
) |
| 268 |
|
| 269 |
@override_settings(SERIALIZATION_MODULES={"unkn": "unexistent.path"}) |
| 270 |
def test_unimportable_serializer(self): |
| 271 |
""" |
| 272 |
Failing serializer import raises the proper error |
| 273 |
""" |
| 274 |
with self.assertRaisesMessage(ImportError, "No module named 'unexistent'"): |
| 275 |
management.call_command( |
| 276 |
"loaddata", |
| 277 |
"bad_fix.ture1.unkn", |
| 278 |
verbosity=0, |
| 279 |
) |
| 280 |
|
| 281 |
def test_invalid_data(self): |
| 282 |
""" |
| 283 |
Test for ticket #4371 -- Loading a fixture file with invalid data |
| 284 |
using explicit filename. |
| 285 |
Test for ticket #18213 -- warning conditions are caught correctly |
| 286 |
""" |
| 287 |
msg = "No fixture data found for 'bad_fixture2'. (File format may be invalid.)" |
| 288 |
with self.assertWarnsMessage(RuntimeWarning, msg): |
| 289 |
management.call_command( |
| 290 |
"loaddata", |
| 291 |
"bad_fixture2.xml", |
| 292 |
verbosity=0, |
| 293 |
) |
| 294 |
|
| 295 |
def test_invalid_data_no_ext(self): |
| 296 |
""" |
| 297 |
Test for ticket #4371 -- Loading a fixture file with invalid data |
| 298 |
without file extension. |
| 299 |
Test for ticket #18213 -- warning conditions are caught correctly |
| 300 |
""" |
| 301 |
msg = "No fixture data found for 'bad_fixture2'. (File format may be invalid.)" |
| 302 |
with self.assertWarnsMessage(RuntimeWarning, msg): |
| 303 |
management.call_command( |
| 304 |
"loaddata", |
| 305 |
"bad_fixture2", |
| 306 |
verbosity=0, |
| 307 |
) |
| 308 |
|
| 309 |
def test_empty(self): |
| 310 |
""" |
| 311 |
Test for ticket #18213 -- Loading a fixture file with no data output a warning. |
| 312 |
Previously empty fixture raises an error exception, see ticket #4371. |
| 313 |
""" |
| 314 |
msg = "No fixture data found for 'empty'. (File format may be invalid.)" |
| 315 |
with self.assertWarnsMessage(RuntimeWarning, msg): |
| 316 |
management.call_command( |
| 317 |
"loaddata", |
| 318 |
"empty", |
| 319 |
verbosity=0, |
| 320 |
) |
| 321 |
|
| 322 |
def test_error_message(self): |
| 323 |
""" |
| 324 |
Regression for #9011 - error message is correct. |
| 325 |
Change from error to warning for ticket #18213. |
| 326 |
""" |
| 327 |
msg = "No fixture data found for 'bad_fixture2'. (File format may be invalid.)" |
| 328 |
with self.assertWarnsMessage(RuntimeWarning, msg): |
| 329 |
management.call_command( |
| 330 |
"loaddata", |
| 331 |
"bad_fixture2", |
| 332 |
"animal", |
| 333 |
verbosity=0, |
| 334 |
) |
| 335 |
|
| 336 |
def test_pg_sequence_resetting_checks(self): |
| 337 |
""" |
| 338 |
Test for ticket #7565 -- PostgreSQL sequence resetting checks shouldn't |
| 339 |
ascend to parent models when inheritance is used |
| 340 |
(since they are treated individually). |
| 341 |
""" |
| 342 |
management.call_command( |
| 343 |
"loaddata", |
| 344 |
"model-inheritance.json", |
| 345 |
verbosity=0, |
| 346 |
) |
| 347 |
self.assertEqual(Parent.objects.all()[0].id, 1) |
| 348 |
self.assertEqual(Child.objects.all()[0].id, 1) |
| 349 |
|
| 350 |
def test_close_connection_after_loaddata(self): |
| 351 |
""" |
| 352 |
Test for ticket #7572 -- MySQL has a problem if the same connection is |
| 353 |
used to create tables, load data, and then query over that data. |
| 354 |
To compensate, we close the connection after running loaddata. |
| 355 |
This ensures that a new connection is opened when test queries are |
| 356 |
issued. |
| 357 |
""" |
| 358 |
management.call_command( |
| 359 |
"loaddata", |
| 360 |
"big-fixture.json", |
| 361 |
verbosity=0, |
| 362 |
) |
| 363 |
articles = Article.objects.exclude(id=9) |
| 364 |
self.assertEqual( |
| 365 |
list(articles.values_list("id", flat=True)), [1, 2, 3, 4, 5, 6, 7, 8] |
| 366 |
) |
| 367 |
# Just for good measure, run the same query again. |
| 368 |
# Under the influence of ticket #7572, this will |
| 369 |
# give a different result to the previous call. |
| 370 |
self.assertEqual( |
| 371 |
list(articles.values_list("id", flat=True)), [1, 2, 3, 4, 5, 6, 7, 8] |
| 372 |
) |
| 373 |
|
| 374 |
def test_field_value_coerce(self): |
| 375 |
""" |
| 376 |
Test for tickets #8298, #9942 - Field values should be coerced into the |
| 377 |
correct type by the deserializer, not as part of the database write. |
| 378 |
""" |
| 379 |
self.pre_save_checks = [] |
| 380 |
signals.pre_save.connect(self.animal_pre_save_check) |
| 381 |
try: |
| 382 |
management.call_command( |
| 383 |
"loaddata", |
| 384 |
"animal.xml", |
| 385 |
verbosity=0, |
| 386 |
) |
| 387 |
self.assertEqual( |
| 388 |
self.pre_save_checks, |
| 389 |
[("Count = 42 (<class 'int'>)", "Weight = 1.2 (<class 'float'>)")], |
| 390 |
) |
| 391 |
finally: |
| 392 |
signals.pre_save.disconnect(self.animal_pre_save_check) |
| 393 |
|
| 394 |
def test_dumpdata_uses_default_manager(self): |
| 395 |
""" |
| 396 |
Regression for #11286 |
| 397 |
Dumpdata honors the default manager. Dump the current contents of |
| 398 |
the database as a JSON fixture |
| 399 |
""" |
| 400 |
management.call_command( |
| 401 |
"loaddata", |
| 402 |
"animal.xml", |
| 403 |
verbosity=0, |
| 404 |
) |
| 405 |
management.call_command( |
| 406 |
"loaddata", |
| 407 |
"sequence.json", |
| 408 |
verbosity=0, |
| 409 |
) |
| 410 |
animal = Animal( |
| 411 |
name="Platypus", |
| 412 |
latin_name="Ornithorhynchus anatinus", |
| 413 |
count=2, |
| 414 |
weight=2.2, |
| 415 |
) |
| 416 |
animal.save() |
| 417 |
|
| 418 |
out = StringIO() |
| 419 |
management.call_command( |
| 420 |
"dumpdata", |
| 421 |
"fixtures_regress.animal", |
| 422 |
format="json", |
| 423 |
stdout=out, |
| 424 |
) |
| 425 |
|
| 426 |
# Output order isn't guaranteed, so check for parts |
| 427 |
data = out.getvalue() |
| 428 |
|
| 429 |
# Get rid of artifacts like '000000002' to eliminate the differences |
| 430 |
# between different Python versions. |
| 431 |
data = re.sub("0{6,}[0-9]", "", data) |
| 432 |
|
| 433 |
animals_data = sorted( |
| 434 |
[ |
| 435 |
{ |
| 436 |
"pk": 1, |
| 437 |
"model": "fixtures_regress.animal", |
| 438 |
"fields": { |
| 439 |
"count": 3, |
| 440 |
"weight": 1.2, |
| 441 |
"name": "Lion", |
| 442 |
"latin_name": "Panthera leo", |
| 443 |
}, |
| 444 |
}, |
| 445 |
{ |
| 446 |
"pk": 10, |
| 447 |
"model": "fixtures_regress.animal", |
| 448 |
"fields": { |
| 449 |
"count": 42, |
| 450 |
"weight": 1.2, |
| 451 |
"name": "Emu", |
| 452 |
"latin_name": "Dromaius novaehollandiae", |
| 453 |
}, |
| 454 |
}, |
| 455 |
{ |
| 456 |
"pk": animal.pk, |
| 457 |
"model": "fixtures_regress.animal", |
| 458 |
"fields": { |
| 459 |
"count": 2, |
| 460 |
"weight": 2.2, |
| 461 |
"name": "Platypus", |
| 462 |
"latin_name": "Ornithorhynchus anatinus", |
| 463 |
}, |
| 464 |
}, |
| 465 |
], |
| 466 |
key=lambda x: x["pk"], |
| 467 |
) |
| 468 |
|
| 469 |
data = sorted(json.loads(data), key=lambda x: x["pk"]) |
| 470 |
|
| 471 |
self.maxDiff = 1024 |
| 472 |
self.assertEqual(data, animals_data) |
| 473 |
|
| 474 |
def test_proxy_model_included(self): |
| 475 |
""" |
| 476 |
Regression for #11428 - Proxy models aren't included when you dumpdata |
| 477 |
""" |
| 478 |
out = StringIO() |
| 479 |
# Create an instance of the concrete class |
| 480 |
widget = Widget.objects.create(name="grommet") |
| 481 |
management.call_command( |
| 482 |
"dumpdata", |
| 483 |
"fixtures_regress.widget", |
| 484 |
"fixtures_regress.widgetproxy", |
| 485 |
format="json", |
| 486 |
stdout=out, |
| 487 |
) |
| 488 |
self.assertJSONEqual( |
| 489 |
out.getvalue(), |
| 490 |
'[{"pk": %d, "model": "fixtures_regress.widget", ' |
| 491 |
'"fields": {"name": "grommet"}}]' % widget.pk, |
| 492 |
) |
| 493 |
|
| 494 |
@skipUnlessDBFeature("supports_forward_references") |
| 495 |
def test_loaddata_works_when_fixture_has_forward_refs(self): |
| 496 |
""" |
| 497 |
Forward references cause fixtures not to load in MySQL (InnoDB). |
| 498 |
""" |
| 499 |
management.call_command( |
| 500 |
"loaddata", |
| 501 |
"forward_ref.json", |
| 502 |
verbosity=0, |
| 503 |
) |
| 504 |
self.assertEqual(Book.objects.all()[0].id, 1) |
| 505 |
self.assertEqual(Person.objects.all()[0].id, 4) |
| 506 |
|
| 507 |
def test_loaddata_raises_error_when_fixture_has_invalid_foreign_key(self): |
| 508 |
""" |
| 509 |
Data with nonexistent child key references raises error. |
| 510 |
""" |
| 511 |
with self.assertRaisesMessage(IntegrityError, "Problem installing fixture"): |
| 512 |
management.call_command( |
| 513 |
"loaddata", |
| 514 |
"forward_ref_bad_data.json", |
| 515 |
verbosity=0, |
| 516 |
) |
| 517 |
|
| 518 |
@skipUnlessDBFeature("supports_forward_references") |
| 519 |
@override_settings( |
| 520 |
FIXTURE_DIRS=[ |
| 521 |
os.path.join(_cur_dir, "fixtures_1"), |
| 522 |
os.path.join(_cur_dir, "fixtures_2"), |
| 523 |
] |
| 524 |
) |
| 525 |
def test_loaddata_forward_refs_split_fixtures(self): |
| 526 |
""" |
| 527 |
Regression for #17530 - should be able to cope with forward references |
| 528 |
when the fixtures are not in the same files or directories. |
| 529 |
""" |
| 530 |
management.call_command( |
| 531 |
"loaddata", |
| 532 |
"forward_ref_1.json", |
| 533 |
"forward_ref_2.json", |
| 534 |
verbosity=0, |
| 535 |
) |
| 536 |
self.assertEqual(Book.objects.all()[0].id, 1) |
| 537 |
self.assertEqual(Person.objects.all()[0].id, 4) |
| 538 |
|
| 539 |
def test_loaddata_no_fixture_specified(self): |
| 540 |
""" |
| 541 |
Error is quickly reported when no fixtures is provided in the command |
| 542 |
line. |
| 543 |
""" |
| 544 |
msg = ( |
| 545 |
"No database fixture specified. Please provide the path of at least one " |
| 546 |
"fixture in the command line." |
| 547 |
) |
| 548 |
with self.assertRaisesMessage(management.CommandError, msg): |
| 549 |
management.call_command( |
| 550 |
"loaddata", |
| 551 |
verbosity=0, |
| 552 |
) |
| 553 |
|
| 554 |
def test_ticket_20820(self): |
| 555 |
""" |
| 556 |
Regression for ticket #20820 -- loaddata on a model that inherits |
| 557 |
from a model with a M2M shouldn't blow up. |
| 558 |
""" |
| 559 |
management.call_command( |
| 560 |
"loaddata", |
| 561 |
"special-article.json", |
| 562 |
verbosity=0, |
| 563 |
) |
| 564 |
|
| 565 |
def test_ticket_22421(self): |
| 566 |
""" |
| 567 |
Regression for ticket #22421 -- loaddata on a model that inherits from |
| 568 |
a grand-parent model with a M2M but via an abstract parent shouldn't |
| 569 |
blow up. |
| 570 |
""" |
| 571 |
management.call_command( |
| 572 |
"loaddata", |
| 573 |
"feature.json", |
| 574 |
verbosity=0, |
| 575 |
) |
| 576 |
|
| 577 |
def test_loaddata_with_m2m_to_self(self): |
| 578 |
""" |
| 579 |
Regression test for ticket #17946. |
| 580 |
""" |
| 581 |
management.call_command( |
| 582 |
"loaddata", |
| 583 |
"m2mtoself.json", |
| 584 |
verbosity=0, |
| 585 |
) |
| 586 |
|
| 587 |
@override_settings( |
| 588 |
FIXTURE_DIRS=[ |
| 589 |
os.path.join(_cur_dir, "fixtures_1"), |
| 590 |
os.path.join(_cur_dir, "fixtures_1"), |
| 591 |
] |
| 592 |
) |
| 593 |
def test_fixture_dirs_with_duplicates(self): |
| 594 |
""" |
| 595 |
settings.FIXTURE_DIRS cannot contain duplicates in order to avoid |
| 596 |
repeated fixture loading. |
| 597 |
""" |
| 598 |
with self.assertRaisesMessage( |
| 599 |
ImproperlyConfigured, "settings.FIXTURE_DIRS contains duplicates." |
| 600 |
): |
| 601 |
management.call_command("loaddata", "absolute.json", verbosity=0) |
| 602 |
|
| 603 |
@override_settings(FIXTURE_DIRS=[os.path.join(_cur_dir, "fixtures")]) |
| 604 |
def test_fixture_dirs_with_default_fixture_path(self): |
| 605 |
""" |
| 606 |
settings.FIXTURE_DIRS cannot contain a default fixtures directory |
| 607 |
for application (app/fixtures) in order to avoid repeated fixture loading. |
| 608 |
""" |
| 609 |
msg = ( |
| 610 |
"'%s' is a default fixture directory for the '%s' app " |
| 611 |
"and cannot be listed in settings.FIXTURE_DIRS." |
| 612 |
% (os.path.join(_cur_dir, "fixtures"), "fixtures_regress") |
| 613 |
) |
| 614 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 615 |
management.call_command("loaddata", "absolute.json", verbosity=0) |
| 616 |
|
| 617 |
@override_settings(FIXTURE_DIRS=[Path(_cur_dir) / "fixtures"]) |
| 618 |
def test_fixture_dirs_with_default_fixture_path_as_pathlib(self): |
| 619 |
""" |
| 620 |
settings.FIXTURE_DIRS cannot contain a default fixtures directory |
| 621 |
for application (app/fixtures) in order to avoid repeated fixture loading. |
| 622 |
""" |
| 623 |
msg = ( |
| 624 |
"'%s' is a default fixture directory for the '%s' app " |
| 625 |
"and cannot be listed in settings.FIXTURE_DIRS." |
| 626 |
% (os.path.join(_cur_dir, "fixtures"), "fixtures_regress") |
| 627 |
) |
| 628 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 629 |
management.call_command("loaddata", "absolute.json", verbosity=0) |
| 630 |
|
| 631 |
@override_settings( |
| 632 |
FIXTURE_DIRS=[ |
| 633 |
os.path.join(_cur_dir, "fixtures_1"), |
| 634 |
os.path.join(_cur_dir, "fixtures_2"), |
| 635 |
] |
| 636 |
) |
| 637 |
def test_loaddata_with_valid_fixture_dirs(self): |
| 638 |
management.call_command( |
| 639 |
"loaddata", |
| 640 |
"absolute.json", |
| 641 |
verbosity=0, |
| 642 |
) |
| 643 |
|
| 644 |
@override_settings(FIXTURE_DIRS=[Path(_cur_dir) / "fixtures_1"]) |
| 645 |
def test_fixtures_dir_pathlib(self): |
| 646 |
management.call_command("loaddata", "inner/absolute.json", verbosity=0) |
| 647 |
self.assertQuerySetEqual(Absolute.objects.all(), [1], transform=lambda o: o.pk) |
| 648 |
|
| 649 |
|
| 650 |
class NaturalKeyFixtureTests(TestCase): |
| 651 |
def test_nk_deserialize(self): |
| 652 |
""" |
| 653 |
Test for ticket #13030 - Python based parser version |
| 654 |
natural keys deserialize with fk to inheriting model |
| 655 |
""" |
| 656 |
management.call_command( |
| 657 |
"loaddata", |
| 658 |
"model-inheritance.json", |
| 659 |
verbosity=0, |
| 660 |
) |
| 661 |
management.call_command( |
| 662 |
"loaddata", |
| 663 |
"nk-inheritance.json", |
| 664 |
verbosity=0, |
| 665 |
) |
| 666 |
self.assertEqual(NKChild.objects.get(pk=1).data, "apple") |
| 667 |
|
| 668 |
self.assertEqual(RefToNKChild.objects.get(pk=1).nk_fk.data, "apple") |
| 669 |
|
| 670 |
def test_nk_deserialize_xml(self): |
| 671 |
""" |
| 672 |
Test for ticket #13030 - XML version |
| 673 |
natural keys deserialize with fk to inheriting model |
| 674 |
""" |
| 675 |
management.call_command( |
| 676 |
"loaddata", |
| 677 |
"model-inheritance.json", |
| 678 |
verbosity=0, |
| 679 |
) |
| 680 |
management.call_command( |
| 681 |
"loaddata", |
| 682 |
"nk-inheritance.json", |
| 683 |
verbosity=0, |
| 684 |
) |
| 685 |
management.call_command( |
| 686 |
"loaddata", |
| 687 |
"nk-inheritance2.xml", |
| 688 |
verbosity=0, |
| 689 |
) |
| 690 |
self.assertEqual(NKChild.objects.get(pk=2).data, "banana") |
| 691 |
self.assertEqual(RefToNKChild.objects.get(pk=2).nk_fk.data, "apple") |
| 692 |
|
| 693 |
def test_nk_on_serialize(self): |
| 694 |
""" |
| 695 |
Natural key requirements are taken into account when serializing models. |
| 696 |
""" |
| 697 |
management.call_command( |
| 698 |
"loaddata", |
| 699 |
"forward_ref_lookup.json", |
| 700 |
verbosity=0, |
| 701 |
) |
| 702 |
|
| 703 |
out = StringIO() |
| 704 |
management.call_command( |
| 705 |
"dumpdata", |
| 706 |
"fixtures_regress.book", |
| 707 |
"fixtures_regress.person", |
| 708 |
"fixtures_regress.store", |
| 709 |
verbosity=0, |
| 710 |
format="json", |
| 711 |
use_natural_foreign_keys=True, |
| 712 |
use_natural_primary_keys=True, |
| 713 |
stdout=out, |
| 714 |
) |
| 715 |
self.assertJSONEqual( |
| 716 |
out.getvalue(), |
| 717 |
""" |
| 718 |
[{"fields": {"main": null, "name": "Amazon"}, |
| 719 |
"model": "fixtures_regress.store"}, |
| 720 |
{"fields": {"main": null, "name": "Borders"}, |
| 721 |
"model": "fixtures_regress.store"}, |
| 722 |
{"fields": {"name": "Neal Stephenson"}, "model": "fixtures_regress.person"}, |
| 723 |
{"pk": 1, "model": "fixtures_regress.book", |
| 724 |
"fields": {"stores": [["Amazon"], ["Borders"]], |
| 725 |
"name": "Cryptonomicon", "author": ["Neal Stephenson"]}}] |
| 726 |
""", |
| 727 |
) |
| 728 |
|
| 729 |
def test_dependency_sorting(self): |
| 730 |
""" |
| 731 |
It doesn't matter what order you mention the models, Store *must* be |
| 732 |
serialized before then Person, and both must be serialized before Book. |
| 733 |
""" |
| 734 |
sorted_deps = serializers.sort_dependencies( |
| 735 |
[("fixtures_regress", [Book, Person, Store])] |
| 736 |
) |
| 737 |
self.assertEqual(sorted_deps, [Store, Person, Book]) |
| 738 |
|
| 739 |
def test_dependency_sorting_2(self): |
| 740 |
sorted_deps = serializers.sort_dependencies( |
| 741 |
[("fixtures_regress", [Book, Store, Person])] |
| 742 |
) |
| 743 |
self.assertEqual(sorted_deps, [Store, Person, Book]) |
| 744 |
|
| 745 |
def test_dependency_sorting_3(self): |
| 746 |
sorted_deps = serializers.sort_dependencies( |
| 747 |
[("fixtures_regress", [Store, Book, Person])] |
| 748 |
) |
| 749 |
self.assertEqual(sorted_deps, [Store, Person, Book]) |
| 750 |
|
| 751 |
def test_dependency_sorting_4(self): |
| 752 |
sorted_deps = serializers.sort_dependencies( |
| 753 |
[("fixtures_regress", [Store, Person, Book])] |
| 754 |
) |
| 755 |
self.assertEqual(sorted_deps, [Store, Person, Book]) |
| 756 |
|
| 757 |
def test_dependency_sorting_5(self): |
| 758 |
sorted_deps = serializers.sort_dependencies( |
| 759 |
[("fixtures_regress", [Person, Book, Store])] |
| 760 |
) |
| 761 |
self.assertEqual(sorted_deps, [Store, Person, Book]) |
| 762 |
|
| 763 |
def test_dependency_sorting_6(self): |
| 764 |
sorted_deps = serializers.sort_dependencies( |
| 765 |
[("fixtures_regress", [Person, Store, Book])] |
| 766 |
) |
| 767 |
self.assertEqual(sorted_deps, [Store, Person, Book]) |
| 768 |
|
| 769 |
def test_dependency_sorting_dangling(self): |
| 770 |
sorted_deps = serializers.sort_dependencies( |
| 771 |
[("fixtures_regress", [Person, Circle1, Store, Book])] |
| 772 |
) |
| 773 |
self.assertEqual(sorted_deps, [Circle1, Store, Person, Book]) |
| 774 |
|
| 775 |
def test_dependency_sorting_tight_circular(self): |
| 776 |
with self.assertRaisesMessage( |
| 777 |
RuntimeError, |
| 778 |
"Can't resolve dependencies for fixtures_regress.Circle1, " |
| 779 |
"fixtures_regress.Circle2 in serialized app list.", |
| 780 |
): |
| 781 |
serializers.sort_dependencies( |
| 782 |
[("fixtures_regress", [Person, Circle2, Circle1, Store, Book])] |
| 783 |
) |
| 784 |
|
| 785 |
def test_dependency_sorting_tight_circular_2(self): |
| 786 |
with self.assertRaisesMessage( |
| 787 |
RuntimeError, |
| 788 |
"Can't resolve dependencies for fixtures_regress.Circle1, " |
| 789 |
"fixtures_regress.Circle2 in serialized app list.", |
| 790 |
): |
| 791 |
serializers.sort_dependencies( |
| 792 |
[("fixtures_regress", [Circle1, Book, Circle2])] |
| 793 |
) |
| 794 |
|
| 795 |
def test_dependency_self_referential(self): |
| 796 |
with self.assertRaisesMessage( |
| 797 |
RuntimeError, |
| 798 |
"Can't resolve dependencies for fixtures_regress.Circle3 in " |
| 799 |
"serialized app list.", |
| 800 |
): |
| 801 |
serializers.sort_dependencies([("fixtures_regress", [Book, Circle3])]) |
| 802 |
|
| 803 |
def test_dependency_sorting_long(self): |
| 804 |
with self.assertRaisesMessage( |
| 805 |
RuntimeError, |
| 806 |
"Can't resolve dependencies for fixtures_regress.Circle1, " |
| 807 |
"fixtures_regress.Circle2, fixtures_regress.Circle3 in serialized " |
| 808 |
"app list.", |
| 809 |
): |
| 810 |
serializers.sort_dependencies( |
| 811 |
[("fixtures_regress", [Person, Circle2, Circle1, Circle3, Store, Book])] |
| 812 |
) |
| 813 |
|
| 814 |
def test_dependency_sorting_normal(self): |
| 815 |
sorted_deps = serializers.sort_dependencies( |
| 816 |
[("fixtures_regress", [Person, ExternalDependency, Book])] |
| 817 |
) |
| 818 |
self.assertEqual(sorted_deps, [Person, Book, ExternalDependency]) |
| 819 |
|
| 820 |
def test_normal_pk(self): |
| 821 |
""" |
| 822 |
Normal primary keys work on a model with natural key capabilities. |
| 823 |
""" |
| 824 |
management.call_command( |
| 825 |
"loaddata", |
| 826 |
"non_natural_1.json", |
| 827 |
verbosity=0, |
| 828 |
) |
| 829 |
management.call_command( |
| 830 |
"loaddata", |
| 831 |
"forward_ref_lookup.json", |
| 832 |
verbosity=0, |
| 833 |
) |
| 834 |
management.call_command( |
| 835 |
"loaddata", |
| 836 |
"non_natural_2.xml", |
| 837 |
verbosity=0, |
| 838 |
) |
| 839 |
books = Book.objects.all() |
| 840 |
self.assertQuerySetEqual( |
| 841 |
books, |
| 842 |
[ |
| 843 |
"<Book: Cryptonomicon by Neal Stephenson (available at Amazon, " |
| 844 |
"Borders)>", |
| 845 |
"<Book: Ender's Game by Orson Scott Card (available at Collins " |
| 846 |
"Bookstore)>", |
| 847 |
"<Book: Permutation City by Greg Egan (available at Angus and " |
| 848 |
"Robertson)>", |
| 849 |
], |
| 850 |
transform=repr, |
| 851 |
) |
| 852 |
|
| 853 |
|
| 854 |
class NaturalKeyFixtureOnOtherDatabaseTests(TestCase): |
| 855 |
databases = {"other"} |
| 856 |
|
| 857 |
def test_natural_key_dependencies(self): |
| 858 |
""" |
| 859 |
Natural keys with foreign keys in dependencies works in a multiple |
| 860 |
database setup. |
| 861 |
""" |
| 862 |
management.call_command( |
| 863 |
"loaddata", |
| 864 |
"nk_with_foreign_key.json", |
| 865 |
database="other", |
| 866 |
verbosity=0, |
| 867 |
) |
| 868 |
obj = NaturalKeyWithFKDependency.objects.using("other").get() |
| 869 |
self.assertEqual(obj.name, "The Lord of the Rings") |
| 870 |
self.assertEqual(obj.author.name, "J.R.R. Tolkien") |
| 871 |
|
| 872 |
|
| 873 |
class M2MNaturalKeyFixtureTests(TestCase): |
| 874 |
"""Tests for ticket #14426.""" |
| 875 |
|
| 876 |
def test_dependency_sorting_m2m_simple(self): |
| 877 |
""" |
| 878 |
M2M relations without explicit through models SHOULD count as dependencies |
| 879 |
|
| 880 |
Regression test for bugs that could be caused by flawed fixes to |
| 881 |
#14226, namely if M2M checks are removed from sort_dependencies |
| 882 |
altogether. |
| 883 |
""" |
| 884 |
sorted_deps = serializers.sort_dependencies( |
| 885 |
[("fixtures_regress", [M2MSimpleA, M2MSimpleB])] |
| 886 |
) |
| 887 |
self.assertEqual(sorted_deps, [M2MSimpleB, M2MSimpleA]) |
| 888 |
|
| 889 |
def test_dependency_sorting_m2m_simple_circular(self): |
| 890 |
""" |
| 891 |
Resolving circular M2M relations without explicit through models should |
| 892 |
fail loudly |
| 893 |
""" |
| 894 |
with self.assertRaisesMessage( |
| 895 |
RuntimeError, |
| 896 |
"Can't resolve dependencies for fixtures_regress.M2MSimpleCircularA, " |
| 897 |
"fixtures_regress.M2MSimpleCircularB in serialized app list.", |
| 898 |
): |
| 899 |
serializers.sort_dependencies( |
| 900 |
[("fixtures_regress", [M2MSimpleCircularA, M2MSimpleCircularB])] |
| 901 |
) |
| 902 |
|
| 903 |
def test_dependency_sorting_m2m_complex(self): |
| 904 |
""" |
| 905 |
M2M relations with explicit through models should NOT count as |
| 906 |
dependencies. The through model itself will have dependencies, though. |
| 907 |
""" |
| 908 |
sorted_deps = serializers.sort_dependencies( |
| 909 |
[("fixtures_regress", [M2MComplexA, M2MComplexB, M2MThroughAB])] |
| 910 |
) |
| 911 |
# Order between M2MComplexA and M2MComplexB doesn't matter. The through |
| 912 |
# model has dependencies to them though, so it should come last. |
| 913 |
self.assertEqual(sorted_deps[-1], M2MThroughAB) |
| 914 |
|
| 915 |
def test_dependency_sorting_m2m_complex_circular_1(self): |
| 916 |
""" |
| 917 |
Circular M2M relations with explicit through models should be serializable |
| 918 |
""" |
| 919 |
A, B, C, AtoB, BtoC, CtoA = ( |
| 920 |
M2MComplexCircular1A, |
| 921 |
M2MComplexCircular1B, |
| 922 |
M2MComplexCircular1C, |
| 923 |
M2MCircular1ThroughAB, |
| 924 |
M2MCircular1ThroughBC, |
| 925 |
M2MCircular1ThroughCA, |
| 926 |
) |
| 927 |
sorted_deps = serializers.sort_dependencies( |
| 928 |
[("fixtures_regress", [A, B, C, AtoB, BtoC, CtoA])] |
| 929 |
) |
| 930 |
# The dependency sorting should not result in an error, and the |
| 931 |
# through model should have dependencies to the other models and as |
| 932 |
# such come last in the list. |
| 933 |
self.assertEqual(sorted_deps[:3], [A, B, C]) |
| 934 |
self.assertEqual(sorted_deps[3:], [AtoB, BtoC, CtoA]) |
| 935 |
|
| 936 |
def test_dependency_sorting_m2m_complex_circular_2(self): |
| 937 |
""" |
| 938 |
Circular M2M relations with explicit through models should be serializable |
| 939 |
This test tests the circularity with explicit natural_key.dependencies |
| 940 |
""" |
| 941 |
sorted_deps = serializers.sort_dependencies( |
| 942 |
[ |
| 943 |
( |
| 944 |
"fixtures_regress", |
| 945 |
[M2MComplexCircular2A, M2MComplexCircular2B, M2MCircular2ThroughAB], |
| 946 |
) |
| 947 |
] |
| 948 |
) |
| 949 |
self.assertEqual(sorted_deps[:2], [M2MComplexCircular2A, M2MComplexCircular2B]) |
| 950 |
self.assertEqual(sorted_deps[2:], [M2MCircular2ThroughAB]) |
| 951 |
|
| 952 |
def test_dump_and_load_m2m_simple(self): |
| 953 |
""" |
| 954 |
Test serializing and deserializing back models with simple M2M relations |
| 955 |
""" |
| 956 |
a = M2MSimpleA.objects.create(data="a") |
| 957 |
b1 = M2MSimpleB.objects.create(data="b1") |
| 958 |
b2 = M2MSimpleB.objects.create(data="b2") |
| 959 |
a.b_set.add(b1) |
| 960 |
a.b_set.add(b2) |
| 961 |
|
| 962 |
out = StringIO() |
| 963 |
management.call_command( |
| 964 |
"dumpdata", |
| 965 |
"fixtures_regress.M2MSimpleA", |
| 966 |
"fixtures_regress.M2MSimpleB", |
| 967 |
use_natural_foreign_keys=True, |
| 968 |
stdout=out, |
| 969 |
) |
| 970 |
|
| 971 |
for model in [M2MSimpleA, M2MSimpleB]: |
| 972 |
model.objects.all().delete() |
| 973 |
|
| 974 |
objects = serializers.deserialize("json", out.getvalue()) |
| 975 |
for obj in objects: |
| 976 |
obj.save() |
| 977 |
|
| 978 |
new_a = M2MSimpleA.objects.get_by_natural_key("a") |
| 979 |
self.assertCountEqual(new_a.b_set.all(), [b1, b2]) |
| 980 |
|
| 981 |
|
| 982 |
class TestTicket11101(TransactionTestCase): |
| 983 |
available_apps = ["fixtures_regress"] |
| 984 |
|
| 985 |
@skipUnlessDBFeature("supports_transactions") |
| 986 |
def test_ticket_11101(self): |
| 987 |
"""Fixtures can be rolled back (ticket #11101).""" |
| 988 |
with transaction.atomic(): |
| 989 |
management.call_command( |
| 990 |
"loaddata", |
| 991 |
"thingy.json", |
| 992 |
verbosity=0, |
| 993 |
) |
| 994 |
self.assertEqual(Thingy.objects.count(), 1) |
| 995 |
transaction.set_rollback(True) |
| 996 |
self.assertEqual(Thingy.objects.count(), 0) |
| 997 |
|
| 998 |
|
| 999 |
class TestLoadFixtureFromOtherAppDirectory(TestCase): |
| 1000 |
""" |
| 1001 |
#23612 -- fixtures path should be normalized to allow referencing relative |
| 1002 |
paths on Windows. |
| 1003 |
""" |
| 1004 |
|
| 1005 |
current_dir = os.path.abspath(os.path.dirname(__file__)) |
| 1006 |
# relative_prefix is something like tests/fixtures_regress or |
| 1007 |
# fixtures_regress depending on how runtests.py is invoked. |
| 1008 |
# All path separators must be / in order to be a proper regression test on |
| 1009 |
# Windows, so replace as appropriate. |
| 1010 |
relative_prefix = os.path.relpath(current_dir, os.getcwd()).replace("\\", "/") |
| 1011 |
fixtures = [relative_prefix + "/fixtures/absolute.json"] |
| 1012 |
|
| 1013 |
def test_fixtures_loaded(self): |
| 1014 |
count = Absolute.objects.count() |
| 1015 |
self.assertGreater(count, 0, "Fixtures not loaded properly.") |