2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-05-19 18:49:03 +08:00
|
|
|
import warnings
|
|
|
|
|
2010-08-23 15:04:41 +08:00
|
|
|
from django.core import management
|
2012-06-06 21:45:28 +08:00
|
|
|
from django.db import transaction
|
|
|
|
from django.test import TestCase, TransactionTestCase
|
2013-07-16 21:10:04 +08:00
|
|
|
from django.utils.six import StringIO
|
2010-08-23 15:04:41 +08:00
|
|
|
|
2011-11-27 21:00:56 +08:00
|
|
|
from .models import Article, Book
|
2010-08-23 15:04:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SampleTestCase(TestCase):
|
|
|
|
fixtures = ['fixture1.json', 'fixture2.json']
|
|
|
|
|
|
|
|
def testClassFixtures(self):
|
|
|
|
"Test cases can load fixture objects into models defined in packages"
|
2011-11-27 21:00:56 +08:00
|
|
|
self.assertEqual(Article.objects.count(), 3)
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
2013-10-27 03:15:03 +08:00
|
|
|
Article.objects.all(), [
|
2012-06-08 00:08:47 +08:00
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker has no place on ESPN",
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-06-06 21:45:28 +08:00
|
|
|
class TestNoInitialDataLoading(TransactionTestCase):
|
2013-06-04 14:09:29 +08:00
|
|
|
|
|
|
|
available_apps = ['fixtures_model_package']
|
|
|
|
|
2013-09-03 23:51:34 +08:00
|
|
|
def test_migrate(self):
|
2013-07-10 03:12:51 +08:00
|
|
|
with transaction.atomic():
|
2012-06-06 21:45:28 +08:00
|
|
|
Book.objects.all().delete()
|
2012-06-05 22:46:15 +08:00
|
|
|
|
2012-06-06 21:45:28 +08:00
|
|
|
management.call_command(
|
2013-09-03 23:51:34 +08:00
|
|
|
'migrate',
|
2012-06-06 21:45:28 +08:00
|
|
|
verbosity=0,
|
|
|
|
load_initial_data=False
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Book.objects.all(), [])
|
2013-03-05 06:26:31 +08:00
|
|
|
|
2012-06-05 22:46:15 +08:00
|
|
|
def test_flush(self):
|
2012-06-06 19:53:12 +08:00
|
|
|
# Test presence of fixture (flush called by TransactionTestCase)
|
2012-06-05 22:46:15 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Book.objects.all(), [
|
2012-06-19 23:37:28 +08:00
|
|
|
'Achieving self-awareness of Python programs'
|
2012-06-05 22:46:15 +08:00
|
|
|
],
|
|
|
|
lambda a: a.name
|
|
|
|
)
|
|
|
|
|
2013-07-10 03:12:51 +08:00
|
|
|
with transaction.atomic():
|
2012-06-06 21:45:28 +08:00
|
|
|
management.call_command(
|
|
|
|
'flush',
|
|
|
|
verbosity=0,
|
|
|
|
interactive=False,
|
|
|
|
load_initial_data=False
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Book.objects.all(), [])
|
2012-06-05 22:46:15 +08:00
|
|
|
|
|
|
|
|
2010-08-23 15:04:41 +08:00
|
|
|
class FixtureTestCase(TestCase):
|
|
|
|
def test_initial_data(self):
|
|
|
|
"Fixtures can load initial data into models defined in packages"
|
2013-09-03 23:51:34 +08:00
|
|
|
# migrate introduces 1 initial data object from initial_data.json
|
2014-07-27 01:15:54 +08:00
|
|
|
# this behavior is deprecated and will be removed in Django 1.9
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
2011-11-27 21:00:56 +08:00
|
|
|
Book.objects.all(), [
|
2012-06-08 00:08:47 +08:00
|
|
|
'Achieving self-awareness of Python programs'
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
2011-11-27 21:00:56 +08:00
|
|
|
lambda a: a.name
|
2010-08-23 15:04:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_loaddata(self):
|
|
|
|
"Fixtures can load data into models defined in packages"
|
|
|
|
# Load fixture 1. Single JSON file, with two objects
|
2013-06-30 20:17:33 +08:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0)
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.all(), [
|
2012-06-08 00:08:47 +08:00
|
|
|
"Time to reform copyright",
|
|
|
|
"Poker has no place on ESPN",
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Load fixture 2. JSON file imported by default. Overwrites some
|
|
|
|
# existing objects
|
2013-06-30 20:17:33 +08:00
|
|
|
management.call_command("loaddata", "fixture2.json", verbosity=0)
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.all(), [
|
2012-06-08 00:08:47 +08:00
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker has no place on ESPN",
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Load a fixture that doesn't exist
|
2013-05-20 01:51:59 +08:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
2013-06-30 20:17:33 +08:00
|
|
|
management.call_command("loaddata", "unknown.json", verbosity=0)
|
2013-05-20 01:51:59 +08:00
|
|
|
self.assertEqual(len(w), 1)
|
|
|
|
self.assertTrue(w[0].message, "No fixture named 'unknown' found.")
|
2013-05-19 17:20:10 +08:00
|
|
|
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.all(), [
|
2012-06-08 00:08:47 +08:00
|
|
|
"Django conquers world!",
|
|
|
|
"Copyright is fine the way it is",
|
|
|
|
"Poker has no place on ESPN",
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|
2013-07-16 21:10:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class InitialSQLTests(TestCase):
|
|
|
|
|
|
|
|
def test_custom_sql(self):
|
|
|
|
"""
|
|
|
|
#14300 -- Verify that custom_sql_for_model searches `app/sql` and not
|
|
|
|
`app/models/sql` (the old location will work until Django 1.9)
|
|
|
|
"""
|
|
|
|
out = StringIO()
|
|
|
|
management.call_command("sqlcustom", "fixtures_model_package", stdout=out)
|
|
|
|
output = out.getvalue()
|
|
|
|
self.assertTrue("INSERT INTO fixtures_model_package_book (name) "
|
|
|
|
"VALUES ('My Book')" in output)
|
|
|
|
# value from deprecated search path models/sql (remove in Django 1.9)
|
|
|
|
self.assertTrue("Deprecated Book" in output)
|