2010-08-23 07:04:41 +00:00
|
|
|
from django.core import management
|
2016-01-02 19:11:18 +00:00
|
|
|
from django.core.management import CommandError
|
2014-12-26 18:23:38 +00:00
|
|
|
from django.test import TestCase
|
2010-08-23 07:04:41 +00:00
|
|
|
|
2014-12-26 18:23:38 +00:00
|
|
|
from .models import Article
|
2010-08-23 07:04:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SampleTestCase(TestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
fixtures = ["model_package_fixture1.json", "model_package_fixture2.json"]
|
2010-08-23 07:04:41 +00:00
|
|
|
|
2019-04-14 13:00:48 +00:00
|
|
|
def test_class_fixtures(self):
|
2010-08-23 07:04:41 +00:00
|
|
|
"Test cases can load fixture objects into models defined in packages"
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.all(),
|
|
|
|
[
|
2012-06-07 16:08:47 +00:00
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker has no place on ESPN",
|
2010-08-23 07:04:41 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.headline,
|
2010-08-23 07:04:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "model_package_fixture1.json", verbosity=0)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.all(),
|
|
|
|
[
|
2012-06-07 16:08:47 +00:00
|
|
|
"Time to reform copyright",
|
|
|
|
"Poker has no place on ESPN",
|
2010-08-23 07:04:41 +00:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Load fixture 2. JSON file imported by default. Overwrites some
|
|
|
|
# existing objects
|
2022-02-03 19:24:19 +00:00
|
|
|
management.call_command("loaddata", "model_package_fixture2.json", verbosity=0)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.all(),
|
|
|
|
[
|
2012-06-07 16:08:47 +00:00
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker has no place on ESPN",
|
2010-08-23 07:04:41 +00:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Load a fixture that doesn't exist
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "No fixture named 'unknown' found."
|
|
|
|
):
|
2013-06-30 12:17:33 +00:00
|
|
|
management.call_command("loaddata", "unknown.json", verbosity=0)
|
2013-05-19 09:20:10 +00:00
|
|
|
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Article.objects.all(),
|
|
|
|
[
|
2012-06-07 16:08:47 +00:00
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker has no place on ESPN",
|
2010-08-23 07:04:41 +00:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|