summaryrefslogtreecommitdiff
path: root/tests/commands_sql
diff options
context:
space:
mode:
Diffstat (limited to 'tests/commands_sql')
-rw-r--r--tests/commands_sql/tests.py26
1 files changed, 21 insertions, 5 deletions
diff --git a/tests/commands_sql/tests.py b/tests/commands_sql/tests.py
index 8360648e51..d057883728 100644
--- a/tests/commands_sql/tests.py
+++ b/tests/commands_sql/tests.py
@@ -1,5 +1,7 @@
from __future__ import unicode_literals
+import re
+
from django.apps import apps
from django.core.management.color import no_style
from django.core.management.sql import (sql_create, sql_delete, sql_indexes,
@@ -19,11 +21,25 @@ class SQLCommandsTestCase(TestCase):
def test_sql_create(self):
app_config = apps.get_app_config('commands_sql')
output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
- create_tables = [o for o in output if o.startswith('CREATE TABLE')]
- self.assertEqual(len(create_tables), 3)
- # Lower so that Oracle's upper case tbl names wont break
- sql = create_tables[-1].lower()
- six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
+
+ 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:
+ tables.add(create_table.group('table'))
+ continue
+ reference = reference_re.match(statement)
+ if reference:
+ table = reference.group('table')
+ 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'
+ })
def test_sql_delete(self):
app_config = apps.get_app_config('commands_sql')