2012-11-04 19:16:06 +01:00
|
|
|
from django.core.management.color import no_style
|
|
|
|
from django.core.management.sql import custom_sql_for_model
|
|
|
|
from django.db import connections, DEFAULT_DB_ALIAS
|
2013-12-23 16:01:13 +01:00
|
|
|
from django.test import TestCase, override_settings
|
2010-10-12 03:59:28 +02:00
|
|
|
|
2011-10-13 23:34:56 +02:00
|
|
|
from .models import Simple
|
2010-10-12 03:59:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
class InitialSQLTests(TestCase):
|
2013-03-26 23:18:03 +01:00
|
|
|
"""
|
|
|
|
The format of the included SQL file for this test suite is important.
|
|
|
|
It must end with a trailing newline in order to test the fix for #2161.
|
|
|
|
"""
|
2011-01-18 17:43:01 +01:00
|
|
|
|
2012-07-21 14:24:29 +02:00
|
|
|
def test_initial_sql(self):
|
2013-03-26 23:18:03 +01:00
|
|
|
"""
|
|
|
|
As pointed out by #14661, test data loaded by custom SQL
|
|
|
|
can't be relied upon; as a result, the test framework flushes the
|
|
|
|
data contents before every test. This test validates that this has
|
|
|
|
occurred.
|
|
|
|
"""
|
2011-01-18 17:43:01 +01:00
|
|
|
self.assertEqual(Simple.objects.count(), 0)
|
2012-07-21 14:24:29 +02:00
|
|
|
|
|
|
|
def test_custom_sql(self):
|
2013-03-26 23:18:03 +01:00
|
|
|
"""
|
2013-09-03 17:51:34 +02:00
|
|
|
Simulate the custom SQL loading by migrate.
|
2013-03-26 23:18:03 +01:00
|
|
|
"""
|
2012-07-21 14:24:29 +02:00
|
|
|
connection = connections[DEFAULT_DB_ALIAS]
|
|
|
|
custom_sql = custom_sql_for_model(Simple, no_style(), connection)
|
2013-03-26 23:18:03 +01:00
|
|
|
self.assertEqual(len(custom_sql), 9)
|
2012-07-21 14:24:29 +02:00
|
|
|
cursor = connection.cursor()
|
|
|
|
for sql in custom_sql:
|
|
|
|
cursor.execute(sql)
|
2013-03-26 23:18:03 +01:00
|
|
|
self.assertEqual(Simple.objects.count(), 9)
|
|
|
|
self.assertEqual(
|
|
|
|
Simple.objects.get(name__contains='placeholders').name,
|
|
|
|
'"100%" of % are not placeholders'
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(DEBUG=True)
|
|
|
|
def test_custom_sql_debug(self):
|
|
|
|
"""
|
|
|
|
Same test, ensure that CursorDebugWrapper doesn't alter sql loading
|
|
|
|
(#3485).
|
|
|
|
"""
|
|
|
|
self.test_custom_sql()
|