| 1 |
import gzip |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
import unittest |
| 6 |
import warnings |
| 7 |
from io import StringIO |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
from django.apps import apps |
| 11 |
from django.contrib.sites.models import Site |
| 12 |
from django.core import management |
| 13 |
from django.core.files.temp import NamedTemporaryFile |
| 14 |
from django.core.management import CommandError |
| 15 |
from django.core.management.commands.dumpdata import ProxyModelWarning |
| 16 |
from django.core.serializers.base import ProgressBar |
| 17 |
from django.db import IntegrityError, connection |
| 18 |
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature |
| 19 |
|
| 20 |
from .models import ( |
| 21 |
Article, |
| 22 |
Category, |
| 23 |
CircularA, |
| 24 |
CircularB, |
| 25 |
NaturalKeyThing, |
| 26 |
PrimaryKeyUUIDModel, |
| 27 |
ProxySpy, |
| 28 |
Spy, |
| 29 |
Tag, |
| 30 |
Visa, |
| 31 |
) |
| 32 |
|
| 33 |
try: |
| 34 |
import bz2 # NOQA |
| 35 |
|
| 36 |
HAS_BZ2 = True |
| 37 |
except ImportError: |
| 38 |
HAS_BZ2 = False |
| 39 |
|
| 40 |
try: |
| 41 |
import lzma # NOQA |
| 42 |
|
| 43 |
HAS_LZMA = True |
| 44 |
except ImportError: |
| 45 |
HAS_LZMA = False |
| 46 |
|
| 47 |
|
| 48 |
class TestCaseFixtureLoadingTests(TestCase): |
| 49 |
fixtures = ["fixture1.json", "fixture2.json"] |
| 50 |
|
| 51 |
def test_class_fixtures(self): |
| 52 |
"Test case has installed 3 fixture objects" |
| 53 |
self.assertSequenceEqual( |
| 54 |
Article.objects.values_list("headline", flat=True), |
| 55 |
[ |
| 56 |
"Django conquers world!", |
| 57 |
"Copyright is fine the way it is", |
| 58 |
"Poker has no place on ESPN", |
| 59 |
], |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
class SubclassTestCaseFixtureLoadingTests(TestCaseFixtureLoadingTests): |
| 64 |
""" |
| 65 |
Make sure that subclasses can remove fixtures from parent class (#21089). |
| 66 |
""" |
| 67 |
|
| 68 |
fixtures = [] |
| 69 |
|
| 70 |
def test_class_fixtures(self): |
| 71 |
"There were no fixture objects installed" |
| 72 |
self.assertEqual(Article.objects.count(), 0) |
| 73 |
|
| 74 |
|
| 75 |
class DumpDataAssertMixin: |
| 76 |
def _dumpdata_assert( |
| 77 |
self, |
| 78 |
args, |
| 79 |
output, |
| 80 |
format="json", |
| 81 |
filename=None, |
| 82 |
natural_foreign_keys=False, |
| 83 |
natural_primary_keys=False, |
| 84 |
use_base_manager=False, |
| 85 |
exclude_list=[], |
| 86 |
primary_keys="", |
| 87 |
): |
| 88 |
new_io = StringIO() |
| 89 |
filename = filename and os.path.join(tempfile.gettempdir(), filename) |
| 90 |
management.call_command( |
| 91 |
"dumpdata", |
| 92 |
*args, |
| 93 |
format=format, |
| 94 |
stdout=new_io, |
| 95 |
stderr=new_io, |
| 96 |
output=filename, |
| 97 |
use_natural_foreign_keys=natural_foreign_keys, |
| 98 |
use_natural_primary_keys=natural_primary_keys, |
| 99 |
use_base_manager=use_base_manager, |
| 100 |
exclude=exclude_list, |
| 101 |
primary_keys=primary_keys, |
| 102 |
) |
| 103 |
if filename: |
| 104 |
file_root, file_ext = os.path.splitext(filename) |
| 105 |
compression_formats = { |
| 106 |
".bz2": (open, file_root), |
| 107 |
".gz": (gzip.open, filename), |
| 108 |
".lzma": (open, file_root), |
| 109 |
".xz": (open, file_root), |
| 110 |
".zip": (open, file_root), |
| 111 |
} |
| 112 |
if HAS_BZ2: |
| 113 |
compression_formats[".bz2"] = (bz2.open, filename) |
| 114 |
if HAS_LZMA: |
| 115 |
compression_formats[".lzma"] = (lzma.open, filename) |
| 116 |
compression_formats[".xz"] = (lzma.open, filename) |
| 117 |
try: |
| 118 |
open_method, file_path = compression_formats[file_ext] |
| 119 |
except KeyError: |
| 120 |
open_method, file_path = open, filename |
| 121 |
with open_method(file_path, "rt") as f: |
| 122 |
command_output = f.read() |
| 123 |
os.remove(file_path) |
| 124 |
else: |
| 125 |
command_output = new_io.getvalue().strip() |
| 126 |
if format == "json": |
| 127 |
self.assertJSONEqual(command_output, output) |
| 128 |
elif format == "xml": |
| 129 |
self.assertXMLEqual(command_output, output) |
| 130 |
else: |
| 131 |
self.assertEqual(command_output, output) |
| 132 |
|
| 133 |
|
| 134 |
class FixtureLoadingTests(DumpDataAssertMixin, TestCase): |
| 135 |
def test_loading_and_dumping(self): |
| 136 |
apps.clear_cache() |
| 137 |
Site.objects.all().delete() |
| 138 |
# Load fixture 1. Single JSON file, with two objects. |
| 139 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 140 |
self.assertSequenceEqual( |
| 141 |
Article.objects.values_list("headline", flat=True), |
| 142 |
["Time to reform copyright", "Poker has no place on ESPN"], |
| 143 |
) |
| 144 |
|
| 145 |
# Dump the current contents of the database as a JSON fixture |
| 146 |
self._dumpdata_assert( |
| 147 |
["fixtures"], |
| 148 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 149 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 150 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 151 |
'{"headline": "Poker has no place on ESPN", ' |
| 152 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 153 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 154 |
'{"headline": "Time to reform copyright", ' |
| 155 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 156 |
) |
| 157 |
|
| 158 |
# Try just dumping the contents of fixtures.Category |
| 159 |
self._dumpdata_assert( |
| 160 |
["fixtures.Category"], |
| 161 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 162 |
'{"description": "Latest news stories", "title": "News Stories"}}]', |
| 163 |
) |
| 164 |
|
| 165 |
# ...and just fixtures.Article |
| 166 |
self._dumpdata_assert( |
| 167 |
["fixtures.Article"], |
| 168 |
'[{"pk": 2, "model": "fixtures.article", "fields": ' |
| 169 |
'{"headline": "Poker has no place on ESPN", ' |
| 170 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 171 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 172 |
'{"headline": "Time to reform copyright", ' |
| 173 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 174 |
) |
| 175 |
|
| 176 |
# ...and both |
| 177 |
self._dumpdata_assert( |
| 178 |
["fixtures.Category", "fixtures.Article"], |
| 179 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 180 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 181 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 182 |
'{"headline": "Poker has no place on ESPN", ' |
| 183 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 184 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 185 |
'{"headline": "Time to reform copyright", ' |
| 186 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 187 |
) |
| 188 |
|
| 189 |
# Specify a specific model twice |
| 190 |
self._dumpdata_assert( |
| 191 |
["fixtures.Article", "fixtures.Article"], |
| 192 |
( |
| 193 |
'[{"pk": 2, "model": "fixtures.article", "fields": ' |
| 194 |
'{"headline": "Poker has no place on ESPN", ' |
| 195 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 196 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 197 |
'{"headline": "Time to reform copyright", ' |
| 198 |
'"pub_date": "2006-06-16T13:00:00"}}]' |
| 199 |
), |
| 200 |
) |
| 201 |
|
| 202 |
# Specify a dump that specifies Article both explicitly and implicitly |
| 203 |
self._dumpdata_assert( |
| 204 |
["fixtures.Article", "fixtures"], |
| 205 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 206 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 207 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 208 |
'{"headline": "Poker has no place on ESPN", ' |
| 209 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 210 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 211 |
'{"headline": "Time to reform copyright", ' |
| 212 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 213 |
) |
| 214 |
|
| 215 |
# Specify a dump that specifies Article both explicitly and implicitly, |
| 216 |
# but lists the app first (#22025). |
| 217 |
self._dumpdata_assert( |
| 218 |
["fixtures", "fixtures.Article"], |
| 219 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 220 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 221 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 222 |
'{"headline": "Poker has no place on ESPN", ' |
| 223 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 224 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 225 |
'{"headline": "Time to reform copyright", ' |
| 226 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 227 |
) |
| 228 |
|
| 229 |
# Same again, but specify in the reverse order |
| 230 |
self._dumpdata_assert( |
| 231 |
["fixtures"], |
| 232 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 233 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 234 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 235 |
'{"headline": "Poker has no place on ESPN", ' |
| 236 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 237 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 238 |
'{"headline": "Time to reform copyright", ' |
| 239 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 240 |
) |
| 241 |
|
| 242 |
# Specify one model from one application, and an entire other application. |
| 243 |
self._dumpdata_assert( |
| 244 |
["fixtures.Category", "sites"], |
| 245 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 246 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 247 |
'{"pk": 1, "model": "sites.site", "fields": ' |
| 248 |
'{"domain": "example.com", "name": "example.com"}}]', |
| 249 |
) |
| 250 |
|
| 251 |
# Load fixture 2. JSON file imported by default. Overwrites some |
| 252 |
# existing objects. |
| 253 |
management.call_command("loaddata", "fixture2.json", verbosity=0) |
| 254 |
self.assertSequenceEqual( |
| 255 |
Article.objects.values_list("headline", flat=True), |
| 256 |
[ |
| 257 |
"Django conquers world!", |
| 258 |
"Copyright is fine the way it is", |
| 259 |
"Poker has no place on ESPN", |
| 260 |
], |
| 261 |
) |
| 262 |
|
| 263 |
# Load fixture 3, XML format. |
| 264 |
management.call_command("loaddata", "fixture3.xml", verbosity=0) |
| 265 |
self.assertSequenceEqual( |
| 266 |
Article.objects.values_list("headline", flat=True), |
| 267 |
[ |
| 268 |
"XML identified as leading cause of cancer", |
| 269 |
"Django conquers world!", |
| 270 |
"Copyright is fine the way it is", |
| 271 |
"Poker on TV is great!", |
| 272 |
], |
| 273 |
) |
| 274 |
|
| 275 |
# Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne. |
| 276 |
management.call_command("loaddata", "fixture6.json", verbosity=0) |
| 277 |
self.assertQuerySetEqual( |
| 278 |
Tag.objects.all(), |
| 279 |
[ |
| 280 |
'<Tag: <Article: Copyright is fine the way it is> tagged "copyright">', |
| 281 |
'<Tag: <Article: Copyright is fine the way it is> tagged "law">', |
| 282 |
], |
| 283 |
transform=repr, |
| 284 |
ordered=False, |
| 285 |
) |
| 286 |
|
| 287 |
# Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne. |
| 288 |
management.call_command("loaddata", "fixture7.xml", verbosity=0) |
| 289 |
self.assertQuerySetEqual( |
| 290 |
Tag.objects.all(), |
| 291 |
[ |
| 292 |
'<Tag: <Article: Copyright is fine the way it is> tagged "copyright">', |
| 293 |
'<Tag: <Article: Copyright is fine the way it is> tagged "legal">', |
| 294 |
'<Tag: <Article: Django conquers world!> tagged "django">', |
| 295 |
'<Tag: <Article: Django conquers world!> tagged "world domination">', |
| 296 |
], |
| 297 |
transform=repr, |
| 298 |
ordered=False, |
| 299 |
) |
| 300 |
|
| 301 |
# Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany. |
| 302 |
management.call_command("loaddata", "fixture8.json", verbosity=0) |
| 303 |
self.assertQuerySetEqual( |
| 304 |
Visa.objects.all(), |
| 305 |
[ |
| 306 |
"<Visa: Django Reinhardt Can add user, Can change user, Can delete " |
| 307 |
"user>", |
| 308 |
"<Visa: Stephane Grappelli Can add user>", |
| 309 |
"<Visa: Prince >", |
| 310 |
], |
| 311 |
transform=repr, |
| 312 |
ordered=False, |
| 313 |
) |
| 314 |
|
| 315 |
# Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany. |
| 316 |
management.call_command("loaddata", "fixture9.xml", verbosity=0) |
| 317 |
self.assertQuerySetEqual( |
| 318 |
Visa.objects.all(), |
| 319 |
[ |
| 320 |
"<Visa: Django Reinhardt Can add user, Can change user, Can delete " |
| 321 |
"user>", |
| 322 |
"<Visa: Stephane Grappelli Can add user, Can delete user>", |
| 323 |
'<Visa: Artist formerly known as "Prince" Can change user>', |
| 324 |
], |
| 325 |
transform=repr, |
| 326 |
ordered=False, |
| 327 |
) |
| 328 |
|
| 329 |
# object list is unaffected |
| 330 |
self.assertSequenceEqual( |
| 331 |
Article.objects.values_list("headline", flat=True), |
| 332 |
[ |
| 333 |
"XML identified as leading cause of cancer", |
| 334 |
"Django conquers world!", |
| 335 |
"Copyright is fine the way it is", |
| 336 |
"Poker on TV is great!", |
| 337 |
], |
| 338 |
) |
| 339 |
|
| 340 |
# By default, you get raw keys on dumpdata |
| 341 |
self._dumpdata_assert( |
| 342 |
["fixtures.book"], |
| 343 |
'[{"pk": 1, "model": "fixtures.book", "fields": ' |
| 344 |
'{"name": "Music for all ages", "authors": [3, 1]}}]', |
| 345 |
) |
| 346 |
|
| 347 |
# But you can get natural keys if you ask for them and they are available |
| 348 |
self._dumpdata_assert( |
| 349 |
["fixtures.book"], |
| 350 |
'[{"pk": 1, "model": "fixtures.book", "fields": ' |
| 351 |
'{"name": "Music for all ages", "authors": ' |
| 352 |
'[["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]', |
| 353 |
natural_foreign_keys=True, |
| 354 |
) |
| 355 |
|
| 356 |
# You can also omit the primary keys for models that we can get later |
| 357 |
# with natural keys. |
| 358 |
self._dumpdata_assert( |
| 359 |
["fixtures.person"], |
| 360 |
'[{"fields": {"name": "Django Reinhardt"}, "model": "fixtures.person"}, ' |
| 361 |
'{"fields": {"name": "Stephane Grappelli"}, "model": "fixtures.person"}, ' |
| 362 |
'{"fields": {"name": "Artist formerly known as \\"Prince\\""}, ' |
| 363 |
'"model": "fixtures.person"}]', |
| 364 |
natural_primary_keys=True, |
| 365 |
) |
| 366 |
|
| 367 |
# Dump the current contents of the database as a JSON fixture |
| 368 |
self._dumpdata_assert( |
| 369 |
["fixtures"], |
| 370 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 371 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 372 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 373 |
'{"headline": "Poker on TV is great!", ' |
| 374 |
'"pub_date": "2006-06-16T11:00:00"}}, ' |
| 375 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 376 |
'{"headline": "Copyright is fine the way it is", ' |
| 377 |
'"pub_date": "2006-06-16T14:00:00"}}, ' |
| 378 |
'{"pk": 4, "model": "fixtures.article", "fields": ' |
| 379 |
'{"headline": "Django conquers world!", ' |
| 380 |
'"pub_date": "2006-06-16T15:00:00"}}, ' |
| 381 |
'{"pk": 5, "model": "fixtures.article", "fields": ' |
| 382 |
'{"headline": "XML identified as leading cause of cancer", ' |
| 383 |
'"pub_date": "2006-06-16T16:00:00"}}, ' |
| 384 |
'{"pk": 1, "model": "fixtures.tag", "fields": ' |
| 385 |
'{"tagged_type": ["fixtures", "article"], "name": "copyright", ' |
| 386 |
'"tagged_id": 3}}, ' |
| 387 |
'{"pk": 2, "model": "fixtures.tag", "fields": ' |
| 388 |
'{"tagged_type": ["fixtures", "article"], "name": "legal", ' |
| 389 |
'"tagged_id": 3}}, ' |
| 390 |
'{"pk": 3, "model": "fixtures.tag", "fields": ' |
| 391 |
'{"tagged_type": ["fixtures", "article"], "name": "django", ' |
| 392 |
'"tagged_id": 4}}, ' |
| 393 |
'{"pk": 4, "model": "fixtures.tag", "fields": ' |
| 394 |
'{"tagged_type": ["fixtures", "article"], "name": "world domination", ' |
| 395 |
'"tagged_id": 4}}, ' |
| 396 |
'{"pk": 1, "model": "fixtures.person", ' |
| 397 |
'"fields": {"name": "Django Reinhardt"}}, ' |
| 398 |
'{"pk": 2, "model": "fixtures.person", ' |
| 399 |
'"fields": {"name": "Stephane Grappelli"}}, ' |
| 400 |
'{"pk": 3, "model": "fixtures.person", ' |
| 401 |
'"fields": {"name": "Artist formerly known as \\"Prince\\""}}, ' |
| 402 |
'{"pk": 1, "model": "fixtures.visa", ' |
| 403 |
'"fields": {"person": ["Django Reinhardt"], "permissions": ' |
| 404 |
'[["add_user", "auth", "user"], ["change_user", "auth", "user"], ' |
| 405 |
'["delete_user", "auth", "user"]]}}, ' |
| 406 |
'{"pk": 2, "model": "fixtures.visa", "fields": ' |
| 407 |
'{"person": ["Stephane Grappelli"], "permissions": ' |
| 408 |
'[["add_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, ' |
| 409 |
'{"pk": 3, "model": "fixtures.visa", "fields": ' |
| 410 |
'{"person": ["Artist formerly known as \\"Prince\\""], "permissions": ' |
| 411 |
'[["change_user", "auth", "user"]]}}, ' |
| 412 |
'{"pk": 1, "model": "fixtures.book", "fields": ' |
| 413 |
'{"name": "Music for all ages", "authors": ' |
| 414 |
'[["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]', |
| 415 |
natural_foreign_keys=True, |
| 416 |
) |
| 417 |
|
| 418 |
# Dump the current contents of the database as an XML fixture |
| 419 |
self._dumpdata_assert( |
| 420 |
["fixtures"], |
| 421 |
'<?xml version="1.0" encoding="utf-8"?><django-objects version="1.0">' |
| 422 |
'<object pk="1" model="fixtures.category">' |
| 423 |
'<field type="CharField" name="title">News Stories</field>' |
| 424 |
'<field type="TextField" name="description">Latest news stories</field>' |
| 425 |
"</object>" |
| 426 |
'<object pk="2" model="fixtures.article">' |
| 427 |
'<field type="CharField" name="headline">Poker on TV is great!</field>' |
| 428 |
'<field type="DateTimeField" name="pub_date">2006-06-16T11:00:00</field>' |
| 429 |
"</object>" |
| 430 |
'<object pk="3" model="fixtures.article">' |
| 431 |
'<field type="CharField" name="headline">Copyright is fine the way it is' |
| 432 |
"</field>" |
| 433 |
'<field type="DateTimeField" name="pub_date">2006-06-16T14:00:00</field>' |
| 434 |
"</object>" |
| 435 |
'<object pk="4" model="fixtures.article">' |
| 436 |
'<field type="CharField" name="headline">Django conquers world!</field>' |
| 437 |
'<field type="DateTimeField" name="pub_date">2006-06-16T15:00:00</field>' |
| 438 |
"</object>" |
| 439 |
'<object pk="5" model="fixtures.article">' |
| 440 |
'<field type="CharField" name="headline">' |
| 441 |
"XML identified as leading cause of cancer</field>" |
| 442 |
'<field type="DateTimeField" name="pub_date">2006-06-16T16:00:00</field>' |
| 443 |
"</object>" |
| 444 |
'<object pk="1" model="fixtures.tag">' |
| 445 |
'<field type="CharField" name="name">copyright</field>' |
| 446 |
'<field to="contenttypes.contenttype" name="tagged_type" ' |
| 447 |
'rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural>' |
| 448 |
"</field>" |
| 449 |
'<field type="PositiveIntegerField" name="tagged_id">3</field>' |
| 450 |
"</object>" |
| 451 |
'<object pk="2" model="fixtures.tag">' |
| 452 |
'<field type="CharField" name="name">legal</field>' |
| 453 |
'<field to="contenttypes.contenttype" name="tagged_type" ' |
| 454 |
'rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural>' |
| 455 |
"</field>" |
| 456 |
'<field type="PositiveIntegerField" name="tagged_id">3</field></object>' |
| 457 |
'<object pk="3" model="fixtures.tag">' |
| 458 |
'<field type="CharField" name="name">django</field>' |
| 459 |
'<field to="contenttypes.contenttype" name="tagged_type" ' |
| 460 |
'rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural>' |
| 461 |
"</field>" |
| 462 |
'<field type="PositiveIntegerField" name="tagged_id">4</field>' |
| 463 |
"</object>" |
| 464 |
'<object pk="4" model="fixtures.tag">' |
| 465 |
'<field type="CharField" name="name">world domination</field>' |
| 466 |
'<field to="contenttypes.contenttype" name="tagged_type" ' |
| 467 |
'rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural>' |
| 468 |
"</field>" |
| 469 |
'<field type="PositiveIntegerField" name="tagged_id">4</field>' |
| 470 |
"</object>" |
| 471 |
'<object pk="1" model="fixtures.person">' |
| 472 |
'<field type="CharField" name="name">Django Reinhardt</field>' |
| 473 |
"</object>" |
| 474 |
'<object pk="2" model="fixtures.person">' |
| 475 |
'<field type="CharField" name="name">Stephane Grappelli</field>' |
| 476 |
"</object>" |
| 477 |
'<object pk="3" model="fixtures.person">' |
| 478 |
'<field type="CharField" name="name">Artist formerly known as "Prince"' |
| 479 |
"</field>" |
| 480 |
"</object>" |
| 481 |
'<object pk="1" model="fixtures.visa">' |
| 482 |
'<field to="fixtures.person" name="person" rel="ManyToOneRel">' |
| 483 |
"<natural>Django Reinhardt</natural></field>" |
| 484 |
'<field to="auth.permission" name="permissions" rel="ManyToManyRel">' |
| 485 |
"<object><natural>add_user</natural><natural>auth</natural>" |
| 486 |
"<natural>user</natural></object><object><natural>change_user</natural>" |
| 487 |
"<natural>auth</natural><natural>user</natural></object>" |
| 488 |
"<object><natural>delete_user</natural><natural>auth</natural>" |
| 489 |
"<natural>user</natural></object></field>" |
| 490 |
"</object>" |
| 491 |
'<object pk="2" model="fixtures.visa">' |
| 492 |
'<field to="fixtures.person" name="person" rel="ManyToOneRel">' |
| 493 |
"<natural>Stephane Grappelli</natural></field>" |
| 494 |
'<field to="auth.permission" name="permissions" rel="ManyToManyRel">' |
| 495 |
"<object><natural>add_user</natural><natural>auth</natural>" |
| 496 |
"<natural>user</natural></object>" |
| 497 |
"<object><natural>delete_user</natural><natural>auth</natural>" |
| 498 |
"<natural>user</natural></object></field>" |
| 499 |
"</object>" |
| 500 |
'<object pk="3" model="fixtures.visa">' |
| 501 |
'<field to="fixtures.person" name="person" rel="ManyToOneRel">' |
| 502 |
'<natural>Artist formerly known as "Prince"</natural></field>' |
| 503 |
'<field to="auth.permission" name="permissions" rel="ManyToManyRel">' |
| 504 |
"<object><natural>change_user</natural><natural>auth</natural>" |
| 505 |
"<natural>user</natural></object></field>" |
| 506 |
"</object>" |
| 507 |
'<object pk="1" model="fixtures.book">' |
| 508 |
'<field type="CharField" name="name">Music for all ages</field>' |
| 509 |
'<field to="fixtures.person" name="authors" rel="ManyToManyRel">' |
| 510 |
'<object><natural>Artist formerly known as "Prince"</natural></object>' |
| 511 |
"<object><natural>Django Reinhardt</natural></object></field>" |
| 512 |
"</object></django-objects>", |
| 513 |
format="xml", |
| 514 |
natural_foreign_keys=True, |
| 515 |
) |
| 516 |
|
| 517 |
def test_dumpdata_with_excludes(self): |
| 518 |
# Load fixture1 which has a site, two articles, and a category |
| 519 |
Site.objects.all().delete() |
| 520 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 521 |
|
| 522 |
# Excluding fixtures app should only leave sites |
| 523 |
self._dumpdata_assert( |
| 524 |
["sites", "fixtures"], |
| 525 |
'[{"pk": 1, "model": "sites.site", "fields": ' |
| 526 |
'{"domain": "example.com", "name": "example.com"}}]', |
| 527 |
exclude_list=["fixtures"], |
| 528 |
) |
| 529 |
|
| 530 |
# Excluding fixtures.Article/Book should leave fixtures.Category |
| 531 |
self._dumpdata_assert( |
| 532 |
["sites", "fixtures"], |
| 533 |
'[{"pk": 1, "model": "sites.site", ' |
| 534 |
'"fields": {"domain": "example.com", "name": "example.com"}}, ' |
| 535 |
'{"pk": 1, "model": "fixtures.category", "fields": ' |
| 536 |
'{"description": "Latest news stories", "title": "News Stories"}}]', |
| 537 |
exclude_list=["fixtures.Article", "fixtures.Book"], |
| 538 |
) |
| 539 |
|
| 540 |
# Excluding fixtures and fixtures.Article/Book should be a no-op |
| 541 |
self._dumpdata_assert( |
| 542 |
["sites", "fixtures"], |
| 543 |
'[{"pk": 1, "model": "sites.site", ' |
| 544 |
'"fields": {"domain": "example.com", "name": "example.com"}}, ' |
| 545 |
'{"pk": 1, "model": "fixtures.category", ' |
| 546 |
'"fields": {"description": "Latest news stories", ' |
| 547 |
'"title": "News Stories"}}]', |
| 548 |
exclude_list=["fixtures.Article", "fixtures.Book"], |
| 549 |
) |
| 550 |
|
| 551 |
# Excluding sites and fixtures.Article/Book should only leave fixtures.Category |
| 552 |
self._dumpdata_assert( |
| 553 |
["sites", "fixtures"], |
| 554 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 555 |
'{"description": "Latest news stories", "title": "News Stories"}}]', |
| 556 |
exclude_list=["fixtures.Article", "fixtures.Book", "sites"], |
| 557 |
) |
| 558 |
|
| 559 |
# Excluding a bogus app should throw an error |
| 560 |
with self.assertRaisesMessage( |
| 561 |
management.CommandError, "No installed app with label 'foo_app'." |
| 562 |
): |
| 563 |
self._dumpdata_assert(["fixtures", "sites"], "", exclude_list=["foo_app"]) |
| 564 |
|
| 565 |
# Excluding a bogus model should throw an error |
| 566 |
with self.assertRaisesMessage( |
| 567 |
management.CommandError, "Unknown model: fixtures.FooModel" |
| 568 |
): |
| 569 |
self._dumpdata_assert( |
| 570 |
["fixtures", "sites"], "", exclude_list=["fixtures.FooModel"] |
| 571 |
) |
| 572 |
|
| 573 |
@unittest.skipIf( |
| 574 |
sys.platform == "win32", "Windows doesn't support '?' in filenames." |
| 575 |
) |
| 576 |
def test_load_fixture_with_special_characters(self): |
| 577 |
management.call_command("loaddata", "fixture_with[special]chars", verbosity=0) |
| 578 |
self.assertEqual( |
| 579 |
Article.objects.get().headline, |
| 580 |
"How To Deal With Special Characters", |
| 581 |
) |
| 582 |
|
| 583 |
def test_dumpdata_with_filtering_manager(self): |
| 584 |
spy1 = Spy.objects.create(name="Paul") |
| 585 |
spy2 = Spy.objects.create(name="Alex", cover_blown=True) |
| 586 |
self.assertSequenceEqual(Spy.objects.all(), [spy1]) |
| 587 |
# Use the default manager |
| 588 |
self._dumpdata_assert( |
| 589 |
["fixtures.Spy"], |
| 590 |
'[{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": false}}]' |
| 591 |
% spy1.pk, |
| 592 |
) |
| 593 |
# Dump using Django's base manager. Should return all objects, |
| 594 |
# even those normally filtered by the manager |
| 595 |
self._dumpdata_assert( |
| 596 |
["fixtures.Spy"], |
| 597 |
'[{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": true}}, ' |
| 598 |
'{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": false}}]' |
| 599 |
% (spy2.pk, spy1.pk), |
| 600 |
use_base_manager=True, |
| 601 |
) |
| 602 |
|
| 603 |
def test_dumpdata_with_pks(self): |
| 604 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 605 |
management.call_command("loaddata", "fixture2.json", verbosity=0) |
| 606 |
self._dumpdata_assert( |
| 607 |
["fixtures.Article"], |
| 608 |
'[{"pk": 2, "model": "fixtures.article", ' |
| 609 |
'"fields": {"headline": "Poker has no place on ESPN", ' |
| 610 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 611 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 612 |
'{"headline": "Copyright is fine the way it is", ' |
| 613 |
'"pub_date": "2006-06-16T14:00:00"}}]', |
| 614 |
primary_keys="2,3", |
| 615 |
) |
| 616 |
|
| 617 |
self._dumpdata_assert( |
| 618 |
["fixtures.Article"], |
| 619 |
'[{"pk": 2, "model": "fixtures.article", ' |
| 620 |
'"fields": {"headline": "Poker has no place on ESPN", ' |
| 621 |
'"pub_date": "2006-06-16T12:00:00"}}]', |
| 622 |
primary_keys="2", |
| 623 |
) |
| 624 |
|
| 625 |
with self.assertRaisesMessage( |
| 626 |
management.CommandError, "You can only use --pks option with one model" |
| 627 |
): |
| 628 |
self._dumpdata_assert( |
| 629 |
["fixtures"], |
| 630 |
'[{"pk": 2, "model": "fixtures.article", "fields": ' |
| 631 |
'{"headline": "Poker has no place on ESPN", ' |
| 632 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 633 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 634 |
'{"headline": "Copyright is fine the way it is", ' |
| 635 |
'"pub_date": "2006-06-16T14:00:00"}}]', |
| 636 |
primary_keys="2,3", |
| 637 |
) |
| 638 |
|
| 639 |
with self.assertRaisesMessage( |
| 640 |
management.CommandError, "You can only use --pks option with one model" |
| 641 |
): |
| 642 |
self._dumpdata_assert( |
| 643 |
"", |
| 644 |
'[{"pk": 2, "model": "fixtures.article", "fields": ' |
| 645 |
'{"headline": "Poker has no place on ESPN", ' |
| 646 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 647 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 648 |
'{"headline": "Copyright is fine the way it is", ' |
| 649 |
'"pub_date": "2006-06-16T14:00:00"}}]', |
| 650 |
primary_keys="2,3", |
| 651 |
) |
| 652 |
|
| 653 |
with self.assertRaisesMessage( |
| 654 |
management.CommandError, "You can only use --pks option with one model" |
| 655 |
): |
| 656 |
self._dumpdata_assert( |
| 657 |
["fixtures.Article", "fixtures.category"], |
| 658 |
'[{"pk": 2, "model": "fixtures.article", "fields": ' |
| 659 |
'{"headline": "Poker has no place on ESPN", ' |
| 660 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 661 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 662 |
'{"headline": "Copyright is fine the way it is", ' |
| 663 |
'"pub_date": "2006-06-16T14:00:00"}}]', |
| 664 |
primary_keys="2,3", |
| 665 |
) |
| 666 |
|
| 667 |
def test_dumpdata_with_uuid_pks(self): |
| 668 |
m1 = PrimaryKeyUUIDModel.objects.create() |
| 669 |
m2 = PrimaryKeyUUIDModel.objects.create() |
| 670 |
output = StringIO() |
| 671 |
management.call_command( |
| 672 |
"dumpdata", |
| 673 |
"fixtures.PrimaryKeyUUIDModel", |
| 674 |
"--pks", |
| 675 |
", ".join([str(m1.id), str(m2.id)]), |
| 676 |
stdout=output, |
| 677 |
) |
| 678 |
result = output.getvalue() |
| 679 |
self.assertIn('"pk": "%s"' % m1.id, result) |
| 680 |
self.assertIn('"pk": "%s"' % m2.id, result) |
| 681 |
|
| 682 |
def test_dumpdata_with_file_output(self): |
| 683 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 684 |
self._dumpdata_assert( |
| 685 |
["fixtures"], |
| 686 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 687 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 688 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 689 |
'{"headline": "Poker has no place on ESPN", ' |
| 690 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 691 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 692 |
'{"headline": "Time to reform copyright", ' |
| 693 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 694 |
filename="dumpdata.json", |
| 695 |
) |
| 696 |
|
| 697 |
def test_dumpdata_with_file_gzip_output(self): |
| 698 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 699 |
self._dumpdata_assert( |
| 700 |
["fixtures"], |
| 701 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 702 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 703 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 704 |
'{"headline": "Poker has no place on ESPN", ' |
| 705 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 706 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 707 |
'{"headline": "Time to reform copyright", ' |
| 708 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 709 |
filename="dumpdata.json.gz", |
| 710 |
) |
| 711 |
|
| 712 |
@unittest.skipUnless(HAS_BZ2, "No bz2 library detected.") |
| 713 |
def test_dumpdata_with_file_bz2_output(self): |
| 714 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 715 |
self._dumpdata_assert( |
| 716 |
["fixtures"], |
| 717 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 718 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 719 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 720 |
'{"headline": "Poker has no place on ESPN", ' |
| 721 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 722 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 723 |
'{"headline": "Time to reform copyright", ' |
| 724 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 725 |
filename="dumpdata.json.bz2", |
| 726 |
) |
| 727 |
|
| 728 |
@unittest.skipUnless(HAS_LZMA, "No lzma library detected.") |
| 729 |
def test_dumpdata_with_file_lzma_output(self): |
| 730 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 731 |
self._dumpdata_assert( |
| 732 |
["fixtures"], |
| 733 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 734 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 735 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 736 |
'{"headline": "Poker has no place on ESPN", ' |
| 737 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 738 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 739 |
'{"headline": "Time to reform copyright", ' |
| 740 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 741 |
filename="dumpdata.json.lzma", |
| 742 |
) |
| 743 |
|
| 744 |
@unittest.skipUnless(HAS_LZMA, "No lzma library detected.") |
| 745 |
def test_dumpdata_with_file_xz_output(self): |
| 746 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 747 |
self._dumpdata_assert( |
| 748 |
["fixtures"], |
| 749 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 750 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 751 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 752 |
'{"headline": "Poker has no place on ESPN", ' |
| 753 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 754 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 755 |
'{"headline": "Time to reform copyright", ' |
| 756 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 757 |
filename="dumpdata.json.xz", |
| 758 |
) |
| 759 |
|
| 760 |
def test_dumpdata_with_file_zip_output(self): |
| 761 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 762 |
msg = "Unsupported file extension (.zip). Fixtures saved in 'dumpdata.json'." |
| 763 |
with self.assertWarnsMessage(RuntimeWarning, msg): |
| 764 |
self._dumpdata_assert( |
| 765 |
["fixtures"], |
| 766 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 767 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 768 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 769 |
'{"headline": "Poker has no place on ESPN", ' |
| 770 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 771 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 772 |
'{"headline": "Time to reform copyright", ' |
| 773 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 774 |
filename="dumpdata.json.zip", |
| 775 |
) |
| 776 |
|
| 777 |
def test_dumpdata_progressbar(self): |
| 778 |
""" |
| 779 |
Dumpdata shows a progress bar on the command line when --output is set, |
| 780 |
stdout is a tty, and verbosity > 0. |
| 781 |
""" |
| 782 |
management.call_command("loaddata", "fixture1.json", verbosity=0) |
| 783 |
new_io = StringIO() |
| 784 |
new_io.isatty = lambda: True |
| 785 |
with NamedTemporaryFile() as file: |
| 786 |
options = { |
| 787 |
"format": "json", |
| 788 |
"stdout": new_io, |
| 789 |
"stderr": new_io, |
| 790 |
"output": file.name, |
| 791 |
} |
| 792 |
management.call_command("dumpdata", "fixtures", **options) |
| 793 |
self.assertTrue( |
| 794 |
new_io.getvalue().endswith( |
| 795 |
"[" + "." * ProgressBar.progress_width + "]\n" |
| 796 |
) |
| 797 |
) |
| 798 |
|
| 799 |
# Test no progress bar when verbosity = 0 |
| 800 |
options["verbosity"] = 0 |
| 801 |
new_io = StringIO() |
| 802 |
new_io.isatty = lambda: True |
| 803 |
options.update({"stdout": new_io, "stderr": new_io}) |
| 804 |
management.call_command("dumpdata", "fixtures", **options) |
| 805 |
self.assertEqual(new_io.getvalue(), "") |
| 806 |
|
| 807 |
def test_dumpdata_proxy_without_concrete(self): |
| 808 |
""" |
| 809 |
A warning is displayed if a proxy model is dumped without its concrete |
| 810 |
parent. |
| 811 |
""" |
| 812 |
ProxySpy.objects.create(name="Paul") |
| 813 |
msg = "fixtures.ProxySpy is a proxy model and won't be serialized." |
| 814 |
with self.assertWarnsMessage(ProxyModelWarning, msg): |
| 815 |
self._dumpdata_assert(["fixtures.ProxySpy"], "[]") |
| 816 |
|
| 817 |
def test_dumpdata_proxy_with_concrete(self): |
| 818 |
""" |
| 819 |
A warning isn't displayed if a proxy model is dumped with its concrete |
| 820 |
parent. |
| 821 |
""" |
| 822 |
spy = ProxySpy.objects.create(name="Paul") |
| 823 |
|
| 824 |
with warnings.catch_warnings(record=True) as warning_list: |
| 825 |
warnings.simplefilter("always") |
| 826 |
self._dumpdata_assert( |
| 827 |
["fixtures.ProxySpy", "fixtures.Spy"], |
| 828 |
'[{"pk": %d, "model": "fixtures.spy", ' |
| 829 |
'"fields": {"cover_blown": false}}]' % spy.pk, |
| 830 |
) |
| 831 |
self.assertEqual(len(warning_list), 0) |
| 832 |
|
| 833 |
def test_dumpdata_objects_with_prefetch_related(self): |
| 834 |
management.call_command( |
| 835 |
"loaddata", "fixture6.json", "fixture8.json", verbosity=0 |
| 836 |
) |
| 837 |
with self.assertNumQueries(5): |
| 838 |
self._dumpdata_assert( |
| 839 |
["fixtures.visa"], |
| 840 |
'[{"fields": {"permissions": [["add_user", "auth", "user"]],' |
| 841 |
'"person": ["Stephane Grappelli"]},' |
| 842 |
'"model": "fixtures.visa", "pk": 2},' |
| 843 |
'{"fields": {"permissions": [], "person": ["Prince"]},' |
| 844 |
'"model": "fixtures.visa", "pk": 3}]', |
| 845 |
natural_foreign_keys=True, |
| 846 |
primary_keys="2,3", |
| 847 |
) |
| 848 |
|
| 849 |
def test_compress_format_loading(self): |
| 850 |
# Load fixture 4 (compressed), using format specification |
| 851 |
management.call_command("loaddata", "fixture4.json", verbosity=0) |
| 852 |
self.assertEqual(Article.objects.get().headline, "Django pets kitten") |
| 853 |
|
| 854 |
def test_compressed_specified_loading(self): |
| 855 |
# Load fixture 5 (compressed), using format *and* compression specification |
| 856 |
management.call_command("loaddata", "fixture5.json.zip", verbosity=0) |
| 857 |
self.assertEqual( |
| 858 |
Article.objects.get().headline, |
| 859 |
"WoW subscribers now outnumber readers", |
| 860 |
) |
| 861 |
|
| 862 |
def test_compressed_loading(self): |
| 863 |
# Load fixture 5 (compressed), only compression specification |
| 864 |
management.call_command("loaddata", "fixture5.zip", verbosity=0) |
| 865 |
self.assertEqual( |
| 866 |
Article.objects.get().headline, |
| 867 |
"WoW subscribers now outnumber readers", |
| 868 |
) |
| 869 |
|
| 870 |
def test_compressed_loading_gzip(self): |
| 871 |
management.call_command("loaddata", "fixture5.json.gz", verbosity=0) |
| 872 |
self.assertEqual( |
| 873 |
Article.objects.get().headline, |
| 874 |
"WoW subscribers now outnumber readers", |
| 875 |
) |
| 876 |
|
| 877 |
@unittest.skipUnless(HAS_BZ2, "No bz2 library detected.") |
| 878 |
def test_compressed_loading_bz2(self): |
| 879 |
management.call_command("loaddata", "fixture5.json.bz2", verbosity=0) |
| 880 |
self.assertEqual( |
| 881 |
Article.objects.get().headline, |
| 882 |
"WoW subscribers now outnumber readers", |
| 883 |
) |
| 884 |
|
| 885 |
@unittest.skipUnless(HAS_LZMA, "No lzma library detected.") |
| 886 |
def test_compressed_loading_lzma(self): |
| 887 |
management.call_command("loaddata", "fixture5.json.lzma", verbosity=0) |
| 888 |
self.assertEqual( |
| 889 |
Article.objects.get().headline, |
| 890 |
"WoW subscribers now outnumber readers", |
| 891 |
) |
| 892 |
|
| 893 |
@unittest.skipUnless(HAS_LZMA, "No lzma library detected.") |
| 894 |
def test_compressed_loading_xz(self): |
| 895 |
management.call_command("loaddata", "fixture5.json.xz", verbosity=0) |
| 896 |
self.assertEqual( |
| 897 |
Article.objects.get().headline, |
| 898 |
"WoW subscribers now outnumber readers", |
| 899 |
) |
| 900 |
|
| 901 |
def test_ambiguous_compressed_fixture(self): |
| 902 |
# The name "fixture5" is ambiguous, so loading raises an error. |
| 903 |
msg = "Multiple fixtures named 'fixture5'" |
| 904 |
with self.assertRaisesMessage(management.CommandError, msg): |
| 905 |
management.call_command("loaddata", "fixture5", verbosity=0) |
| 906 |
|
| 907 |
def test_db_loading(self): |
| 908 |
# Load db fixtures 1 and 2. These will load using the 'default' |
| 909 |
# database identifier implicitly. |
| 910 |
management.call_command("loaddata", "db_fixture_1", verbosity=0) |
| 911 |
management.call_command("loaddata", "db_fixture_2", verbosity=0) |
| 912 |
self.assertSequenceEqual( |
| 913 |
Article.objects.values_list("headline", flat=True), |
| 914 |
[ |
| 915 |
"Who needs more than one database?", |
| 916 |
"Who needs to use compressed data?", |
| 917 |
], |
| 918 |
) |
| 919 |
|
| 920 |
def test_loaddata_error_message(self): |
| 921 |
""" |
| 922 |
Loading a fixture which contains an invalid object outputs an error |
| 923 |
message which contains the pk of the object that triggered the error. |
| 924 |
""" |
| 925 |
# MySQL needs a little prodding to reject invalid data. |
| 926 |
# This won't affect other tests because the database connection |
| 927 |
# is closed at the end of each test. |
| 928 |
if connection.vendor == "mysql": |
| 929 |
with connection.cursor() as cursor: |
| 930 |
cursor.execute("SET sql_mode = 'TRADITIONAL'") |
| 931 |
msg = "Could not load fixtures.Article(pk=1):" |
| 932 |
with self.assertRaisesMessage(IntegrityError, msg): |
| 933 |
management.call_command("loaddata", "invalid.json", verbosity=0) |
| 934 |
|
| 935 |
@skipUnlessDBFeature("prohibits_null_characters_in_text_exception") |
| 936 |
def test_loaddata_null_characters_on_postgresql(self): |
| 937 |
error, msg = connection.features.prohibits_null_characters_in_text_exception |
| 938 |
msg = f"Could not load fixtures.Article(pk=2): {msg}" |
| 939 |
with self.assertRaisesMessage(error, msg): |
| 940 |
management.call_command("loaddata", "null_character_in_field_value.json") |
| 941 |
|
| 942 |
def test_loaddata_app_option(self): |
| 943 |
with self.assertRaisesMessage( |
| 944 |
CommandError, "No fixture named 'db_fixture_1' found." |
| 945 |
): |
| 946 |
management.call_command( |
| 947 |
"loaddata", "db_fixture_1", verbosity=0, app_label="someotherapp" |
| 948 |
) |
| 949 |
self.assertQuerySetEqual(Article.objects.all(), []) |
| 950 |
management.call_command( |
| 951 |
"loaddata", "db_fixture_1", verbosity=0, app_label="fixtures" |
| 952 |
) |
| 953 |
self.assertEqual( |
| 954 |
Article.objects.get().headline, |
| 955 |
"Who needs more than one database?", |
| 956 |
) |
| 957 |
|
| 958 |
def test_loaddata_verbosity_three(self): |
| 959 |
output = StringIO() |
| 960 |
management.call_command( |
| 961 |
"loaddata", "fixture1.json", verbosity=3, stdout=output, stderr=output |
| 962 |
) |
| 963 |
command_output = output.getvalue() |
| 964 |
self.assertIn( |
| 965 |
"\rProcessed 1 object(s).\rProcessed 2 object(s)." |
| 966 |
"\rProcessed 3 object(s).\rProcessed 4 object(s).\n", |
| 967 |
command_output, |
| 968 |
) |
| 969 |
|
| 970 |
def test_loading_using(self): |
| 971 |
# Load fixtures 1 and 2. These will load using the 'default' database |
| 972 |
# identifier explicitly. |
| 973 |
management.call_command( |
| 974 |
"loaddata", "db_fixture_1", verbosity=0, database="default" |
| 975 |
) |
| 976 |
management.call_command( |
| 977 |
"loaddata", "db_fixture_2", verbosity=0, database="default" |
| 978 |
) |
| 979 |
self.assertSequenceEqual( |
| 980 |
Article.objects.values_list("headline", flat=True), |
| 981 |
[ |
| 982 |
"Who needs more than one database?", |
| 983 |
"Who needs to use compressed data?", |
| 984 |
], |
| 985 |
) |
| 986 |
|
| 987 |
def test_unmatched_identifier_loading(self): |
| 988 |
# Db fixture 3 won't load because the database identifier doesn't |
| 989 |
# match. |
| 990 |
with self.assertRaisesMessage( |
| 991 |
CommandError, "No fixture named 'db_fixture_3' found." |
| 992 |
): |
| 993 |
management.call_command("loaddata", "db_fixture_3", verbosity=0) |
| 994 |
with self.assertRaisesMessage( |
| 995 |
CommandError, "No fixture named 'db_fixture_3' found." |
| 996 |
): |
| 997 |
management.call_command( |
| 998 |
"loaddata", "db_fixture_3", verbosity=0, database="default" |
| 999 |
) |
| 1000 |
self.assertQuerySetEqual(Article.objects.all(), []) |
| 1001 |
|
| 1002 |
def test_output_formats(self): |
| 1003 |
# Load back in fixture 1, we need the articles from it |
| 1004 |
management.call_command("loaddata", "fixture1", verbosity=0) |
| 1005 |
|
| 1006 |
# Try to load fixture 6 using format discovery |
| 1007 |
management.call_command("loaddata", "fixture6", verbosity=0) |
| 1008 |
self.assertQuerySetEqual( |
| 1009 |
Tag.objects.all(), |
| 1010 |
[ |
| 1011 |
'<Tag: <Article: Time to reform copyright> tagged "copyright">', |
| 1012 |
'<Tag: <Article: Time to reform copyright> tagged "law">', |
| 1013 |
], |
| 1014 |
transform=repr, |
| 1015 |
ordered=False, |
| 1016 |
) |
| 1017 |
|
| 1018 |
# Dump the current contents of the database as a JSON fixture |
| 1019 |
self._dumpdata_assert( |
| 1020 |
["fixtures"], |
| 1021 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 1022 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 1023 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 1024 |
'{"headline": "Poker has no place on ESPN", ' |
| 1025 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 1026 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 1027 |
'{"headline": "Time to reform copyright", ' |
| 1028 |
'"pub_date": "2006-06-16T13:00:00"}}, ' |
| 1029 |
'{"pk": 1, "model": "fixtures.tag", "fields": ' |
| 1030 |
'{"tagged_type": ["fixtures", "article"], "name": "copyright", ' |
| 1031 |
'"tagged_id": 3}}, ' |
| 1032 |
'{"pk": 2, "model": "fixtures.tag", "fields": ' |
| 1033 |
'{"tagged_type": ["fixtures", "article"], "name": "law", "tagged_id": 3}}, ' |
| 1034 |
'{"pk": 1, "model": "fixtures.person", "fields": ' |
| 1035 |
'{"name": "Django Reinhardt"}}, ' |
| 1036 |
'{"pk": 2, "model": "fixtures.person", "fields": ' |
| 1037 |
'{"name": "Stephane Grappelli"}}, ' |
| 1038 |
'{"pk": 3, "model": "fixtures.person", "fields": {"name": "Prince"}}]', |
| 1039 |
natural_foreign_keys=True, |
| 1040 |
) |
| 1041 |
|
| 1042 |
# Dump the current contents of the database as an XML fixture |
| 1043 |
self._dumpdata_assert( |
| 1044 |
["fixtures"], |
| 1045 |
'<?xml version="1.0" encoding="utf-8"?><django-objects version="1.0">' |
| 1046 |
'<object pk="1" model="fixtures.category">' |
| 1047 |
'<field type="CharField" name="title">News Stories</field>' |
| 1048 |
'<field type="TextField" name="description">Latest news stories</field>' |
| 1049 |
"</object>" |
| 1050 |
'<object pk="2" model="fixtures.article">' |
| 1051 |
'<field type="CharField" name="headline">Poker has no place on ESPN</field>' |
| 1052 |
'<field type="DateTimeField" name="pub_date">2006-06-16T12:00:00</field>' |
| 1053 |
"</object>" |
| 1054 |
'<object pk="3" model="fixtures.article">' |
| 1055 |
'<field type="CharField" name="headline">Time to reform copyright</field>' |
| 1056 |
'<field type="DateTimeField" name="pub_date">2006-06-16T13:00:00</field>' |
| 1057 |
"</object>" |
| 1058 |
'<object pk="1" model="fixtures.tag">' |
| 1059 |
'<field type="CharField" name="name">copyright</field>' |
| 1060 |
'<field to="contenttypes.contenttype" name="tagged_type" ' |
| 1061 |
'rel="ManyToOneRel"><natural>fixtures</natural>' |
| 1062 |
"<natural>article</natural></field>" |
| 1063 |
'<field type="PositiveIntegerField" name="tagged_id">3</field>' |
| 1064 |
"</object>" |
| 1065 |
'<object pk="2" model="fixtures.tag">' |
| 1066 |
'<field type="CharField" name="name">law</field>' |
| 1067 |
'<field to="contenttypes.contenttype" name="tagged_type" ' |
| 1068 |
'rel="ManyToOneRel"><natural>fixtures</natural>' |
| 1069 |
"<natural>article</natural></field>" |
| 1070 |
'<field type="PositiveIntegerField" name="tagged_id">3</field>' |
| 1071 |
"</object>" |
| 1072 |
'<object pk="1" model="fixtures.person">' |
| 1073 |
'<field type="CharField" name="name">Django Reinhardt</field>' |
| 1074 |
"</object>" |
| 1075 |
'<object pk="2" model="fixtures.person">' |
| 1076 |
'<field type="CharField" name="name">Stephane Grappelli</field>' |
| 1077 |
"</object>" |
| 1078 |
'<object pk="3" model="fixtures.person">' |
| 1079 |
'<field type="CharField" name="name">Prince</field>' |
| 1080 |
"</object></django-objects>", |
| 1081 |
format="xml", |
| 1082 |
natural_foreign_keys=True, |
| 1083 |
) |
| 1084 |
|
| 1085 |
def test_loading_with_exclude_app(self): |
| 1086 |
Site.objects.all().delete() |
| 1087 |
management.call_command( |
| 1088 |
"loaddata", "fixture1", exclude=["fixtures"], verbosity=0 |
| 1089 |
) |
| 1090 |
self.assertFalse(Article.objects.exists()) |
| 1091 |
self.assertFalse(Category.objects.exists()) |
| 1092 |
self.assertEqual(Site.objects.get().domain, "example.com") |
| 1093 |
|
| 1094 |
def test_loading_with_exclude_model(self): |
| 1095 |
Site.objects.all().delete() |
| 1096 |
management.call_command( |
| 1097 |
"loaddata", "fixture1", exclude=["fixtures.Article"], verbosity=0 |
| 1098 |
) |
| 1099 |
self.assertFalse(Article.objects.exists()) |
| 1100 |
self.assertEqual(Category.objects.get().title, "News Stories") |
| 1101 |
self.assertEqual(Site.objects.get().domain, "example.com") |
| 1102 |
|
| 1103 |
def test_exclude_option_errors(self): |
| 1104 |
"""Excluding a bogus app or model should raise an error.""" |
| 1105 |
msg = "No installed app with label 'foo_app'." |
| 1106 |
with self.assertRaisesMessage(management.CommandError, msg): |
| 1107 |
management.call_command( |
| 1108 |
"loaddata", "fixture1", exclude=["foo_app"], verbosity=0 |
| 1109 |
) |
| 1110 |
|
| 1111 |
msg = "Unknown model: fixtures.FooModel" |
| 1112 |
with self.assertRaisesMessage(management.CommandError, msg): |
| 1113 |
management.call_command( |
| 1114 |
"loaddata", "fixture1", exclude=["fixtures.FooModel"], verbosity=0 |
| 1115 |
) |
| 1116 |
|
| 1117 |
def test_stdin_without_format(self): |
| 1118 |
"""Reading from stdin raises an error if format isn't specified.""" |
| 1119 |
msg = "--format must be specified when reading from stdin." |
| 1120 |
with self.assertRaisesMessage(management.CommandError, msg): |
| 1121 |
management.call_command("loaddata", "-", verbosity=0) |
| 1122 |
|
| 1123 |
def test_loading_stdin(self): |
| 1124 |
"""Loading fixtures from stdin with json and xml.""" |
| 1125 |
tests_dir = os.path.dirname(__file__) |
| 1126 |
fixture_json = os.path.join(tests_dir, "fixtures", "fixture1.json") |
| 1127 |
fixture_xml = os.path.join(tests_dir, "fixtures", "fixture3.xml") |
| 1128 |
|
| 1129 |
with mock.patch( |
| 1130 |
"django.core.management.commands.loaddata.sys.stdin", open(fixture_json) |
| 1131 |
): |
| 1132 |
management.call_command("loaddata", "--format=json", "-", verbosity=0) |
| 1133 |
self.assertSequenceEqual( |
| 1134 |
Article.objects.values_list("headline", flat=True), |
| 1135 |
["Time to reform copyright", "Poker has no place on ESPN"], |
| 1136 |
) |
| 1137 |
|
| 1138 |
with mock.patch( |
| 1139 |
"django.core.management.commands.loaddata.sys.stdin", open(fixture_xml) |
| 1140 |
): |
| 1141 |
management.call_command("loaddata", "--format=xml", "-", verbosity=0) |
| 1142 |
self.assertSequenceEqual( |
| 1143 |
Article.objects.values_list("headline", flat=True), |
| 1144 |
[ |
| 1145 |
"XML identified as leading cause of cancer", |
| 1146 |
"Time to reform copyright", |
| 1147 |
"Poker on TV is great!", |
| 1148 |
], |
| 1149 |
) |
| 1150 |
|
| 1151 |
|
| 1152 |
class NonexistentFixtureTests(TestCase): |
| 1153 |
""" |
| 1154 |
Custom class to limit fixture dirs. |
| 1155 |
""" |
| 1156 |
|
| 1157 |
def test_loaddata_not_existent_fixture_file(self): |
| 1158 |
stdout_output = StringIO() |
| 1159 |
with self.assertRaisesMessage( |
| 1160 |
CommandError, "No fixture named 'this_fixture_doesnt_exist' found." |
| 1161 |
): |
| 1162 |
management.call_command( |
| 1163 |
"loaddata", "this_fixture_doesnt_exist", stdout=stdout_output |
| 1164 |
) |
| 1165 |
|
| 1166 |
@mock.patch("django.db.connection.enable_constraint_checking") |
| 1167 |
@mock.patch("django.db.connection.disable_constraint_checking") |
| 1168 |
def test_nonexistent_fixture_no_constraint_checking( |
| 1169 |
self, disable_constraint_checking, enable_constraint_checking |
| 1170 |
): |
| 1171 |
""" |
| 1172 |
If no fixtures match the loaddata command, constraints checks on the |
| 1173 |
database shouldn't be disabled. This is performance critical on MSSQL. |
| 1174 |
""" |
| 1175 |
with self.assertRaisesMessage( |
| 1176 |
CommandError, "No fixture named 'this_fixture_doesnt_exist' found." |
| 1177 |
): |
| 1178 |
management.call_command( |
| 1179 |
"loaddata", "this_fixture_doesnt_exist", verbosity=0 |
| 1180 |
) |
| 1181 |
disable_constraint_checking.assert_not_called() |
| 1182 |
enable_constraint_checking.assert_not_called() |
| 1183 |
|
| 1184 |
|
| 1185 |
class FixtureTransactionTests(DumpDataAssertMixin, TransactionTestCase): |
| 1186 |
available_apps = [ |
| 1187 |
"fixtures", |
| 1188 |
"django.contrib.sites", |
| 1189 |
] |
| 1190 |
|
| 1191 |
@skipUnlessDBFeature("supports_forward_references") |
| 1192 |
def test_format_discovery(self): |
| 1193 |
# Load fixture 1 again, using format discovery |
| 1194 |
management.call_command("loaddata", "fixture1", verbosity=0) |
| 1195 |
self.assertSequenceEqual( |
| 1196 |
Article.objects.values_list("headline", flat=True), |
| 1197 |
["Time to reform copyright", "Poker has no place on ESPN"], |
| 1198 |
) |
| 1199 |
|
| 1200 |
# Try to load fixture 2 using format discovery; this will fail |
| 1201 |
# because there are two fixture2's in the fixtures directory |
| 1202 |
msg = "Multiple fixtures named 'fixture2'" |
| 1203 |
with self.assertRaisesMessage(management.CommandError, msg): |
| 1204 |
management.call_command("loaddata", "fixture2", verbosity=0) |
| 1205 |
|
| 1206 |
# object list is unaffected |
| 1207 |
self.assertSequenceEqual( |
| 1208 |
Article.objects.values_list("headline", flat=True), |
| 1209 |
["Time to reform copyright", "Poker has no place on ESPN"], |
| 1210 |
) |
| 1211 |
|
| 1212 |
# Dump the current contents of the database as a JSON fixture |
| 1213 |
self._dumpdata_assert( |
| 1214 |
["fixtures"], |
| 1215 |
'[{"pk": 1, "model": "fixtures.category", "fields": ' |
| 1216 |
'{"description": "Latest news stories", "title": "News Stories"}}, ' |
| 1217 |
'{"pk": 2, "model": "fixtures.article", "fields": ' |
| 1218 |
'{"headline": "Poker has no place on ESPN", ' |
| 1219 |
'"pub_date": "2006-06-16T12:00:00"}}, ' |
| 1220 |
'{"pk": 3, "model": "fixtures.article", "fields": ' |
| 1221 |
'{"headline": "Time to reform copyright", ' |
| 1222 |
'"pub_date": "2006-06-16T13:00:00"}}]', |
| 1223 |
) |
| 1224 |
|
| 1225 |
# Load fixture 4 (compressed), using format discovery |
| 1226 |
management.call_command("loaddata", "fixture4", verbosity=0) |
| 1227 |
self.assertSequenceEqual( |
| 1228 |
Article.objects.values_list("headline", flat=True), |
| 1229 |
[ |
| 1230 |
"Django pets kitten", |
| 1231 |
"Time to reform copyright", |
| 1232 |
"Poker has no place on ESPN", |
| 1233 |
], |
| 1234 |
) |
| 1235 |
|
| 1236 |
|
| 1237 |
class ForwardReferenceTests(DumpDataAssertMixin, TestCase): |
| 1238 |
def test_forward_reference_fk(self): |
| 1239 |
management.call_command("loaddata", "forward_reference_fk.json", verbosity=0) |
| 1240 |
t1, t2 = NaturalKeyThing.objects.all() |
| 1241 |
self.assertEqual(t1.other_thing, t2) |
| 1242 |
self.assertEqual(t2.other_thing, t1) |
| 1243 |
self._dumpdata_assert( |
| 1244 |
["fixtures"], |
| 1245 |
'[{"model": "fixtures.naturalkeything", "pk": 1, ' |
| 1246 |
'"fields": {"key": "t1", "other_thing": 2, "other_things": []}}, ' |
| 1247 |
'{"model": "fixtures.naturalkeything", "pk": 2, ' |
| 1248 |
'"fields": {"key": "t2", "other_thing": 1, "other_things": []}}]', |
| 1249 |
) |
| 1250 |
|
| 1251 |
def test_forward_reference_fk_natural_key(self): |
| 1252 |
management.call_command( |
| 1253 |
"loaddata", |
| 1254 |
"forward_reference_fk_natural_key.json", |
| 1255 |
verbosity=0, |
| 1256 |
) |
| 1257 |
t1, t2 = NaturalKeyThing.objects.all() |
| 1258 |
self.assertEqual(t1.other_thing, t2) |
| 1259 |
self.assertEqual(t2.other_thing, t1) |
| 1260 |
self._dumpdata_assert( |
| 1261 |
["fixtures"], |
| 1262 |
'[{"model": "fixtures.naturalkeything", ' |
| 1263 |
'"fields": {"key": "t1", "other_thing": ["t2"], "other_things": []}}, ' |
| 1264 |
'{"model": "fixtures.naturalkeything", ' |
| 1265 |
'"fields": {"key": "t2", "other_thing": ["t1"], "other_things": []}}]', |
| 1266 |
natural_primary_keys=True, |
| 1267 |
natural_foreign_keys=True, |
| 1268 |
) |
| 1269 |
|
| 1270 |
def test_forward_reference_m2m(self): |
| 1271 |
management.call_command("loaddata", "forward_reference_m2m.json", verbosity=0) |
| 1272 |
self.assertEqual(NaturalKeyThing.objects.count(), 3) |
| 1273 |
t1 = NaturalKeyThing.objects.get_by_natural_key("t1") |
| 1274 |
self.assertSequenceEqual( |
| 1275 |
t1.other_things.order_by("key").values_list("key", flat=True), |
| 1276 |
["t2", "t3"], |
| 1277 |
) |
| 1278 |
self._dumpdata_assert( |
| 1279 |
["fixtures"], |
| 1280 |
'[{"model": "fixtures.naturalkeything", "pk": 1, ' |
| 1281 |
'"fields": {"key": "t1", "other_thing": null, "other_things": [2, 3]}}, ' |
| 1282 |
'{"model": "fixtures.naturalkeything", "pk": 2, ' |
| 1283 |
'"fields": {"key": "t2", "other_thing": null, "other_things": []}}, ' |
| 1284 |
'{"model": "fixtures.naturalkeything", "pk": 3, ' |
| 1285 |
'"fields": {"key": "t3", "other_thing": null, "other_things": []}}]', |
| 1286 |
) |
| 1287 |
|
| 1288 |
def test_forward_reference_m2m_natural_key(self): |
| 1289 |
management.call_command( |
| 1290 |
"loaddata", |
| 1291 |
"forward_reference_m2m_natural_key.json", |
| 1292 |
verbosity=0, |
| 1293 |
) |
| 1294 |
self.assertEqual(NaturalKeyThing.objects.count(), 3) |
| 1295 |
t1 = NaturalKeyThing.objects.get_by_natural_key("t1") |
| 1296 |
self.assertSequenceEqual( |
| 1297 |
t1.other_things.order_by("key").values_list("key", flat=True), |
| 1298 |
["t2", "t3"], |
| 1299 |
) |
| 1300 |
self._dumpdata_assert( |
| 1301 |
["fixtures"], |
| 1302 |
'[{"model": "fixtures.naturalkeything", ' |
| 1303 |
'"fields": {"key": "t1", "other_thing": null, ' |
| 1304 |
'"other_things": [["t2"], ["t3"]]}}, ' |
| 1305 |
'{"model": "fixtures.naturalkeything", ' |
| 1306 |
'"fields": {"key": "t2", "other_thing": null, "other_things": []}}, ' |
| 1307 |
'{"model": "fixtures.naturalkeything", ' |
| 1308 |
'"fields": {"key": "t3", "other_thing": null, "other_things": []}}]', |
| 1309 |
natural_primary_keys=True, |
| 1310 |
natural_foreign_keys=True, |
| 1311 |
) |
| 1312 |
|
| 1313 |
|
| 1314 |
class CircularReferenceTests(DumpDataAssertMixin, TestCase): |
| 1315 |
def test_circular_reference(self): |
| 1316 |
management.call_command("loaddata", "circular_reference.json", verbosity=0) |
| 1317 |
obj_a = CircularA.objects.get() |
| 1318 |
obj_b = CircularB.objects.get() |
| 1319 |
self.assertEqual(obj_a.obj, obj_b) |
| 1320 |
self.assertEqual(obj_b.obj, obj_a) |
| 1321 |
self._dumpdata_assert( |
| 1322 |
["fixtures"], |
| 1323 |
'[{"model": "fixtures.circulara", "pk": 1, ' |
| 1324 |
'"fields": {"key": "x", "obj": 1}}, ' |
| 1325 |
'{"model": "fixtures.circularb", "pk": 1, ' |
| 1326 |
'"fields": {"key": "y", "obj": 1}}]', |
| 1327 |
) |
| 1328 |
|
| 1329 |
def test_circular_reference_natural_key(self): |
| 1330 |
management.call_command( |
| 1331 |
"loaddata", |
| 1332 |
"circular_reference_natural_key.json", |
| 1333 |
verbosity=0, |
| 1334 |
) |
| 1335 |
obj_a = CircularA.objects.get() |
| 1336 |
obj_b = CircularB.objects.get() |
| 1337 |
self.assertEqual(obj_a.obj, obj_b) |
| 1338 |
self.assertEqual(obj_b.obj, obj_a) |
| 1339 |
self._dumpdata_assert( |
| 1340 |
["fixtures"], |
| 1341 |
'[{"model": "fixtures.circulara", ' |
| 1342 |
'"fields": {"key": "x", "obj": ["y"]}}, ' |
| 1343 |
'{"model": "fixtures.circularb", ' |
| 1344 |
'"fields": {"key": "y", "obj": ["x"]}}]', |
| 1345 |
natural_primary_keys=True, |
| 1346 |
natural_foreign_keys=True, |
| 1347 |
) |