mirror of
https://github.com/django/django.git
synced 2024-12-28 03:55:50 +00:00
8b5b199e20
Avoided introducing a new regex-based SQL splitter in the migrations framework, before we're bound by backwards compatibility. Adapted this change to the legacy "initial SQL data" feature, even though it's already deprecated, in order to facilitate the transition to migrations. sqlparse becomes mandatory for RunSQL on some databases (all but PostgreSQL). There's no API to provide a single statement and tell Django not to attempt splitting. Since we have a more robust splitting implementation, that seems like a good tradeoff. It's easier to add a new keyword argument later if necessary than to remove one. Many people contributed to both tickets, thank you all, and especially Claude for the review. Refs #22401.
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
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
|
|
from django.test import TestCase, override_settings
|
|
|
|
from .models import Simple
|
|
|
|
|
|
class InitialSQLTests(TestCase):
|
|
"""
|
|
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.
|
|
"""
|
|
|
|
def test_initial_sql(self):
|
|
"""
|
|
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.
|
|
"""
|
|
self.assertEqual(Simple.objects.count(), 0)
|
|
|
|
def test_custom_sql(self):
|
|
"""
|
|
Simulate the custom SQL loading by migrate.
|
|
"""
|
|
connection = connections[DEFAULT_DB_ALIAS]
|
|
custom_sql = custom_sql_for_model(Simple, no_style(), connection)
|
|
with connection.cursor() as cursor:
|
|
for sql in custom_sql:
|
|
cursor.execute(sql)
|
|
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()
|