shaib  committed 79acae9

Fix tests to require no defaults in database

  • Participants
  • Parent commits 75080a0
  • Branches dont-leave-defaults

Comments (0)

Files changed (1)

File south/tests/

         # insert some data so we can test the default values of the added column
         db.execute("INSERT INTO test_addnbc (spam, eggs) VALUES (%s, 1)", [False])
         # try selecting from the new columns to make sure they were properly created
-        false, null, true = db.execute("SELECT spam,add1,add2 FROM test_addnbc")[0][0:3]
-        self.assertTrue(true)
-        self.assertEquals(null, None)
+        false, null1, null2 = db.execute("SELECT spam,add1,add2 FROM test_addnbc")[0][0:3]
+        self.assertIsNone(null1, "Null boolean field with no value inserted returns non-null")
+        self.assertIsNone(null2, "Null boolean field (added with default) with no value inserted returns non-null")
         self.assertEquals(false, False)
         # Change spam default
         db.alter_column("test_altercd", "spam", models.CharField(max_length=30, default="loof"))
+        # Assert the default is not in the database
+        db.execute("INSERT INTO test_altercd (eggs) values (12)")
+        null = db.execute("SELECT spam FROM test_altercd")[0][0]
+        self.assertFalse(null, "Default for char field was installed into database")
     def test_mysql_defaults(self):
     def test_datetime_default(self):
-        Test that defaults are created correctly for datetime columns
+        Test that defaults are correctly not created for datetime columns
         end_of_world = datetime.datetime(2012, 12, 21, 0, 0, 1)
             ('col2', models.DateTimeField(null=True)),
+        # insert a row
+        db.execute("INSERT INTO test_datetime_def (col0, col1, col2) values (null,%s,null)", [end_of_world])
         db.alter_column("test_datetime_def", "col2", models.DateTimeField(default=end_of_world))
         db.add_column("test_datetime_def", "col3", models.DateTimeField(default=end_of_world))
-        # There should not be a default in the database for col1
+        # In the single existing row, we now expect col1=col2=col3=end_of_world...
-        self.assertRaises(
-            IntegrityError,
-            db.execute, "insert into test_datetime_def (col0) values (null)"
-        )
-        db.rollback_transaction()
-        db.start_transaction()
-        # There should be for the others
-        db.execute("insert into test_datetime_def (col0, col1) values (null, %s)", [end_of_world])
         ends = db.execute("select col1,col2,col3 from test_datetime_def")[0]
         self.failUnlessEqual(len(ends), 3)
         for e in ends:
             self.failUnlessEqual(e, end_of_world)
+        db.commit_transaction()
+        # ...but there should not be a default in the database for any column (other than the 'null' for col0)
+        for cols in ["col1,col2", "col1,col3","col2,col3"]:
+            db.start_transaction()
+            statement = "insert into test_datetime_def (col0,%s) values (null,%%s,%%s)" % cols
+            self.assertRaises(
+                IntegrityError,
+                db.execute, statement, [None, end_of_world, end_of_world]
+            )
+            db.rollback_transaction()
     def test_add_unique_fk(self):