1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00
django/tests/fixtures_model_package/tests.py
François Freitag 2e48cf6bd9 Fixed fixtures loading isolation in tests.
fixture1.json and fixture2.json exist in both "fixtures" and
"fixtures_model_package". Both apps are listed in "INSTALLED_APPS". The
loaddata management command loads from installed apps, thus loads both
fixtures when a test runs loaddata with any of these fixtures.
2020-05-15 09:14:05 +02:00

61 lines
2.0 KiB
Python

from django.core import management
from django.core.management import CommandError
from django.test import TestCase
from .models import Article
class SampleTestCase(TestCase):
fixtures = ['model_package_fixture1.json', 'model_package_fixture2.json']
def test_class_fixtures(self):
"Test cases can load fixture objects into models defined in packages"
self.assertQuerysetEqual(
Article.objects.all(), [
"Django conquers world!",
"Copyright is fine the way it is",
"Poker has no place on ESPN",
],
lambda a: a.headline
)
class FixtureTestCase(TestCase):
def test_loaddata(self):
"Fixtures can load data into models defined in packages"
# Load fixture 1. Single JSON file, with two objects
management.call_command('loaddata', 'model_package_fixture1.json', verbosity=0)
self.assertQuerysetEqual(
Article.objects.all(), [
"Time to reform copyright",
"Poker has no place on ESPN",
],
lambda a: a.headline,
)
# Load fixture 2. JSON file imported by default. Overwrites some
# existing objects
management.call_command('loaddata', 'model_package_fixture2.json', verbosity=0)
self.assertQuerysetEqual(
Article.objects.all(), [
"Django conquers world!",
"Copyright is fine the way it is",
"Poker has no place on ESPN",
],
lambda a: a.headline,
)
# Load a fixture that doesn't exist
with self.assertRaisesMessage(CommandError, "No fixture named 'unknown' found."):
management.call_command("loaddata", "unknown.json", verbosity=0)
self.assertQuerysetEqual(
Article.objects.all(), [
"Django conquers world!",
"Copyright is fine the way it is",
"Poker has no place on ESPN",
],
lambda a: a.headline,
)