2021-01-12 14:47:58 +00:00
|
|
|
import gzip
|
2014-03-23 05:10:12 +00:00
|
|
|
import os
|
2015-06-17 14:19:15 +00:00
|
|
|
import sys
|
2015-02-21 20:09:30 +00:00
|
|
|
import tempfile
|
2015-06-17 14:19:15 +00:00
|
|
|
import unittest
|
2016-01-27 11:15:38 +00:00
|
|
|
import warnings
|
2017-01-07 11:11:46 +00:00
|
|
|
from io import StringIO
|
2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2013-05-19 09:20:10 +00:00
|
|
|
|
2015-01-07 00:16:35 +00:00
|
|
|
from django.apps import apps
|
2011-03-09 23:46:28 +00:00
|
|
|
from django.contrib.sites.models import Site
|
2010-06-05 05:26:04 +00:00
|
|
|
from django.core import management
|
2015-09-09 18:08:35 +00:00
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
2016-01-02 19:11:18 +00:00
|
|
|
from django.core.management import CommandError
|
2016-01-27 11:15:38 +00:00
|
|
|
from django.core.management.commands.dumpdata import ProxyModelWarning
|
2015-07-21 21:24:32 +00:00
|
|
|
from django.core.serializers.base import ProgressBar
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.db import IntegrityError, connection
|
2017-01-19 17:16:04 +00:00
|
|
|
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2016-07-16 10:27:25 +00:00
|
|
|
from .models import (
|
2022-02-03 19:24:19 +00:00
|
|
|
Article,
|
|
|
|
Category,
|
|
|
|
CircularA,
|
|
|
|
CircularB,
|
|
|
|
NaturalKeyThing,
|
|
|
|
PrimaryKeyUUIDModel,
|
|
|
|
ProxySpy,
|
|
|
|
Spy,
|
|
|
|
Tag,
|
|
|
|
Visa,
|
2016-07-16 10:27:25 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2020-05-07 13:57:37 +00:00
|
|
|
try:
|
|
|
|
import bz2 # NOQA
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2020-05-07 13:57:37 +00:00
|
|
|
HAS_BZ2 = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_BZ2 = False
|
|
|
|
|
2020-05-15 07:40:42 +00:00
|
|
|
try:
|
|
|
|
import lzma # NOQA
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2020-05-15 07:40:42 +00:00
|
|
|
HAS_LZMA = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_LZMA = False
|
|
|
|
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-06-05 05:26:04 +00:00
|
|
|
class TestCaseFixtureLoadingTests(TestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
fixtures = ["fixture1.json", "fixture2.json"]
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2019-04-14 13:00:48 +00:00
|
|
|
def test_class_fixtures(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"Test case has installed 3 fixture objects"
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
|
|
|
[
|
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker has no place on ESPN",
|
2020-10-18 16:29:52 +00:00
|
|
|
],
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2012-09-07 17:17:09 +00:00
|
|
|
|
2013-09-11 13:20:15 +00:00
|
|
|
class SubclassTestCaseFixtureLoadingTests(TestCaseFixtureLoadingTests):
|
|
|
|
"""
|
|
|
|
Make sure that subclasses can remove fixtures from parent class (#21089).
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2013-09-11 13:20:15 +00:00
|
|
|
fixtures = []
|
|
|
|
|
2019-04-14 13:00:48 +00:00
|
|
|
def test_class_fixtures(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"There were no fixture objects installed"
|
2013-09-11 13:20:15 +00:00
|
|
|
self.assertEqual(Article.objects.count(), 0)
|
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class DumpDataAssertMixin:
|
2022-02-03 19:24:19 +00:00
|
|
|
def _dumpdata_assert(
|
|
|
|
self,
|
|
|
|
args,
|
|
|
|
output,
|
|
|
|
format="json",
|
|
|
|
filename=None,
|
|
|
|
natural_foreign_keys=False,
|
|
|
|
natural_primary_keys=False,
|
|
|
|
use_base_manager=False,
|
|
|
|
exclude_list=[],
|
|
|
|
primary_keys="",
|
|
|
|
):
|
2017-01-07 11:11:46 +00:00
|
|
|
new_io = StringIO()
|
2018-01-03 23:52:12 +00:00
|
|
|
filename = filename and os.path.join(tempfile.gettempdir(), filename)
|
2019-01-02 23:18:19 +00:00
|
|
|
management.call_command(
|
2022-02-03 19:24:19 +00:00
|
|
|
"dumpdata",
|
2019-01-02 23:18:19 +00:00
|
|
|
*args,
|
|
|
|
format=format,
|
|
|
|
stdout=new_io,
|
|
|
|
stderr=new_io,
|
|
|
|
output=filename,
|
|
|
|
use_natural_foreign_keys=natural_foreign_keys,
|
|
|
|
use_natural_primary_keys=natural_primary_keys,
|
|
|
|
use_base_manager=use_base_manager,
|
|
|
|
exclude=exclude_list,
|
|
|
|
primary_keys=primary_keys,
|
|
|
|
)
|
2014-03-23 05:10:12 +00:00
|
|
|
if filename:
|
2021-01-12 14:47:58 +00:00
|
|
|
file_root, file_ext = os.path.splitext(filename)
|
|
|
|
compression_formats = {
|
2022-02-03 19:24:19 +00:00
|
|
|
".bz2": (open, file_root),
|
|
|
|
".gz": (gzip.open, filename),
|
|
|
|
".lzma": (open, file_root),
|
|
|
|
".xz": (open, file_root),
|
|
|
|
".zip": (open, file_root),
|
2021-01-12 14:47:58 +00:00
|
|
|
}
|
|
|
|
if HAS_BZ2:
|
2022-02-03 19:24:19 +00:00
|
|
|
compression_formats[".bz2"] = (bz2.open, filename)
|
2021-01-12 14:47:58 +00:00
|
|
|
if HAS_LZMA:
|
2022-02-03 19:24:19 +00:00
|
|
|
compression_formats[".lzma"] = (lzma.open, filename)
|
|
|
|
compression_formats[".xz"] = (lzma.open, filename)
|
2021-01-12 14:47:58 +00:00
|
|
|
try:
|
|
|
|
open_method, file_path = compression_formats[file_ext]
|
|
|
|
except KeyError:
|
|
|
|
open_method, file_path = open, filename
|
2022-02-03 19:24:19 +00:00
|
|
|
with open_method(file_path, "rt") as f:
|
2014-03-30 18:50:18 +00:00
|
|
|
command_output = f.read()
|
2021-01-12 14:47:58 +00:00
|
|
|
os.remove(file_path)
|
2014-03-23 05:10:12 +00:00
|
|
|
else:
|
|
|
|
command_output = new_io.getvalue().strip()
|
2012-09-28 07:20:01 +00:00
|
|
|
if format == "json":
|
|
|
|
self.assertJSONEqual(command_output, output)
|
|
|
|
elif format == "xml":
|
|
|
|
self.assertXMLEqual(command_output, output)
|
|
|
|
else:
|
|
|
|
self.assertEqual(command_output, output)
|
|
|
|
|
|
|
|
|
|
|
|
class FixtureLoadingTests(DumpDataAssertMixin, TestCase):
|
2010-06-05 05:26:04 +00:00
|
|
|
def test_loading_and_dumping(self):
|
2015-01-07 00:16:35 +00:00
|
|
|
apps.clear_cache()
|
2011-03-09 23:46:28 +00:00
|
|
|
Site.objects.all().delete()
|
2010-06-05 05:26:04 +00:00
|
|
|
# Load fixture 1. Single JSON file, with two objects.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
|
|
|
["Time to reform copyright", "Poker has no place on ESPN"],
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Dump the current contents of the database as a JSON fixture
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Try just dumping the contents of fixtures.Category
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Category"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# ...and just fixtures.Article
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Article"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# ...and both
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Category", "fixtures.Article"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Specify a specific model twice
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Article", "fixtures.Article"],
|
2015-09-11 23:33:12 +00:00
|
|
|
(
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]'
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Specify a dump that specifies Article both explicitly and implicitly
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Article", "fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2014-02-11 14:59:57 +00:00
|
|
|
# Specify a dump that specifies Article both explicitly and implicitly,
|
|
|
|
# but lists the app first (#22025).
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures", "fixtures.Article"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2014-02-11 14:59:57 +00:00
|
|
|
|
2010-06-05 05:26:04 +00:00
|
|
|
# Same again, but specify in the reverse order
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Specify one model from one application, and an entire other application.
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Category", "sites"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 1, "model": "sites.site", "fields": '
|
|
|
|
'{"domain": "example.com", "name": "example.com"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2022-02-04 07:08:27 +00:00
|
|
|
# Load fixture 2. JSON file imported by default. Overwrites some
|
|
|
|
# existing objects.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture2.json", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
2020-10-18 16:29:52 +00:00
|
|
|
[
|
2022-02-03 19:24:19 +00:00
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker has no place on ESPN",
|
2020-10-18 16:29:52 +00:00
|
|
|
],
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Load fixture 3, XML format.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture3.xml", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
2020-10-18 16:29:52 +00:00
|
|
|
[
|
2022-02-03 19:24:19 +00:00
|
|
|
"XML identified as leading cause of cancer",
|
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker on TV is great!",
|
2020-10-18 16:29:52 +00:00
|
|
|
],
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture6.json", verbosity=0)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.all(),
|
|
|
|
[
|
|
|
|
'<Tag: <Article: Copyright is fine the way it is> tagged "copyright">',
|
|
|
|
'<Tag: <Article: Copyright is fine the way it is> tagged "law">',
|
|
|
|
],
|
|
|
|
transform=repr,
|
|
|
|
ordered=False,
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture7.xml", verbosity=0)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.all(),
|
|
|
|
[
|
|
|
|
'<Tag: <Article: Copyright is fine the way it is> tagged "copyright">',
|
|
|
|
'<Tag: <Article: Copyright is fine the way it is> tagged "legal">',
|
|
|
|
'<Tag: <Article: Django conquers world!> tagged "django">',
|
|
|
|
'<Tag: <Article: Django conquers world!> tagged "world domination">',
|
|
|
|
],
|
|
|
|
transform=repr,
|
|
|
|
ordered=False,
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture8.json", verbosity=0)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Visa.objects.all(),
|
|
|
|
[
|
2022-02-04 07:08:27 +00:00
|
|
|
"<Visa: Django Reinhardt Can add user, Can change user, Can delete "
|
|
|
|
"user>",
|
2022-02-03 19:24:19 +00:00
|
|
|
"<Visa: Stephane Grappelli Can add user>",
|
|
|
|
"<Visa: Prince >",
|
|
|
|
],
|
|
|
|
transform=repr,
|
|
|
|
ordered=False,
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture9.xml", verbosity=0)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Visa.objects.all(),
|
|
|
|
[
|
2022-02-04 07:08:27 +00:00
|
|
|
"<Visa: Django Reinhardt Can add user, Can change user, Can delete "
|
|
|
|
"user>",
|
2022-02-03 19:24:19 +00:00
|
|
|
"<Visa: Stephane Grappelli Can add user, Can delete user>",
|
|
|
|
'<Visa: Artist formerly known as "Prince" Can change user>',
|
|
|
|
],
|
|
|
|
transform=repr,
|
|
|
|
ordered=False,
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# object list is unaffected
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
2020-10-18 16:29:52 +00:00
|
|
|
[
|
2022-02-03 19:24:19 +00:00
|
|
|
"XML identified as leading cause of cancer",
|
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker on TV is great!",
|
2020-10-18 16:29:52 +00:00
|
|
|
],
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# By default, you get raw keys on dumpdata
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.book"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.book", "fields": '
|
|
|
|
'{"name": "Music for all ages", "authors": [3, 1]}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# But you can get natural keys if you ask for them and they are available
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.book"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.book", "fields": '
|
|
|
|
'{"name": "Music for all ages", "authors": '
|
|
|
|
'[["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
natural_foreign_keys=True,
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2012-08-01 01:49:01 +00:00
|
|
|
|
2022-02-04 07:08:27 +00:00
|
|
|
# You can also omit the primary keys for models that we can get later
|
|
|
|
# with natural keys.
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.person"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"fields": {"name": "Django Reinhardt"}, "model": "fixtures.person"}, '
|
|
|
|
'{"fields": {"name": "Stephane Grappelli"}, "model": "fixtures.person"}, '
|
|
|
|
'{"fields": {"name": "Artist formerly known as \\"Prince\\""}, '
|
|
|
|
'"model": "fixtures.person"}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
natural_primary_keys=True,
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Dump the current contents of the database as a JSON fixture
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker on TV is great!", '
|
|
|
|
'"pub_date": "2006-06-16T11:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Copyright is fine the way it is", '
|
|
|
|
'"pub_date": "2006-06-16T14:00:00"}}, '
|
|
|
|
'{"pk": 4, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Django conquers world!", '
|
|
|
|
'"pub_date": "2006-06-16T15:00:00"}}, '
|
|
|
|
'{"pk": 5, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "XML identified as leading cause of cancer", '
|
|
|
|
'"pub_date": "2006-06-16T16:00:00"}}, '
|
|
|
|
'{"pk": 1, "model": "fixtures.tag", "fields": '
|
|
|
|
'{"tagged_type": ["fixtures", "article"], "name": "copyright", '
|
|
|
|
'"tagged_id": 3}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.tag", "fields": '
|
|
|
|
'{"tagged_type": ["fixtures", "article"], "name": "legal", '
|
|
|
|
'"tagged_id": 3}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.tag", "fields": '
|
|
|
|
'{"tagged_type": ["fixtures", "article"], "name": "django", '
|
|
|
|
'"tagged_id": 4}}, '
|
|
|
|
'{"pk": 4, "model": "fixtures.tag", "fields": '
|
|
|
|
'{"tagged_type": ["fixtures", "article"], "name": "world domination", '
|
|
|
|
'"tagged_id": 4}}, '
|
|
|
|
'{"pk": 1, "model": "fixtures.person", '
|
|
|
|
'"fields": {"name": "Django Reinhardt"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.person", '
|
|
|
|
'"fields": {"name": "Stephane Grappelli"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.person", '
|
|
|
|
'"fields": {"name": "Artist formerly known as \\"Prince\\""}}, '
|
|
|
|
'{"pk": 1, "model": "fixtures.visa", '
|
|
|
|
'"fields": {"person": ["Django Reinhardt"], "permissions": '
|
|
|
|
'[["add_user", "auth", "user"], ["change_user", "auth", "user"], '
|
|
|
|
'["delete_user", "auth", "user"]]}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.visa", "fields": '
|
|
|
|
'{"person": ["Stephane Grappelli"], "permissions": '
|
|
|
|
'[["add_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.visa", "fields": '
|
|
|
|
'{"person": ["Artist formerly known as \\"Prince\\""], "permissions": '
|
|
|
|
'[["change_user", "auth", "user"]]}}, '
|
|
|
|
'{"pk": 1, "model": "fixtures.book", "fields": '
|
|
|
|
'{"name": "Music for all ages", "authors": '
|
|
|
|
'[["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
natural_foreign_keys=True,
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Dump the current contents of the database as an XML fixture
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'<?xml version="1.0" encoding="utf-8"?><django-objects version="1.0">'
|
|
|
|
'<object pk="1" model="fixtures.category">'
|
|
|
|
'<field type="CharField" name="title">News Stories</field>'
|
|
|
|
'<field type="TextField" name="description">Latest news stories</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="2" model="fixtures.article">'
|
|
|
|
'<field type="CharField" name="headline">Poker on TV is great!</field>'
|
|
|
|
'<field type="DateTimeField" name="pub_date">2006-06-16T11:00:00</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="3" model="fixtures.article">'
|
|
|
|
'<field type="CharField" name="headline">Copyright is fine the way it is'
|
|
|
|
"</field>"
|
|
|
|
'<field type="DateTimeField" name="pub_date">2006-06-16T14:00:00</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="4" model="fixtures.article">'
|
|
|
|
'<field type="CharField" name="headline">Django conquers world!</field>'
|
|
|
|
'<field type="DateTimeField" name="pub_date">2006-06-16T15:00:00</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="5" model="fixtures.article">'
|
|
|
|
'<field type="CharField" name="headline">'
|
|
|
|
"XML identified as leading cause of cancer</field>"
|
|
|
|
'<field type="DateTimeField" name="pub_date">2006-06-16T16:00:00</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="1" model="fixtures.tag">'
|
|
|
|
'<field type="CharField" name="name">copyright</field>'
|
|
|
|
'<field to="contenttypes.contenttype" name="tagged_type" '
|
|
|
|
'rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural>'
|
|
|
|
"</field>"
|
|
|
|
'<field type="PositiveIntegerField" name="tagged_id">3</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="2" model="fixtures.tag">'
|
|
|
|
'<field type="CharField" name="name">legal</field>'
|
|
|
|
'<field to="contenttypes.contenttype" name="tagged_type" '
|
|
|
|
'rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural>'
|
|
|
|
"</field>"
|
|
|
|
'<field type="PositiveIntegerField" name="tagged_id">3</field></object>'
|
|
|
|
'<object pk="3" model="fixtures.tag">'
|
|
|
|
'<field type="CharField" name="name">django</field>'
|
|
|
|
'<field to="contenttypes.contenttype" name="tagged_type" '
|
|
|
|
'rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural>'
|
|
|
|
"</field>"
|
|
|
|
'<field type="PositiveIntegerField" name="tagged_id">4</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="4" model="fixtures.tag">'
|
|
|
|
'<field type="CharField" name="name">world domination</field>'
|
|
|
|
'<field to="contenttypes.contenttype" name="tagged_type" '
|
|
|
|
'rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural>'
|
|
|
|
"</field>"
|
|
|
|
'<field type="PositiveIntegerField" name="tagged_id">4</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="1" model="fixtures.person">'
|
|
|
|
'<field type="CharField" name="name">Django Reinhardt</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="2" model="fixtures.person">'
|
|
|
|
'<field type="CharField" name="name">Stephane Grappelli</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="3" model="fixtures.person">'
|
|
|
|
'<field type="CharField" name="name">Artist formerly known as "Prince"'
|
|
|
|
"</field>"
|
|
|
|
"</object>"
|
|
|
|
'<object pk="1" model="fixtures.visa">'
|
|
|
|
'<field to="fixtures.person" name="person" rel="ManyToOneRel">'
|
|
|
|
"<natural>Django Reinhardt</natural></field>"
|
|
|
|
'<field to="auth.permission" name="permissions" rel="ManyToManyRel">'
|
|
|
|
"<object><natural>add_user</natural><natural>auth</natural>"
|
|
|
|
"<natural>user</natural></object><object><natural>change_user</natural>"
|
|
|
|
"<natural>auth</natural><natural>user</natural></object>"
|
|
|
|
"<object><natural>delete_user</natural><natural>auth</natural>"
|
|
|
|
"<natural>user</natural></object></field>"
|
|
|
|
"</object>"
|
|
|
|
'<object pk="2" model="fixtures.visa">'
|
|
|
|
'<field to="fixtures.person" name="person" rel="ManyToOneRel">'
|
|
|
|
"<natural>Stephane Grappelli</natural></field>"
|
|
|
|
'<field to="auth.permission" name="permissions" rel="ManyToManyRel">'
|
|
|
|
"<object><natural>add_user</natural><natural>auth</natural>"
|
|
|
|
"<natural>user</natural></object>"
|
|
|
|
"<object><natural>delete_user</natural><natural>auth</natural>"
|
|
|
|
"<natural>user</natural></object></field>"
|
|
|
|
"</object>"
|
|
|
|
'<object pk="3" model="fixtures.visa">'
|
|
|
|
'<field to="fixtures.person" name="person" rel="ManyToOneRel">'
|
|
|
|
'<natural>Artist formerly known as "Prince"</natural></field>'
|
|
|
|
'<field to="auth.permission" name="permissions" rel="ManyToManyRel">'
|
|
|
|
"<object><natural>change_user</natural><natural>auth</natural>"
|
|
|
|
"<natural>user</natural></object></field>"
|
|
|
|
"</object>"
|
|
|
|
'<object pk="1" model="fixtures.book">'
|
|
|
|
'<field type="CharField" name="name">Music for all ages</field>'
|
|
|
|
'<field to="fixtures.person" name="authors" rel="ManyToManyRel">'
|
|
|
|
'<object><natural>Artist formerly known as "Prince"</natural></object>'
|
|
|
|
"<object><natural>Django Reinhardt</natural></object></field>"
|
|
|
|
"</object></django-objects>",
|
2022-02-03 19:24:19 +00:00
|
|
|
format="xml",
|
|
|
|
natural_foreign_keys=True,
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2010-08-06 16:48:07 +00:00
|
|
|
def test_dumpdata_with_excludes(self):
|
|
|
|
# Load fixture1 which has a site, two articles, and a category
|
2011-03-09 23:46:28 +00:00
|
|
|
Site.objects.all().delete()
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2010-08-06 16:48:07 +00:00
|
|
|
|
|
|
|
# Excluding fixtures app should only leave sites
|
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["sites", "fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "sites.site", "fields": '
|
|
|
|
'{"domain": "example.com", "name": "example.com"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
exclude_list=["fixtures"],
|
2018-03-16 09:54:34 +00:00
|
|
|
)
|
2010-08-06 16:48:07 +00:00
|
|
|
|
2011-11-27 13:00:56 +00:00
|
|
|
# Excluding fixtures.Article/Book should leave fixtures.Category
|
2010-08-06 16:48:07 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["sites", "fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "sites.site", '
|
|
|
|
'"fields": {"domain": "example.com", "name": "example.com"}}, '
|
|
|
|
'{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
exclude_list=["fixtures.Article", "fixtures.Book"],
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-08-06 16:48:07 +00:00
|
|
|
|
2011-11-27 13:00:56 +00:00
|
|
|
# Excluding fixtures and fixtures.Article/Book should be a no-op
|
2010-08-06 16:48:07 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["sites", "fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "sites.site", '
|
|
|
|
'"fields": {"domain": "example.com", "name": "example.com"}}, '
|
|
|
|
'{"pk": 1, "model": "fixtures.category", '
|
|
|
|
'"fields": {"description": "Latest news stories", '
|
|
|
|
'"title": "News Stories"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
exclude_list=["fixtures.Article", "fixtures.Book"],
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-08-06 16:48:07 +00:00
|
|
|
|
2011-11-27 13:00:56 +00:00
|
|
|
# Excluding sites and fixtures.Article/Book should only leave fixtures.Category
|
2010-08-06 16:48:07 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["sites", "fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
exclude_list=["fixtures.Article", "fixtures.Book", "sites"],
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-08-06 16:48:07 +00:00
|
|
|
|
|
|
|
# Excluding a bogus app should throw an error
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
management.CommandError, "No installed app with label 'foo_app'."
|
|
|
|
):
|
|
|
|
self._dumpdata_assert(["fixtures", "sites"], "", exclude_list=["foo_app"])
|
2010-08-06 16:48:07 +00:00
|
|
|
|
|
|
|
# Excluding a bogus model should throw an error
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
management.CommandError, "Unknown model: fixtures.FooModel"
|
|
|
|
):
|
|
|
|
self._dumpdata_assert(
|
|
|
|
["fixtures", "sites"], "", exclude_list=["fixtures.FooModel"]
|
|
|
|
)
|
2010-08-06 16:48:07 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipIf(
|
|
|
|
sys.platform == "win32", "Windows doesn't support '?' in filenames."
|
|
|
|
)
|
2015-06-13 10:20:05 +00:00
|
|
|
def test_load_fixture_with_special_characters(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture_with[special]chars", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get().headline,
|
2022-02-03 19:24:19 +00:00
|
|
|
"How To Deal With Special Characters",
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2015-06-13 10:20:05 +00:00
|
|
|
|
2010-08-30 11:58:26 +00:00
|
|
|
def test_dumpdata_with_filtering_manager(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
spy1 = Spy.objects.create(name="Paul")
|
|
|
|
spy2 = Spy.objects.create(name="Alex", cover_blown=True)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(Spy.objects.all(), [spy1])
|
2010-08-30 11:58:26 +00:00
|
|
|
# Use the default manager
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Spy"],
|
|
|
|
'[{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": false}}]'
|
|
|
|
% spy1.pk,
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-08-30 11:58:26 +00:00
|
|
|
# Dump using Django's base manager. Should return all objects,
|
|
|
|
# even those normally filtered by the manager
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Spy"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": true}}, '
|
|
|
|
'{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": false}}]'
|
|
|
|
% (spy2.pk, spy1.pk),
|
2022-02-03 19:24:19 +00:00
|
|
|
use_base_manager=True,
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-08-30 11:58:26 +00:00
|
|
|
|
2013-05-19 10:39:14 +00:00
|
|
|
def test_dumpdata_with_pks(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
|
|
|
management.call_command("loaddata", "fixture2.json", verbosity=0)
|
2013-05-19 10:39:14 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Article"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 2, "model": "fixtures.article", '
|
|
|
|
'"fields": {"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Copyright is fine the way it is", '
|
|
|
|
'"pub_date": "2006-06-16T14:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
primary_keys="2,3",
|
2013-05-19 10:39:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Article"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 2, "model": "fixtures.article", '
|
|
|
|
'"fields": {"headline": "Poker has no place on ESPN", '
|
2015-09-11 23:33:12 +00:00
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
primary_keys="2",
|
2013-05-19 10:39:14 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
management.CommandError, "You can only use --pks option with one model"
|
|
|
|
):
|
2013-05-19 10:39:14 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Copyright is fine the way it is", '
|
|
|
|
'"pub_date": "2006-06-16T14:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
primary_keys="2,3",
|
2013-05-19 10:39:14 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
management.CommandError, "You can only use --pks option with one model"
|
|
|
|
):
|
2013-05-19 10:39:14 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
"",
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Copyright is fine the way it is", '
|
|
|
|
'"pub_date": "2006-06-16T14:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
primary_keys="2,3",
|
2013-05-19 10:39:14 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
management.CommandError, "You can only use --pks option with one model"
|
|
|
|
):
|
2013-05-19 10:39:14 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.Article", "fixtures.category"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Copyright is fine the way it is", '
|
|
|
|
'"pub_date": "2006-06-16T14:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
primary_keys="2,3",
|
2013-05-19 10:39:14 +00:00
|
|
|
)
|
|
|
|
|
2016-07-16 10:27:25 +00:00
|
|
|
def test_dumpdata_with_uuid_pks(self):
|
|
|
|
m1 = PrimaryKeyUUIDModel.objects.create()
|
|
|
|
m2 = PrimaryKeyUUIDModel.objects.create()
|
2017-01-07 11:11:46 +00:00
|
|
|
output = StringIO()
|
2016-07-16 10:27:25 +00:00
|
|
|
management.call_command(
|
2022-02-03 19:24:19 +00:00
|
|
|
"dumpdata",
|
|
|
|
"fixtures.PrimaryKeyUUIDModel",
|
|
|
|
"--pks",
|
|
|
|
", ".join([str(m1.id), str(m2.id)]),
|
2016-07-16 10:27:25 +00:00
|
|
|
stdout=output,
|
|
|
|
)
|
|
|
|
result = output.getvalue()
|
|
|
|
self.assertIn('"pk": "%s"' % m1.id, result)
|
|
|
|
self.assertIn('"pk": "%s"' % m2.id, result)
|
|
|
|
|
2014-03-23 05:10:12 +00:00
|
|
|
def test_dumpdata_with_file_output(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
filename="dumpdata.json",
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2014-03-23 05:10:12 +00:00
|
|
|
|
2021-01-12 14:47:58 +00:00
|
|
|
def test_dumpdata_with_file_gzip_output(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2021-01-12 14:47:58 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
filename="dumpdata.json.gz",
|
2021-01-12 14:47:58 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipUnless(HAS_BZ2, "No bz2 library detected.")
|
2021-01-12 14:47:58 +00:00
|
|
|
def test_dumpdata_with_file_bz2_output(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2021-01-12 14:47:58 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
filename="dumpdata.json.bz2",
|
2021-01-12 14:47:58 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipUnless(HAS_LZMA, "No lzma library detected.")
|
2021-01-12 14:47:58 +00:00
|
|
|
def test_dumpdata_with_file_lzma_output(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2021-01-12 14:47:58 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
filename="dumpdata.json.lzma",
|
2021-01-12 14:47:58 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipUnless(HAS_LZMA, "No lzma library detected.")
|
2021-01-12 14:47:58 +00:00
|
|
|
def test_dumpdata_with_file_xz_output(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2021-01-12 14:47:58 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
filename="dumpdata.json.xz",
|
2021-01-12 14:47:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_dumpdata_with_file_zip_output(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2021-01-12 14:47:58 +00:00
|
|
|
msg = "Unsupported file extension (.zip). Fixtures saved in 'dumpdata.json'."
|
|
|
|
with self.assertWarnsMessage(RuntimeWarning, msg):
|
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
filename="dumpdata.json.zip",
|
2021-01-12 14:47:58 +00:00
|
|
|
)
|
|
|
|
|
2015-07-21 21:24:32 +00:00
|
|
|
def test_dumpdata_progressbar(self):
|
|
|
|
"""
|
|
|
|
Dumpdata shows a progress bar on the command line when --output is set,
|
|
|
|
stdout is a tty, and verbosity > 0.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2017-01-07 11:11:46 +00:00
|
|
|
new_io = StringIO()
|
2015-07-21 21:24:32 +00:00
|
|
|
new_io.isatty = lambda: True
|
2015-09-09 18:08:35 +00:00
|
|
|
with NamedTemporaryFile() as file:
|
|
|
|
options = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"format": "json",
|
|
|
|
"stdout": new_io,
|
|
|
|
"stderr": new_io,
|
|
|
|
"output": file.name,
|
2015-09-09 18:08:35 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("dumpdata", "fixtures", **options)
|
|
|
|
self.assertTrue(
|
|
|
|
new_io.getvalue().endswith(
|
|
|
|
"[" + "." * ProgressBar.progress_width + "]\n"
|
|
|
|
)
|
|
|
|
)
|
2015-09-09 18:08:35 +00:00
|
|
|
|
|
|
|
# Test no progress bar when verbosity = 0
|
2022-02-03 19:24:19 +00:00
|
|
|
options["verbosity"] = 0
|
2017-01-07 11:11:46 +00:00
|
|
|
new_io = StringIO()
|
2015-09-09 18:08:35 +00:00
|
|
|
new_io.isatty = lambda: True
|
2022-02-03 19:24:19 +00:00
|
|
|
options.update({"stdout": new_io, "stderr": new_io})
|
|
|
|
management.call_command("dumpdata", "fixtures", **options)
|
|
|
|
self.assertEqual(new_io.getvalue(), "")
|
2015-07-21 21:24:32 +00:00
|
|
|
|
2016-01-27 11:15:38 +00:00
|
|
|
def test_dumpdata_proxy_without_concrete(self):
|
|
|
|
"""
|
|
|
|
A warning is displayed if a proxy model is dumped without its concrete
|
|
|
|
parent.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
ProxySpy.objects.create(name="Paul")
|
2018-04-27 21:18:15 +00:00
|
|
|
msg = "fixtures.ProxySpy is a proxy model and won't be serialized."
|
|
|
|
with self.assertWarnsMessage(ProxyModelWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
self._dumpdata_assert(["fixtures.ProxySpy"], "[]")
|
2016-01-27 11:15:38 +00:00
|
|
|
|
|
|
|
def test_dumpdata_proxy_with_concrete(self):
|
|
|
|
"""
|
|
|
|
A warning isn't displayed if a proxy model is dumped with its concrete
|
|
|
|
parent.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
spy = ProxySpy.objects.create(name="Paul")
|
2016-01-27 11:15:38 +00:00
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as warning_list:
|
2022-02-03 19:24:19 +00:00
|
|
|
warnings.simplefilter("always")
|
2016-01-27 11:15:38 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures.ProxySpy", "fixtures.Spy"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": %d, "model": "fixtures.spy", '
|
|
|
|
'"fields": {"cover_blown": false}}]' % spy.pk,
|
2016-01-27 11:15:38 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(len(warning_list), 0)
|
|
|
|
|
2010-06-05 05:26:04 +00:00
|
|
|
def test_compress_format_loading(self):
|
|
|
|
# Load fixture 4 (compressed), using format specification
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture4.json", verbosity=0)
|
|
|
|
self.assertEqual(Article.objects.get().headline, "Django pets kitten")
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
def test_compressed_specified_loading(self):
|
|
|
|
# Load fixture 5 (compressed), using format *and* compression specification
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture5.json.zip", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get().headline,
|
2022-02-03 19:24:19 +00:00
|
|
|
"WoW subscribers now outnumber readers",
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
def test_compressed_loading(self):
|
|
|
|
# Load fixture 5 (compressed), only compression specification
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture5.zip", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get().headline,
|
2022-02-03 19:24:19 +00:00
|
|
|
"WoW subscribers now outnumber readers",
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2020-05-07 13:57:37 +00:00
|
|
|
def test_compressed_loading_gzip(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture5.json.gz", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get().headline,
|
2022-02-03 19:24:19 +00:00
|
|
|
"WoW subscribers now outnumber readers",
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2020-05-07 13:57:37 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipUnless(HAS_BZ2, "No bz2 library detected.")
|
2020-05-07 13:57:37 +00:00
|
|
|
def test_compressed_loading_bz2(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture5.json.bz2", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get().headline,
|
2022-02-03 19:24:19 +00:00
|
|
|
"WoW subscribers now outnumber readers",
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2020-05-15 07:40:42 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipUnless(HAS_LZMA, "No lzma library detected.")
|
2020-05-15 07:40:42 +00:00
|
|
|
def test_compressed_loading_lzma(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture5.json.lzma", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get().headline,
|
2022-02-03 19:24:19 +00:00
|
|
|
"WoW subscribers now outnumber readers",
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2020-05-15 07:40:42 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipUnless(HAS_LZMA, "No lzma library detected.")
|
2020-05-15 07:40:42 +00:00
|
|
|
def test_compressed_loading_xz(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture5.json.xz", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get().headline,
|
2022-02-03 19:24:19 +00:00
|
|
|
"WoW subscribers now outnumber readers",
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2020-05-07 13:57:37 +00:00
|
|
|
|
2010-06-05 05:26:04 +00:00
|
|
|
def test_ambiguous_compressed_fixture(self):
|
2020-02-04 20:58:07 +00:00
|
|
|
# The name "fixture5" is ambiguous, so loading raises an error.
|
|
|
|
msg = "Multiple fixtures named 'fixture5'"
|
|
|
|
with self.assertRaisesMessage(management.CommandError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture5", verbosity=0)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
def test_db_loading(self):
|
2022-02-04 07:08:27 +00:00
|
|
|
# Load db fixtures 1 and 2. These will load using the 'default'
|
|
|
|
# database identifier implicitly.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "db_fixture_1", verbosity=0)
|
|
|
|
management.call_command("loaddata", "db_fixture_2", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
2020-10-18 16:29:52 +00:00
|
|
|
[
|
2022-02-03 19:24:19 +00:00
|
|
|
"Who needs more than one database?",
|
|
|
|
"Who needs to use compressed data?",
|
2020-10-18 16:29:52 +00:00
|
|
|
],
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2011-09-22 04:30:20 +00:00
|
|
|
def test_loaddata_error_message(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Loading a fixture which contains an invalid object outputs an error
|
|
|
|
message which contains the pk of the object that triggered the error.
|
2011-09-22 04:30:20 +00:00
|
|
|
"""
|
2011-10-16 14:30:43 +00:00
|
|
|
# MySQL needs a little prodding to reject invalid data.
|
|
|
|
# This won't affect other tests because the database connection
|
|
|
|
# is closed at the end of each test.
|
2022-02-03 19:24:19 +00:00
|
|
|
if connection.vendor == "mysql":
|
2020-02-04 03:07:00 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SET sql_mode = 'TRADITIONAL'")
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "Could not load fixtures.Article(pk=1):"
|
2020-02-04 20:58:07 +00:00
|
|
|
with self.assertRaisesMessage(IntegrityError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "invalid.json", verbosity=0)
|
2011-09-22 04:30:20 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipUnless(
|
|
|
|
connection.vendor == "postgresql", "psycopg2 prohibits null characters in data."
|
|
|
|
)
|
2017-12-30 17:16:11 +00:00
|
|
|
def test_loaddata_null_characters_on_postgresql(self):
|
|
|
|
msg = (
|
2022-02-03 19:24:19 +00:00
|
|
|
"Could not load fixtures.Article(pk=2): "
|
|
|
|
"A string literal cannot contain NUL (0x00) characters."
|
2017-12-30 17:16:11 +00:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "null_character_in_field_value.json")
|
2017-12-30 17:16:11 +00:00
|
|
|
|
2014-02-09 12:22:29 +00:00
|
|
|
def test_loaddata_app_option(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "No fixture named 'db_fixture_1' found."
|
|
|
|
):
|
|
|
|
management.call_command(
|
|
|
|
"loaddata", "db_fixture_1", verbosity=0, app_label="someotherapp"
|
|
|
|
)
|
2014-02-09 12:22:29 +00:00
|
|
|
self.assertQuerysetEqual(Article.objects.all(), [])
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command(
|
|
|
|
"loaddata", "db_fixture_1", verbosity=0, app_label="fixtures"
|
|
|
|
)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get().headline,
|
2022-02-03 19:24:19 +00:00
|
|
|
"Who needs more than one database?",
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2014-02-09 12:22:29 +00:00
|
|
|
|
2015-04-13 17:27:14 +00:00
|
|
|
def test_loaddata_verbosity_three(self):
|
2017-01-07 11:11:46 +00:00
|
|
|
output = StringIO()
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command(
|
|
|
|
"loaddata", "fixture1.json", verbosity=3, stdout=output, stderr=output
|
|
|
|
)
|
2017-01-24 11:22:42 +00:00
|
|
|
command_output = output.getvalue()
|
2015-04-13 17:27:14 +00:00
|
|
|
self.assertIn(
|
|
|
|
"\rProcessed 1 object(s).\rProcessed 2 object(s)."
|
|
|
|
"\rProcessed 3 object(s).\rProcessed 4 object(s).\n",
|
2022-02-03 19:24:19 +00:00
|
|
|
command_output,
|
2015-04-13 17:27:14 +00:00
|
|
|
)
|
|
|
|
|
2010-06-05 05:26:04 +00:00
|
|
|
def test_loading_using(self):
|
2022-02-04 07:08:27 +00:00
|
|
|
# Load fixtures 1 and 2. These will load using the 'default' database
|
|
|
|
# identifier explicitly.
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command(
|
|
|
|
"loaddata", "db_fixture_1", verbosity=0, database="default"
|
|
|
|
)
|
|
|
|
management.call_command(
|
|
|
|
"loaddata", "db_fixture_2", verbosity=0, database="default"
|
|
|
|
)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
2020-10-18 16:29:52 +00:00
|
|
|
[
|
2022-02-03 19:24:19 +00:00
|
|
|
"Who needs more than one database?",
|
|
|
|
"Who needs to use compressed data?",
|
2020-10-18 16:29:52 +00:00
|
|
|
],
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
def test_unmatched_identifier_loading(self):
|
2022-02-04 07:08:27 +00:00
|
|
|
# Db fixture 3 won't load because the database identifier doesn't
|
|
|
|
# match.
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "No fixture named 'db_fixture_3' found."
|
|
|
|
):
|
|
|
|
management.call_command("loaddata", "db_fixture_3", verbosity=0)
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "No fixture named 'db_fixture_3' found."
|
|
|
|
):
|
|
|
|
management.call_command(
|
|
|
|
"loaddata", "db_fixture_3", verbosity=0, database="default"
|
|
|
|
)
|
2011-11-27 13:00:56 +00:00
|
|
|
self.assertQuerysetEqual(Article.objects.all(), [])
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
def test_output_formats(self):
|
|
|
|
# Load back in fixture 1, we need the articles from it
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1", verbosity=0)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Try to load fixture 6 using format discovery
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture6", verbosity=0)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.all(),
|
|
|
|
[
|
|
|
|
'<Tag: <Article: Time to reform copyright> tagged "copyright">',
|
|
|
|
'<Tag: <Article: Time to reform copyright> tagged "law">',
|
|
|
|
],
|
|
|
|
transform=repr,
|
|
|
|
ordered=False,
|
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Dump the current contents of the database as a JSON fixture
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}, '
|
|
|
|
'{"pk": 1, "model": "fixtures.tag", "fields": '
|
|
|
|
'{"tagged_type": ["fixtures", "article"], "name": "copyright", '
|
|
|
|
'"tagged_id": 3}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.tag", "fields": '
|
|
|
|
'{"tagged_type": ["fixtures", "article"], "name": "law", "tagged_id": 3}}, '
|
|
|
|
'{"pk": 1, "model": "fixtures.person", "fields": '
|
|
|
|
'{"name": "Django Reinhardt"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.person", "fields": '
|
|
|
|
'{"name": "Stephane Grappelli"}}, '
|
2015-09-11 23:33:12 +00:00
|
|
|
'{"pk": 3, "model": "fixtures.person", "fields": {"name": "Prince"}}]',
|
2022-02-03 19:24:19 +00:00
|
|
|
natural_foreign_keys=True,
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
|
|
|
# Dump the current contents of the database as an XML fixture
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'<?xml version="1.0" encoding="utf-8"?><django-objects version="1.0">'
|
|
|
|
'<object pk="1" model="fixtures.category">'
|
|
|
|
'<field type="CharField" name="title">News Stories</field>'
|
|
|
|
'<field type="TextField" name="description">Latest news stories</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="2" model="fixtures.article">'
|
|
|
|
'<field type="CharField" name="headline">Poker has no place on ESPN</field>'
|
|
|
|
'<field type="DateTimeField" name="pub_date">2006-06-16T12:00:00</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="3" model="fixtures.article">'
|
|
|
|
'<field type="CharField" name="headline">Time to reform copyright</field>'
|
|
|
|
'<field type="DateTimeField" name="pub_date">2006-06-16T13:00:00</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="1" model="fixtures.tag">'
|
|
|
|
'<field type="CharField" name="name">copyright</field>'
|
|
|
|
'<field to="contenttypes.contenttype" name="tagged_type" '
|
|
|
|
'rel="ManyToOneRel"><natural>fixtures</natural>'
|
|
|
|
"<natural>article</natural></field>"
|
|
|
|
'<field type="PositiveIntegerField" name="tagged_id">3</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="2" model="fixtures.tag">'
|
|
|
|
'<field type="CharField" name="name">law</field>'
|
|
|
|
'<field to="contenttypes.contenttype" name="tagged_type" '
|
|
|
|
'rel="ManyToOneRel"><natural>fixtures</natural>'
|
|
|
|
"<natural>article</natural></field>"
|
|
|
|
'<field type="PositiveIntegerField" name="tagged_id">3</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="1" model="fixtures.person">'
|
|
|
|
'<field type="CharField" name="name">Django Reinhardt</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="2" model="fixtures.person">'
|
|
|
|
'<field type="CharField" name="name">Stephane Grappelli</field>'
|
|
|
|
"</object>"
|
|
|
|
'<object pk="3" model="fixtures.person">'
|
|
|
|
'<field type="CharField" name="name">Prince</field>'
|
|
|
|
"</object></django-objects>",
|
2022-02-03 19:24:19 +00:00
|
|
|
format="xml",
|
|
|
|
natural_foreign_keys=True,
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-06-05 05:26:04 +00:00
|
|
|
|
2016-05-17 06:52:01 +00:00
|
|
|
def test_loading_with_exclude_app(self):
|
|
|
|
Site.objects.all().delete()
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command(
|
|
|
|
"loaddata", "fixture1", exclude=["fixtures"], verbosity=0
|
|
|
|
)
|
2016-05-17 06:52:01 +00:00
|
|
|
self.assertFalse(Article.objects.exists())
|
|
|
|
self.assertFalse(Category.objects.exists())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(Site.objects.get().domain, "example.com")
|
2016-05-17 06:52:01 +00:00
|
|
|
|
|
|
|
def test_loading_with_exclude_model(self):
|
|
|
|
Site.objects.all().delete()
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command(
|
|
|
|
"loaddata", "fixture1", exclude=["fixtures.Article"], verbosity=0
|
|
|
|
)
|
2016-05-17 06:52:01 +00:00
|
|
|
self.assertFalse(Article.objects.exists())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(Category.objects.get().title, "News Stories")
|
|
|
|
self.assertEqual(Site.objects.get().domain, "example.com")
|
2016-05-17 06:52:01 +00:00
|
|
|
|
|
|
|
def test_exclude_option_errors(self):
|
|
|
|
"""Excluding a bogus app or model should raise an error."""
|
|
|
|
msg = "No installed app with label 'foo_app'."
|
|
|
|
with self.assertRaisesMessage(management.CommandError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command(
|
|
|
|
"loaddata", "fixture1", exclude=["foo_app"], verbosity=0
|
|
|
|
)
|
2016-05-17 06:52:01 +00:00
|
|
|
|
|
|
|
msg = "Unknown model: fixtures.FooModel"
|
|
|
|
with self.assertRaisesMessage(management.CommandError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command(
|
|
|
|
"loaddata", "fixture1", exclude=["fixtures.FooModel"], verbosity=0
|
|
|
|
)
|
2016-05-17 06:52:01 +00:00
|
|
|
|
2017-03-26 19:29:05 +00:00
|
|
|
def test_stdin_without_format(self):
|
|
|
|
"""Reading from stdin raises an error if format isn't specified."""
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "--format must be specified when reading from stdin."
|
2017-03-26 19:29:05 +00:00
|
|
|
with self.assertRaisesMessage(management.CommandError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "-", verbosity=0)
|
2017-03-26 19:29:05 +00:00
|
|
|
|
|
|
|
def test_loading_stdin(self):
|
|
|
|
"""Loading fixtures from stdin with json and xml."""
|
|
|
|
tests_dir = os.path.dirname(__file__)
|
2022-02-03 19:24:19 +00:00
|
|
|
fixture_json = os.path.join(tests_dir, "fixtures", "fixture1.json")
|
|
|
|
fixture_xml = os.path.join(tests_dir, "fixtures", "fixture3.xml")
|
2017-03-26 19:29:05 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
with mock.patch(
|
|
|
|
"django.core.management.commands.loaddata.sys.stdin", open(fixture_json)
|
|
|
|
):
|
|
|
|
management.call_command("loaddata", "--format=json", "-", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
|
|
|
["Time to reform copyright", "Poker has no place on ESPN"],
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2017-03-26 19:29:05 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
with mock.patch(
|
|
|
|
"django.core.management.commands.loaddata.sys.stdin", open(fixture_xml)
|
|
|
|
):
|
|
|
|
management.call_command("loaddata", "--format=xml", "-", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
2020-10-18 16:29:52 +00:00
|
|
|
[
|
2022-02-03 19:24:19 +00:00
|
|
|
"XML identified as leading cause of cancer",
|
|
|
|
"Time to reform copyright",
|
|
|
|
"Poker on TV is great!",
|
2020-10-18 16:29:52 +00:00
|
|
|
],
|
|
|
|
)
|
2017-03-26 19:29:05 +00:00
|
|
|
|
2012-09-07 17:17:09 +00:00
|
|
|
|
2017-02-03 01:43:21 +00:00
|
|
|
class NonexistentFixtureTests(TestCase):
|
2014-10-14 21:15:43 +00:00
|
|
|
"""
|
|
|
|
Custom class to limit fixture dirs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_loaddata_not_existent_fixture_file(self):
|
2017-01-07 11:11:46 +00:00
|
|
|
stdout_output = StringIO()
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "No fixture named 'this_fixture_doesnt_exist' found."
|
|
|
|
):
|
|
|
|
management.call_command(
|
|
|
|
"loaddata", "this_fixture_doesnt_exist", stdout=stdout_output
|
|
|
|
)
|
2014-10-14 21:15:43 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@mock.patch("django.db.connection.enable_constraint_checking")
|
|
|
|
@mock.patch("django.db.connection.disable_constraint_checking")
|
2016-04-08 02:04:45 +00:00
|
|
|
def test_nonexistent_fixture_no_constraint_checking(
|
2022-02-03 19:24:19 +00:00
|
|
|
self, disable_constraint_checking, enable_constraint_checking
|
|
|
|
):
|
2015-12-29 16:29:11 +00:00
|
|
|
"""
|
|
|
|
If no fixtures match the loaddata command, constraints checks on the
|
|
|
|
database shouldn't be disabled. This is performance critical on MSSQL.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "No fixture named 'this_fixture_doesnt_exist' found."
|
|
|
|
):
|
|
|
|
management.call_command(
|
|
|
|
"loaddata", "this_fixture_doesnt_exist", verbosity=0
|
|
|
|
)
|
2015-12-29 16:29:11 +00:00
|
|
|
disable_constraint_checking.assert_not_called()
|
|
|
|
enable_constraint_checking.assert_not_called()
|
|
|
|
|
2014-10-14 21:15:43 +00:00
|
|
|
|
2012-09-28 07:20:01 +00:00
|
|
|
class FixtureTransactionTests(DumpDataAssertMixin, TransactionTestCase):
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2013-06-04 06:09:29 +00:00
|
|
|
available_apps = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"fixtures",
|
|
|
|
"django.contrib.sites",
|
2013-06-04 06:09:29 +00:00
|
|
|
]
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@skipUnlessDBFeature("supports_forward_references")
|
2010-10-11 12:55:17 +00:00
|
|
|
def test_format_discovery(self):
|
|
|
|
# Load fixture 1 again, using format discovery
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture1", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
|
|
|
["Time to reform copyright", "Poker has no place on ESPN"],
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
|
|
|
# Try to load fixture 2 using format discovery; this will fail
|
|
|
|
# because there are two fixture2's in the fixtures directory
|
2020-02-04 20:58:07 +00:00
|
|
|
msg = "Multiple fixtures named 'fixture2'"
|
|
|
|
with self.assertRaisesMessage(management.CommandError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture2", verbosity=0)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
|
|
|
# object list is unaffected
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
|
|
|
["Time to reform copyright", "Poker has no place on ESPN"],
|
2020-10-18 16:29:52 +00:00
|
|
|
)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
|
|
|
# Dump the current contents of the database as a JSON fixture
|
2015-09-11 23:33:12 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2022-02-04 07:08:27 +00:00
|
|
|
'[{"pk": 1, "model": "fixtures.category", "fields": '
|
|
|
|
'{"description": "Latest news stories", "title": "News Stories"}}, '
|
|
|
|
'{"pk": 2, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Poker has no place on ESPN", '
|
|
|
|
'"pub_date": "2006-06-16T12:00:00"}}, '
|
|
|
|
'{"pk": 3, "model": "fixtures.article", "fields": '
|
|
|
|
'{"headline": "Time to reform copyright", '
|
|
|
|
'"pub_date": "2006-06-16T13:00:00"}}]',
|
2015-09-11 23:33:12 +00:00
|
|
|
)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
|
|
|
# Load fixture 4 (compressed), using format discovery
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "fixture4", verbosity=0)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.values_list("headline", flat=True),
|
2020-10-18 16:29:52 +00:00
|
|
|
[
|
2022-02-03 19:24:19 +00:00
|
|
|
"Django pets kitten",
|
|
|
|
"Time to reform copyright",
|
|
|
|
"Poker has no place on ESPN",
|
2020-10-18 16:29:52 +00:00
|
|
|
],
|
|
|
|
)
|
2018-07-13 21:54:47 +00:00
|
|
|
|
|
|
|
|
2020-04-07 09:58:21 +00:00
|
|
|
class ForwardReferenceTests(DumpDataAssertMixin, TestCase):
|
2020-04-07 10:14:45 +00:00
|
|
|
def test_forward_reference_fk(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "forward_reference_fk.json", verbosity=0)
|
2020-04-07 10:14:45 +00:00
|
|
|
t1, t2 = NaturalKeyThing.objects.all()
|
|
|
|
self.assertEqual(t1.other_thing, t2)
|
|
|
|
self.assertEqual(t2.other_thing, t1)
|
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2020-04-07 10:14:45 +00:00
|
|
|
'[{"model": "fixtures.naturalkeything", "pk": 1, '
|
|
|
|
'"fields": {"key": "t1", "other_thing": 2, "other_things": []}}, '
|
|
|
|
'{"model": "fixtures.naturalkeything", "pk": 2, '
|
|
|
|
'"fields": {"key": "t2", "other_thing": 1, "other_things": []}}]',
|
|
|
|
)
|
|
|
|
|
2020-04-07 09:58:21 +00:00
|
|
|
def test_forward_reference_fk_natural_key(self):
|
|
|
|
management.call_command(
|
2022-02-03 19:24:19 +00:00
|
|
|
"loaddata",
|
|
|
|
"forward_reference_fk_natural_key.json",
|
2020-04-07 09:58:21 +00:00
|
|
|
verbosity=0,
|
|
|
|
)
|
2018-07-13 21:54:47 +00:00
|
|
|
t1, t2 = NaturalKeyThing.objects.all()
|
|
|
|
self.assertEqual(t1.other_thing, t2)
|
|
|
|
self.assertEqual(t2.other_thing, t1)
|
2020-04-07 09:58:21 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2020-04-07 09:58:21 +00:00
|
|
|
'[{"model": "fixtures.naturalkeything", '
|
|
|
|
'"fields": {"key": "t1", "other_thing": ["t2"], "other_things": []}}, '
|
|
|
|
'{"model": "fixtures.naturalkeything", '
|
|
|
|
'"fields": {"key": "t2", "other_thing": ["t1"], "other_things": []}}]',
|
|
|
|
natural_primary_keys=True,
|
|
|
|
natural_foreign_keys=True,
|
|
|
|
)
|
2018-07-13 21:54:47 +00:00
|
|
|
|
2020-04-07 10:14:45 +00:00
|
|
|
def test_forward_reference_m2m(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "forward_reference_m2m.json", verbosity=0)
|
2020-04-07 10:14:45 +00:00
|
|
|
self.assertEqual(NaturalKeyThing.objects.count(), 3)
|
2022-02-03 19:24:19 +00:00
|
|
|
t1 = NaturalKeyThing.objects.get_by_natural_key("t1")
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
t1.other_things.order_by("key").values_list("key", flat=True),
|
|
|
|
["t2", "t3"],
|
2020-04-07 10:14:45 +00:00
|
|
|
)
|
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2020-04-07 10:14:45 +00:00
|
|
|
'[{"model": "fixtures.naturalkeything", "pk": 1, '
|
|
|
|
'"fields": {"key": "t1", "other_thing": null, "other_things": [2, 3]}}, '
|
|
|
|
'{"model": "fixtures.naturalkeything", "pk": 2, '
|
|
|
|
'"fields": {"key": "t2", "other_thing": null, "other_things": []}}, '
|
|
|
|
'{"model": "fixtures.naturalkeything", "pk": 3, '
|
|
|
|
'"fields": {"key": "t3", "other_thing": null, "other_things": []}}]',
|
|
|
|
)
|
|
|
|
|
2020-04-07 09:58:21 +00:00
|
|
|
def test_forward_reference_m2m_natural_key(self):
|
|
|
|
management.call_command(
|
2022-02-03 19:24:19 +00:00
|
|
|
"loaddata",
|
|
|
|
"forward_reference_m2m_natural_key.json",
|
2020-04-07 09:58:21 +00:00
|
|
|
verbosity=0,
|
|
|
|
)
|
2018-07-13 21:54:47 +00:00
|
|
|
self.assertEqual(NaturalKeyThing.objects.count(), 3)
|
2022-02-03 19:24:19 +00:00
|
|
|
t1 = NaturalKeyThing.objects.get_by_natural_key("t1")
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
t1.other_things.order_by("key").values_list("key", flat=True),
|
|
|
|
["t2", "t3"],
|
2018-07-13 21:54:47 +00:00
|
|
|
)
|
2020-04-07 09:58:21 +00:00
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2020-04-07 09:58:21 +00:00
|
|
|
'[{"model": "fixtures.naturalkeything", '
|
2022-02-04 07:08:27 +00:00
|
|
|
'"fields": {"key": "t1", "other_thing": null, '
|
|
|
|
'"other_things": [["t2"], ["t3"]]}}, '
|
2020-04-07 09:58:21 +00:00
|
|
|
'{"model": "fixtures.naturalkeything", '
|
|
|
|
'"fields": {"key": "t2", "other_thing": null, "other_things": []}}, '
|
|
|
|
'{"model": "fixtures.naturalkeything", '
|
|
|
|
'"fields": {"key": "t3", "other_thing": null, "other_things": []}}]',
|
|
|
|
natural_primary_keys=True,
|
|
|
|
natural_foreign_keys=True,
|
|
|
|
)
|
2020-04-07 09:28:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CircularReferenceTests(DumpDataAssertMixin, TestCase):
|
|
|
|
def test_circular_reference(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "circular_reference.json", verbosity=0)
|
2020-04-07 09:28:25 +00:00
|
|
|
obj_a = CircularA.objects.get()
|
|
|
|
obj_b = CircularB.objects.get()
|
|
|
|
self.assertEqual(obj_a.obj, obj_b)
|
|
|
|
self.assertEqual(obj_b.obj, obj_a)
|
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2020-04-07 09:28:25 +00:00
|
|
|
'[{"model": "fixtures.circulara", "pk": 1, '
|
|
|
|
'"fields": {"key": "x", "obj": 1}}, '
|
|
|
|
'{"model": "fixtures.circularb", "pk": 1, '
|
|
|
|
'"fields": {"key": "y", "obj": 1}}]',
|
|
|
|
)
|
2019-12-01 23:50:59 +00:00
|
|
|
|
|
|
|
def test_circular_reference_natural_key(self):
|
|
|
|
management.call_command(
|
2022-02-03 19:24:19 +00:00
|
|
|
"loaddata",
|
|
|
|
"circular_reference_natural_key.json",
|
2019-12-01 23:50:59 +00:00
|
|
|
verbosity=0,
|
|
|
|
)
|
|
|
|
obj_a = CircularA.objects.get()
|
|
|
|
obj_b = CircularB.objects.get()
|
|
|
|
self.assertEqual(obj_a.obj, obj_b)
|
|
|
|
self.assertEqual(obj_b.obj, obj_a)
|
|
|
|
self._dumpdata_assert(
|
2022-02-03 19:24:19 +00:00
|
|
|
["fixtures"],
|
2019-12-01 23:50:59 +00:00
|
|
|
'[{"model": "fixtures.circulara", '
|
|
|
|
'"fields": {"key": "x", "obj": ["y"]}}, '
|
|
|
|
'{"model": "fixtures.circularb", '
|
|
|
|
'"fields": {"key": "y", "obj": ["x"]}}]',
|
|
|
|
natural_primary_keys=True,
|
|
|
|
natural_foreign_keys=True,
|
|
|
|
)
|