From 718afcafc202940cd15799459178a2534b8217d5 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 9 Feb 2013 16:30:33 +0100 Subject: [PATCH] Added tests for some sql_* management commands --- .../regressiontests/commands_sql/__init__.py | 0 tests/regressiontests/commands_sql/models.py | 7 ++++ tests/regressiontests/commands_sql/tests.py | 38 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/regressiontests/commands_sql/__init__.py create mode 100644 tests/regressiontests/commands_sql/models.py create mode 100644 tests/regressiontests/commands_sql/tests.py diff --git a/tests/regressiontests/commands_sql/__init__.py b/tests/regressiontests/commands_sql/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/commands_sql/models.py b/tests/regressiontests/commands_sql/models.py new file mode 100644 index 0000000000..089aa96f30 --- /dev/null +++ b/tests/regressiontests/commands_sql/models.py @@ -0,0 +1,7 @@ +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + + +@python_2_unicode_compatible +class Book(models.Model): + title = models.CharField(max_length=100, db_index=True) diff --git a/tests/regressiontests/commands_sql/tests.py b/tests/regressiontests/commands_sql/tests.py new file mode 100644 index 0000000000..205a295cfe --- /dev/null +++ b/tests/regressiontests/commands_sql/tests.py @@ -0,0 +1,38 @@ +from __future__ import unicode_literals + +from django.core.management.color import no_style +from django.core.management.sql import (sql_create, sql_delete, sql_indexes, + sql_all) +from django.db import connections, DEFAULT_DB_ALIAS, models +from django.test import TestCase +from django.utils import six + +# See also regressiontests/initial_sql_regress for 'custom_sql_for_model' tests + + +class SQLCommandsTestCase(TestCase): + """Tests for several functions in django/core/management/sql.py""" + def test_sql_create(self): + app = models.get_app('commands_sql') + output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS]) + six.assertRegex(self, output[0], r'^CREATE TABLE .commands_sql_book.*') + + def test_sql_delete(self): + app = models.get_app('commands_sql') + output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS]) + six.assertRegex(self, output[0], r'^DROP TABLE .commands_sql_book.*') + + def test_sql_indexes(self): + app = models.get_app('commands_sql') + output = sql_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS]) + # PostgreSQL creates two indexes + self.assertIn(len(output), [1, 2]) + self.assertTrue(output[0].startswith("CREATE INDEX")) + + def test_sql_all(self): + app = models.get_app('commands_sql') + output = sql_all(app, no_style(), connections[DEFAULT_DB_ALIAS]) + # PostgreSQL creates two indexes + self.assertIn(len(output), [2, 3]) + self.assertTrue(output[0].startswith('CREATE TABLE')) + self.assertTrue(output[1].startswith('CREATE INDEX'))