Commits

agilebunch committed 20e1640

Add proper transaction handling to testcase (fixes one PostgreSQL failure)

  • Participants
  • Parent commits 8c048ce

Comments (0)

Files changed (1)

File south/tests/db.py

         Tests that unique constraints are properly created and deleted when
         altering columns.
         """
-        db.create_table("test_unique", [
+        db.create_table("test_alter_unique", [
             ('spam', models.IntegerField()),
             ('eggs', models.IntegerField(unique=True)),
         ])
         db.execute_deferred_sql()
         
         # Make sure the unique constraint is created
-        db.execute('INSERT INTO test_unique VALUES (0, 42)')
+        db.execute('INSERT INTO test_alter_unique VALUES (0, 42)')
+        db.commit_transaction()
+        db.start_transaction()
         try:
-            db.execute("INSERT INTO test_unique VALUES (1, 42)")
+            db.execute("INSERT INTO test_alter_unique VALUES (1, 42)")
+            self.fail("Could insert the same integer twice into a field with unique=True.")
         except:
             pass
-        else:
-            self.fail("Could insert the same integer twice into a field with unique=True.")
+        db.rollback_transaction()
         
         # remove constraint
-        db.alter_column("test_unique", "eggs", models.IntegerField())
+        db.alter_column("test_alter_unique", "eggs", models.IntegerField())
         # make sure the insertion works now
-        db.execute('INSERT INTO test_unique VALUES (1, 42)')
+        db.execute('INSERT INTO test_alter_unique VALUES (1, 42)')
         
         # add it back again
-        db.execute('DELETE FROM test_unique WHERE spam=1')
-        db.alter_column("test_unique", "eggs", models.IntegerField(unique=True))
+        db.execute('DELETE FROM test_alter_unique WHERE spam=1')
+        db.alter_column("test_alter_unique", "eggs", models.IntegerField(unique=True))
         # it should fail again
+        db.start_transaction()
         try:
-            db.execute("INSERT INTO test_unique VALUES (1, 42)")
+            db.execute("INSERT INTO test_alter_unique VALUES (1, 42)")
+            self.fail("Unique constraint not created during alter_column()")
         except:
             pass
-        else:
-            self.fail("Unique constraint not created during alter_column()")
+        db.rollback_transaction()
         
         # Delete the unique index/constraint
         if db.backend_name != "sqlite3":
-            db.delete_unique("test_unique", ["eggs"])
-        db.delete_table("test_unique")
+            db.delete_unique("test_alter_unique", ["eggs"])
+        db.delete_table("test_alter_unique")
+        db.start_transaction()
 
     def test_capitalised_constraints(self):
         """