3 from south.db import db
4 from django.db import connection, models
6 # Create a list of error classes from the various database libraries
9 from psycopg2 import ProgrammingError
10 errors.append(ProgrammingError)
13 errors = tuple(errors)
15 class TestOperations(unittest.TestCase):
18 Tests if the various DB abstraction calls work.
19 Can only test a limited amount due to DB differences.
25 def test_create(self):
27 Test creation and deletion of tables.
29 cursor = connection.cursor()
30 # It needs to take at least 2 args
31 self.assertRaises(TypeError, db.create_table)
32 self.assertRaises(TypeError, db.create_table, "test1")
33 # Empty tables (i.e. no columns) are not fine, so make at least 1
34 db.create_table("test1", [('email_confirmed', models.BooleanField(default=False))])
35 db.start_transaction()
37 cursor.execute("SELECT * FROM test1")
38 # Make sure we can't do the same query on an empty table
40 cursor.execute("SELECT * FROM nottheretest1")
41 self.fail("Non-existent table could be selected!")
44 # Clear the dirty transaction
45 db.rollback_transaction()
46 db.start_transaction()
48 db.delete_table("test1")
51 cursor.execute("SELECT * FROM test1")
52 self.fail("Just-deleted table could be selected!")
55 # Clear the dirty transaction
56 db.rollback_transaction()
57 db.start_transaction()
58 # Try deleting a nonexistent one
60 db.delete_table("nottheretest1")
61 self.fail("Non-existent table could be deleted!")
64 db.rollback_transaction()
66 def test_rename(self):
70 cursor = connection.cursor()
71 db.create_table("test2", [('spam', models.BooleanField(default=False))])
72 db.start_transaction()
73 # Make sure we can select the column
74 cursor.execute("SELECT spam FROM test2")
76 db.rename_column("test2", "spam", "eggs")
77 cursor.execute("SELECT eggs FROM test2")
79 cursor.execute("SELECT spam FROM test2")
80 self.fail("Just-renamed column could be selected!")
83 db.rollback_transaction()