shaib avatar shaib committed 79acae9

Fix tests to require no defaults in database

Comments (0)

Files changed (1)

south/tests/db.py

         # 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)
         db.delete_table("test_addnbc")
     
         ])
         # 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)),
         ])
         db.execute_deferred_sql()
+        # 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))
         db.execute_deferred_sql()
-        # There should not be a default in the database for col1
         db.commit_transaction()
+        # In the single existing row, we now expect col1=col2=col3=end_of_world...
         db.start_transaction()
-        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):
         """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.