Commits

shaib committed 0947188

Add "execute_deferred_sql()" calls, fix test_unique, add one (failing on MSSQL) test
The test added is for changing a FK from null to not null.

Comments (0)

Files changed (1)

south/tests/db.py

         # insert some data so we can test the default value of the added fkey
         db.execute("INSERT INTO test_addc (eggs, add1) VALUES (1, 2)")
         db.add_column("test_addc", "user", models.ForeignKey(User, null=True), keep_default=False)
+        db.execute_deferred_sql()
         # try selecting from the user_id column to make sure it was actually created
         val = db.execute("SELECT user_id FROM test_addc")[0][0]
         self.assertEquals(val, None)
             ('spam', models.BooleanField(default=False)),
             ('eggs', models.IntegerField()),
         ])
+        db.execute_deferred_sql()
         # Change eggs to be a FloatField
         db.alter_column("test_alterc", "eggs", models.FloatField())
+        db.execute_deferred_sql()
         db.delete_table("test_alterc")
+        db.execute_deferred_sql()
     
     def test_alter_char_default(self):
         """
         db.create_table("test_alterc", [
             ('num', models.PositiveIntegerField()),
         ])
+        db.execute_deferred_sql()
         # Add in some test values
         db.execute("INSERT INTO test_alterc (num) VALUES (1)")
         db.execute("INSERT INTO test_alterc (num) VALUES (2)")
             self.fail("Could insert a negative integer into a PositiveIntegerField.")
         # Alter it to a normal IntegerField
         db.alter_column("test_alterc", "num", models.IntegerField())
+        db.execute_deferred_sql()
         # It should now work
         db.execute("INSERT INTO test_alterc (num) VALUES (-3)")
         db.delete_table("test_alterc")
             ('eggs', models.IntegerField()),
             ('ham', models.ForeignKey(db.mock_model('Unique2', 'test_unique2'))),
         ])
+        db.execute_deferred_sql()
         # Add a constraint
         db.create_unique("test_unique", ["spam"])
+        db.execute_deferred_sql()
         # Shouldn't do anything during dry-run
         db.dry_run = True
         db.delete_unique("test_unique", ["spam"])
         db.dry_run = False
         db.delete_unique("test_unique", ["spam"])
         db.create_unique("test_unique", ["spam"])
+        # Special preparations for Sql Server
+        if db.backend_name == "pyodbc":
+            db.execute("SET IDENTITY_INSERT test_unique2 ON;")
+        db.execute("INSERT INTO test_unique2 (id) VALUES (1)")
+        db.execute("INSERT INTO test_unique2 (id) VALUES (2)")
         db.commit_transaction()
         db.start_transaction()
 
-        # Special preparations for Sql Server
-        if db.backend_name == "pyodbc":
-            db.execute("SET IDENTITY_INSERT test_unique2 ON;")
         
         # Test it works
         TRUE = (True,)
         FALSE = (False,)
-        db.execute("INSERT INTO test_unique2 (id) VALUES (1)")
-        db.execute("INSERT INTO test_unique2 (id) VALUES (2)")
         db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (%s, 0, 1)", TRUE)
         db.execute("INSERT INTO test_unique (spam, eggs, ham_id) VALUES (%s, 1, 2)", FALSE)
         try:
         db.create_table("test_text_to_char", [
             ('textcol', models.TextField()),
         ])
+        db.execute_deferred_sql()
         db.execute("INSERT INTO test_text_to_char VALUES (%s)", [value])
         db.alter_column("test_text_to_char", "textcol", models.CharField(max_length=100))
+        db.execute_deferred_sql()
         after = db.execute("select * from test_text_to_char")[0][0]
         self.assertEqual(value, after, "Change from text to char altered value [ %s != %s ]" % (`value`,`after`))
 
         db.create_table("test_char_to_text", [
             ('textcol', models.CharField(max_length=100)),
         ])
+        db.execute_deferred_sql()
         db.execute("INSERT INTO test_char_to_text VALUES (%s)", [value])
         db.alter_column("test_char_to_text", "textcol", models.TextField())
+        db.execute_deferred_sql()
         after = db.execute("select * from test_char_to_text")[0][0]
         after = unicode(after) # Oracle text fields return a sort of lazy string -- force evaluation
         self.assertEqual(value, after, "Change from char to text altered value [ %s != %s ]" % (`value`,`after`))
             ('col1', models.DateTimeField(default=end_of_world)),
             ('col2', models.DateTimeField(null=True)),
         ])
+        db.execute_deferred_sql()
         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()
         db.execute("insert into test_datetime_def (col0) values (null)")
         ends = db.execute("select col1,col2,col3 from test_datetime_def")[0]
         self.failUnlessEqual(len(ends), 3)
         
         # remove constraint
         db.alter_column("test_column_constraint", "spam", models.IntegerField())
+        db.execute_deferred_sql()
         # make sure the insertion works now
         db.execute('INSERT INTO test_column_constraint VALUES (-42)')
         db.execute('DELETE FROM test_column_constraint')
         
         # add it back again
         db.alter_column("test_column_constraint", "spam", models.PositiveIntegerField())
+        db.execute_deferred_sql()
         # it should fail again
         db.start_transaction()
         try:
             if sql_test_str not in sql:
                 self.fail("default sql value was not properly generated for field %r.\nSql was %s" % (field, sql))
 
+    def test_make_added_foreign_key_not_null(self):
+        # Table for FK to target
+        User = db.mock_model(model_name='User', db_table='auth_user', db_tablespace='', pk_field_name='id', pk_field_type=models.AutoField, pk_field_args=[], pk_field_kwargs={})
+        # Table with no foreign key
+        db.create_table("test_fk", [
+            ('eggs', models.IntegerField()),
+        ])
+        db.execute_deferred_sql()
+        
+        # Add foreign key
+        db.add_column("test_fk", 'foreik', models.ForeignKey(User, null=True),
+                      keep_default = False)
+        db.execute_deferred_sql()
+        
+        # Make the FK null
+        db.alter_column("test_fk", "foreik_id", models.ForeignKey(User))
+        db.execute_deferred_sql()
 
 class TestCacheGeneric(unittest.TestCase):
     base_ops_cls = generic.DatabaseOperations