2013-02-09 23:30:33 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-08-29 02:12:49 +08:00
|
|
|
import re
|
2014-09-04 23:32:51 +08:00
|
|
|
import unittest
|
2014-08-29 02:12:49 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2013-02-09 23:30:33 +08:00
|
|
|
from django.core.management.color import no_style
|
2015-02-10 02:19:34 +08:00
|
|
|
from django.core.management.sql import (
|
|
|
|
sql_all, sql_create, sql_delete, sql_destroy_indexes, sql_indexes,
|
|
|
|
)
|
|
|
|
from django.db import DEFAULT_DB_ALIAS, connections
|
2014-12-22 04:19:05 +08:00
|
|
|
from django.test import TestCase, ignore_warnings, override_settings
|
2013-02-09 23:30:33 +08:00
|
|
|
from django.utils import six
|
2014-09-15 02:26:16 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2013-02-09 23:30:33 +08:00
|
|
|
|
2015-02-10 02:19:34 +08:00
|
|
|
|
2013-02-26 20:19:18 +08:00
|
|
|
# See also initial_sql_regress for 'custom_sql_for_model' tests
|
2013-02-09 23:30:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SQLCommandsTestCase(TestCase):
|
|
|
|
"""Tests for several functions in django/core/management/sql.py"""
|
2013-05-29 21:47:21 +08:00
|
|
|
def count_ddl(self, output, cmd):
|
|
|
|
return len([o for o in output if o.startswith(cmd)])
|
|
|
|
|
2013-02-09 23:30:33 +08:00
|
|
|
def test_sql_create(self):
|
2013-12-30 04:11:12 +08:00
|
|
|
app_config = apps.get_app_config('commands_sql')
|
|
|
|
output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
2014-08-29 02:12:49 +08:00
|
|
|
|
|
|
|
tables = set()
|
|
|
|
create_table_re = re.compile(r'^create table .(?P<table>[\w_]+).*', re.IGNORECASE)
|
|
|
|
reference_re = re.compile(r'.* references .(?P<table>[\w_]+).*', re.IGNORECASE)
|
|
|
|
for statement in output:
|
|
|
|
create_table = create_table_re.match(statement)
|
|
|
|
if create_table:
|
2014-08-29 07:33:36 +08:00
|
|
|
# Lower since Oracle's table names are upper cased.
|
|
|
|
tables.add(create_table.group('table').lower())
|
2014-08-29 02:12:49 +08:00
|
|
|
continue
|
|
|
|
reference = reference_re.match(statement)
|
|
|
|
if reference:
|
2014-08-29 07:33:36 +08:00
|
|
|
# Lower since Oracle's table names are upper cased.
|
|
|
|
table = reference.group('table').lower()
|
2014-08-29 02:12:49 +08:00
|
|
|
self.assertIn(
|
|
|
|
table, tables, "The table %s is referenced before its creation." % table
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(tables, {
|
|
|
|
'commands_sql_comment', 'commands_sql_book', 'commands_sql_book_comments'
|
|
|
|
})
|
2013-02-09 23:30:33 +08:00
|
|
|
|
2014-12-30 04:14:40 +08:00
|
|
|
@unittest.skipUnless('PositiveIntegerField' in connections[DEFAULT_DB_ALIAS].data_type_check_constraints, 'Backend does not have checks.')
|
2014-09-04 23:32:51 +08:00
|
|
|
def test_sql_create_check(self):
|
|
|
|
"""Regression test for #23416 -- Check that db_params['check'] is respected."""
|
|
|
|
app_config = apps.get_app_config('commands_sql')
|
|
|
|
output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
|
|
|
success = False
|
|
|
|
for statement in output:
|
|
|
|
if 'CHECK' in statement:
|
|
|
|
success = True
|
|
|
|
if not success:
|
2014-11-04 06:48:03 +08:00
|
|
|
self.fail("'CHECK' not found in output %s" % output)
|
2014-09-04 23:32:51 +08:00
|
|
|
|
2013-02-09 23:30:33 +08:00
|
|
|
def test_sql_delete(self):
|
2013-12-30 04:11:12 +08:00
|
|
|
app_config = apps.get_app_config('commands_sql')
|
2014-06-09 09:57:55 +08:00
|
|
|
output = sql_delete(app_config, no_style(), connections[DEFAULT_DB_ALIAS], close_connection=False)
|
2013-05-29 21:47:21 +08:00
|
|
|
drop_tables = [o for o in output if o.startswith('DROP TABLE')]
|
|
|
|
self.assertEqual(len(drop_tables), 3)
|
|
|
|
# Lower so that Oracle's upper case tbl names wont break
|
|
|
|
sql = drop_tables[-1].lower()
|
|
|
|
six.assertRegex(self, sql, r'^drop table .commands_sql_comment.*')
|
2013-02-09 23:30:33 +08:00
|
|
|
|
2014-12-22 04:19:05 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2013-02-09 23:30:33 +08:00
|
|
|
def test_sql_indexes(self):
|
2013-12-30 04:11:12 +08:00
|
|
|
app_config = apps.get_app_config('commands_sql')
|
2014-12-22 04:19:05 +08:00
|
|
|
output = sql_indexes(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
2014-12-24 22:55:57 +08:00
|
|
|
# Number of indexes is backend-dependent
|
|
|
|
self.assertTrue(1 <= self.count_ddl(output, 'CREATE INDEX') <= 4)
|
2013-02-09 23:30:33 +08:00
|
|
|
|
2013-02-24 00:07:50 +08:00
|
|
|
def test_sql_destroy_indexes(self):
|
2013-12-30 04:11:12 +08:00
|
|
|
app_config = apps.get_app_config('commands_sql')
|
|
|
|
output = sql_destroy_indexes(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
2014-12-24 22:55:57 +08:00
|
|
|
# Number of indexes is backend-dependent
|
|
|
|
self.assertTrue(1 <= self.count_ddl(output, 'DROP INDEX') <= 4)
|
2013-02-24 00:07:50 +08:00
|
|
|
|
2014-12-22 04:19:05 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2013-02-09 23:30:33 +08:00
|
|
|
def test_sql_all(self):
|
2013-12-30 04:11:12 +08:00
|
|
|
app_config = apps.get_app_config('commands_sql')
|
2014-12-22 04:19:05 +08:00
|
|
|
output = sql_all(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
2013-05-29 21:47:21 +08:00
|
|
|
|
|
|
|
self.assertEqual(self.count_ddl(output, 'CREATE TABLE'), 3)
|
2014-12-24 22:55:57 +08:00
|
|
|
# Number of indexes is backend-dependent
|
|
|
|
self.assertTrue(1 <= self.count_ddl(output, 'CREATE INDEX') <= 4)
|
2013-10-16 23:58:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestRouter(object):
|
|
|
|
def allow_migrate(self, db, model):
|
|
|
|
return False
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2014-11-30 01:09:40 +08:00
|
|
|
@override_settings(DATABASE_ROUTERS=[TestRouter()])
|
2013-10-16 23:58:05 +08:00
|
|
|
class SQLCommandsRouterTestCase(TestCase):
|
|
|
|
|
2015-02-21 02:24:29 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2013-10-16 23:58:05 +08:00
|
|
|
def test_router_honored(self):
|
2013-12-30 04:11:12 +08:00
|
|
|
app_config = apps.get_app_config('commands_sql')
|
2013-10-16 23:58:05 +08:00
|
|
|
for sql_command in (sql_all, sql_create, sql_delete, sql_indexes, sql_destroy_indexes):
|
2014-06-09 09:57:55 +08:00
|
|
|
if sql_command is sql_delete:
|
|
|
|
output = sql_command(app_config, no_style(), connections[DEFAULT_DB_ALIAS], close_connection=False)
|
2014-08-05 08:30:00 +08:00
|
|
|
# "App creates no tables in the database. Nothing to do."
|
|
|
|
expected_output = 1
|
2014-06-09 09:57:55 +08:00
|
|
|
else:
|
|
|
|
output = sql_command(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
|
2014-08-05 08:30:00 +08:00
|
|
|
expected_output = 0
|
|
|
|
self.assertEqual(len(output), expected_output,
|
2013-10-16 23:58:05 +08:00
|
|
|
"%s command is not honoring routers" % sql_command.__name__)
|