Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add the start of some operation tests

  • Loading branch information...
commit 05929ee89bc51de772343d18d5a7bf2518d41333 1 parent 52d2a8b
Andrew Godwin authored May 30, 2013

Showing 1 changed file with 84 additions and 0 deletions. Show diff stats Hide diff stats

  1. 84  tests/migrations/test_operations.py
84  tests/migrations/test_operations.py
... ...
@@ -0,0 +1,84 @@
  1
+from django.test import TransactionTestCase
  2
+from django.db import connection, models, migrations
  3
+from django.db.migrations.state import ProjectState, ModelState
  4
+
  5
+
  6
+class OperationTests(TransactionTestCase):
  7
+    """
  8
+    Tests running the operations and making sure they do what they say they do.
  9
+    Each test looks at their state changing, and then their database operation -
  10
+    both forwards and backwards.
  11
+    """
  12
+
  13
+    def assertTableExists(self, table):
  14
+        self.assertIn(table, connection.introspection.get_table_list(connection.cursor()))
  15
+
  16
+    def assertTableNotExists(self, table):
  17
+        self.assertNotIn(table, connection.introspection.get_table_list(connection.cursor()))
  18
+
  19
+    def set_up_test_model(self, app_label):
  20
+        """
  21
+        Creates a test model state and database table.
  22
+        """
  23
+        # Make the "current" state
  24
+        creation = migrations.CreateModel(
  25
+            "Pony",
  26
+            [
  27
+                ("id", models.AutoField(primary_key=True)),
  28
+                ("pink", models.BooleanField(default=True)),
  29
+            ],
  30
+        )
  31
+        project_state = ProjectState()
  32
+        creation.state_forwards(app_label, project_state)
  33
+        # Set up the database
  34
+        with connection.schema_editor() as editor:
  35
+            creation.database_forwards(app_label, editor, ProjectState(), project_state)
  36
+        return project_state
  37
+
  38
+    def test_create_model(self):
  39
+        """
  40
+        Tests the CreateModel operation.
  41
+        Most other tests use this as part of setup, so check failures here first.
  42
+        """
  43
+        operation = migrations.CreateModel(
  44
+            "Pony",
  45
+            [
  46
+                ("id", models.AutoField(primary_key=True)),
  47
+                ("pink", models.BooleanField(default=True)),
  48
+            ],
  49
+        )
  50
+        # Test the state alteration
  51
+        project_state = ProjectState()
  52
+        new_state = project_state.clone()
  53
+        operation.state_forwards("test_crmo", new_state)
  54
+        self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony")
  55
+        self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2)
  56
+        # Test the database alteration
  57
+        self.assertTableNotExists("test_crmo_pony")
  58
+        with connection.schema_editor() as editor:
  59
+            operation.database_forwards("test_crmo", editor, project_state, new_state)
  60
+        self.assertTableExists("test_crmo_pony")
  61
+        # And test reversal
  62
+        with connection.schema_editor() as editor:
  63
+            operation.database_backwards("test_crmo", editor, new_state, project_state)
  64
+        self.assertTableNotExists("test_crmo_pony")
  65
+
  66
+    def test_delete_model(self):
  67
+        """
  68
+        Tests the DeleteModel operation.
  69
+        """
  70
+        project_state = self.set_up_test_model("test_dlmo")
  71
+        # Test the state alteration
  72
+        operation = migrations.DeleteModel("Pony")
  73
+        new_state = project_state.clone()
  74
+        operation.state_forwards("test_dlmo", new_state)
  75
+        self.assertNotIn(("test_dlmo", "pony"), new_state.models)
  76
+        # Test the database alteration
  77
+        self.assertTableExists("test_dlmo_pony")
  78
+        with connection.schema_editor() as editor:
  79
+            operation.database_forwards("test_dlmo", editor, project_state, new_state)
  80
+        self.assertTableNotExists("test_dlmo_pony")
  81
+        # And test reversal
  82
+        with connection.schema_editor() as editor:
  83
+            operation.database_backwards("test_dlmo", editor, new_state, project_state)
  84
+        self.assertTableExists("test_dlmo_pony")

0 notes on commit 05929ee

Please sign in to comment.
Something went wrong with that request. Please try again.