2013-02-09 15:30:33 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-12-24 11:25:17 +00:00
|
|
|
from django.apps import apps
|
2013-02-09 15:30:33 +00:00
|
|
|
from django.core.management.color import no_style
|
|
|
|
from django.core.management.sql import (sql_create, sql_delete, sql_indexes,
|
2013-02-23 16:07:50 +00:00
|
|
|
sql_destroy_indexes, sql_all)
|
2013-12-11 22:31:34 +00:00
|
|
|
from django.db import connections, DEFAULT_DB_ALIAS, router
|
2013-02-09 15:30:33 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.utils import six
|
|
|
|
|
2013-02-26 12:19:18 +00:00
|
|
|
# See also initial_sql_regress for 'custom_sql_for_model' tests
|
2013-02-09 15:30:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SQLCommandsTestCase(TestCase):
|
|
|
|
"""Tests for several functions in django/core/management/sql.py"""
|
2013-05-29 13:47:21 +00:00
|
|
|
def count_ddl(self, output, cmd):
|
|
|
|
return len([o for o in output if o.startswith(cmd)])
|
|
|
|
|
2013-02-09 15:30:33 +00:00
|
|
|
def test_sql_create(self):
|
2013-12-24 11:25:17 +00:00
|
|
|
app = apps.get_app_config('commands_sql').models_module
|
2013-02-09 15:30:33 +00:00
|
|
|
output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
2013-05-29 13:47:21 +00:00
|
|
|
create_tables = [o for o in output if o.startswith('CREATE TABLE')]
|
|
|
|
self.assertEqual(len(create_tables), 3)
|
2013-02-22 23:06:45 +00:00
|
|
|
# Lower so that Oracle's upper case tbl names wont break
|
2013-05-29 13:47:21 +00:00
|
|
|
sql = create_tables[-1].lower()
|
2013-02-22 23:06:45 +00:00
|
|
|
six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
|
2013-02-09 15:30:33 +00:00
|
|
|
|
|
|
|
def test_sql_delete(self):
|
2013-12-24 11:25:17 +00:00
|
|
|
app = apps.get_app_config('commands_sql').models_module
|
2013-02-09 15:30:33 +00:00
|
|
|
output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
2013-05-29 13:47:21 +00: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 15:30:33 +00:00
|
|
|
|
|
|
|
def test_sql_indexes(self):
|
2013-12-24 11:25:17 +00:00
|
|
|
app = apps.get_app_config('commands_sql').models_module
|
2013-02-09 15:30:33 +00:00
|
|
|
output = sql_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
2013-05-29 13:47:21 +00:00
|
|
|
# PostgreSQL creates one additional index for CharField
|
|
|
|
self.assertIn(self.count_ddl(output, 'CREATE INDEX'), [3, 4])
|
2013-02-09 15:30:33 +00:00
|
|
|
|
2013-02-23 16:07:50 +00:00
|
|
|
def test_sql_destroy_indexes(self):
|
2013-12-24 11:25:17 +00:00
|
|
|
app = apps.get_app_config('commands_sql').models_module
|
2013-02-23 16:07:50 +00:00
|
|
|
output = sql_destroy_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
2013-05-29 13:47:21 +00:00
|
|
|
# PostgreSQL creates one additional index for CharField
|
|
|
|
self.assertIn(self.count_ddl(output, 'DROP INDEX'), [3, 4])
|
2013-02-23 16:07:50 +00:00
|
|
|
|
2013-02-09 15:30:33 +00:00
|
|
|
def test_sql_all(self):
|
2013-12-24 11:25:17 +00:00
|
|
|
app = apps.get_app_config('commands_sql').models_module
|
2013-02-09 15:30:33 +00:00
|
|
|
output = sql_all(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
2013-05-29 13:47:21 +00:00
|
|
|
|
|
|
|
self.assertEqual(self.count_ddl(output, 'CREATE TABLE'), 3)
|
|
|
|
# PostgreSQL creates one additional index for CharField
|
|
|
|
self.assertIn(self.count_ddl(output, 'CREATE INDEX'), [3, 4])
|
2013-10-16 15:58:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestRouter(object):
|
|
|
|
def allow_migrate(self, db, model):
|
|
|
|
return False
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-10-16 15:58:05 +00:00
|
|
|
class SQLCommandsRouterTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self._old_routers = router.routers
|
|
|
|
router.routers = [TestRouter()]
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
router.routers = self._old_routers
|
|
|
|
|
|
|
|
def test_router_honored(self):
|
2013-12-24 11:25:17 +00:00
|
|
|
app = apps.get_app_config('commands_sql').models_module
|
2013-10-16 15:58:05 +00:00
|
|
|
for sql_command in (sql_all, sql_create, sql_delete, sql_indexes, sql_destroy_indexes):
|
|
|
|
output = sql_command(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
|
|
|
self.assertEqual(len(output), 0,
|
|
|
|
"%s command is not honoring routers" % sql_command.__name__)
|