summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2012-06-18 17:32:03 +0100
committerAndrew Godwin <andrew@aeracode.org>2012-06-18 17:34:36 +0100
commit8ba5bf31986fa746ecc81683c64999dcea4f8e0a (patch)
treebe8902819bd57167c10544313191abd92d623d44 /tests
parentac1b9ae6301e2c38af7951f1feb56dafc380d3e7 (diff)
Very start of schema alteration port. Create/delete model and some tests.
Diffstat (limited to 'tests')
-rw-r--r--tests/modeltests/schema/__init__.py0
-rw-r--r--tests/modeltests/schema/models.py21
-rw-r--r--tests/modeltests/schema/tests.py102
3 files changed, 123 insertions, 0 deletions
diff --git a/tests/modeltests/schema/__init__.py b/tests/modeltests/schema/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/modeltests/schema/__init__.py
diff --git a/tests/modeltests/schema/models.py b/tests/modeltests/schema/models.py
new file mode 100644
index 0000000000..2c5dc829c6
--- /dev/null
+++ b/tests/modeltests/schema/models.py
@@ -0,0 +1,21 @@
+from django.db import models
+
+# Because we want to test creation and deletion of these as separate things,
+# these models are all marked as unmanaged and only marked as managed while
+# a schema test is running.
+
+
+class Author(models.Model):
+ name = models.CharField(max_length=255)
+
+ class Meta:
+ managed = False
+
+
+class Book(models.Model):
+ author = models.ForeignKey(Author)
+ title = models.CharField(max_length=100)
+ pub_date = models.DateTimeField()
+
+ class Meta:
+ managed = False
diff --git a/tests/modeltests/schema/tests.py b/tests/modeltests/schema/tests.py
new file mode 100644
index 0000000000..6d5d27cdf1
--- /dev/null
+++ b/tests/modeltests/schema/tests.py
@@ -0,0 +1,102 @@
+from __future__ import absolute_import
+import copy
+import datetime
+from django.test import TestCase
+from django.db.models.loading import cache
+from django.db import connection, DatabaseError, IntegrityError
+from .models import Author, Book
+
+
+class SchemaTests(TestCase):
+ """
+ Tests that the schema-alteration code works correctly.
+
+ Be aware that these tests are more liable than most to false results,
+ as sometimes the code to check if a test has worked is almost as complex
+ as the code it is testing.
+ """
+
+ models = [Author, Book]
+
+ def setUp(self):
+ # Make sure we're in manual transaction mode
+ connection.commit_unless_managed()
+ connection.enter_transaction_management()
+ connection.managed(True)
+ # The unmanaged models need to be removed after the test in order to
+ # prevent bad interactions with the flush operation in other tests.
+ self.old_app_models = copy.deepcopy(cache.app_models)
+ self.old_app_store = copy.deepcopy(cache.app_store)
+ for model in self.models:
+ model._meta.managed = True
+
+ def tearDown(self):
+ # Rollback anything that may have happened
+ connection.rollback()
+ # Delete any tables made for our models
+ cursor = connection.cursor()
+ for model in self.models:
+ try:
+ cursor.execute("DROP TABLE %s CASCADE" % (
+ connection.ops.quote_name(model._meta.db_table),
+ ))
+ except DatabaseError:
+ connection.rollback()
+ else:
+ connection.commit()
+ # Unhook our models
+ for model in self.models:
+ model._meta.managed = False
+ cache.app_models = self.old_app_models
+ cache.app_store = self.old_app_store
+ cache._get_models_cache = {}
+
+ def test_creation_deletion(self):
+ """
+ Tries creating a model's table, and then deleting it.
+ """
+ # Create the table
+ editor = connection.schema_editor()
+ editor.start()
+ editor.create_model(Author)
+ editor.commit()
+ # Check that it's there
+ try:
+ list(Author.objects.all())
+ except DatabaseError, e:
+ self.fail("Table not created: %s" % e)
+ # Clean up that table
+ editor.start()
+ editor.delete_model(Author)
+ editor.commit()
+ # Check that it's gone
+ self.assertRaises(
+ DatabaseError,
+ lambda: list(Author.objects.all()),
+ )
+
+ def test_creation_fk(self):
+ "Tests that creating tables out of FK order works"
+ # Create the table
+ editor = connection.schema_editor()
+ editor.start()
+ editor.create_model(Book)
+ editor.create_model(Author)
+ editor.commit()
+ # Check that both tables are there
+ try:
+ list(Author.objects.all())
+ except DatabaseError, e:
+ self.fail("Author table not created: %s" % e)
+ try:
+ list(Book.objects.all())
+ except DatabaseError, e:
+ self.fail("Book table not created: %s" % e)
+ # Make sure the FK constraint is present
+ with self.assertRaises(IntegrityError):
+ Book.objects.create(
+ author_id = 1,
+ title = "Much Ado About Foreign Keys",
+ pub_date = datetime.datetime.now(),
+ )
+ connection.commit()