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.
24 db.clear_deferred_sql()
26 def test_create(self):
28 Test creation and deletion of tables.
30 cursor = connection.cursor()
31 # It needs to take at least 2 args
32 self.assertRaises(TypeError, db.create_table)
33 self.assertRaises(TypeError, db.create_table, "test1")
34 # Empty tables (i.e. no columns) are not fine, so make at least 1
35 db.create_table("test1", [('email_confirmed', models.BooleanField(default=False))])
36 db.start_transaction()
38 cursor.execute("SELECT * FROM test1")
39 # Make sure we can't do the same query on an empty table
41 cursor.execute("SELECT * FROM nottheretest1")
42 self.fail("Non-existent table could be selected!")
45 # Clear the dirty transaction
46 db.rollback_transaction()
47 db.start_transaction()
49 db.drop_table("test1")
52 cursor.execute("SELECT * FROM test1")
53 self.fail("Just-deleted table could be selected!")
56 # Clear the dirty transaction
57 db.rollback_transaction()
58 db.start_transaction()
59 # Try deleting a nonexistent one
61 db.delete_table("nottheretest1")
62 self.fail("Non-existent table could be deleted!")
65 db.rollback_transaction()
67 def test_foreign_keys(self):
69 Tests foreign key creation, especially uppercase (see #61)
71 Test = db.mock_model(model_name='Test', db_table='test5a',
72 db_tablespace='', pk_field_name='ID',
73 pk_field_type=models.AutoField, pk_field_args=[])
74 cursor = connection.cursor()
75 db.start_transaction()
76 db.create_table("test5a", [('ID', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True))])
77 db.create_table("test5b", [
78 ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
79 ('UNIQUE', models.ForeignKey(Test)),
81 db.execute_deferred_sql()
82 db.rollback_transaction()
84 def test_rename(self):
88 cursor = connection.cursor()
89 db.create_table("test2", [('spam', models.BooleanField(default=False))])
90 db.start_transaction()
91 # Make sure we can select the column
92 cursor.execute("SELECT spam FROM test2")
94 db.rename_column("test2", "spam", "eggs")
95 cursor.execute("SELECT eggs FROM test2")
97 cursor.execute("SELECT spam FROM test2")
98 self.fail("Just-renamed column could be selected!")
101 db.rollback_transaction()
102 db.delete_table("test2")
104 def test_dry_rename(self):
106 Test column renaming while --dry-run is turned on (should do nothing)
109 cursor = connection.cursor()
110 db.create_table("test2", [('spam', models.BooleanField(default=False))])
111 db.start_transaction()
112 # Make sure we can select the column
113 cursor.execute("SELECT spam FROM test2")
116 db.rename_column("test2", "spam", "eggs")
118 cursor.execute("SELECT spam FROM test2")
120 cursor.execute("SELECT eggs FROM test2")
121 self.fail("Dry-renamed new column could be selected!")
124 db.rollback_transaction()
125 db.delete_table("test2")
127 def test_table_rename(self):
131 cursor = connection.cursor()
132 db.create_table("testtr", [('spam', models.BooleanField(default=False))])
133 db.start_transaction()
134 # Make sure we can select the column
135 cursor.execute("SELECT spam FROM testtr")
137 db.rename_table("testtr", "testtr2")
138 cursor.execute("SELECT spam FROM testtr2")
140 cursor.execute("SELECT spam FROM testtr")
141 self.fail("Just-renamed column could be selected!")
144 db.rollback_transaction()
145 db.delete_table("testtr2")
147 def test_index(self):
149 Test the index operations
151 db.create_table("test3", [
152 ('SELECT', models.BooleanField(default=False)),
153 ('eggs', models.IntegerField(unique=True)),
155 db.execute_deferred_sql()
156 db.start_transaction()
157 # Add an index on that column
158 db.create_index("test3", ["SELECT"])
159 # Add another index on two columns
160 db.create_index("test3", ["SELECT", "eggs"])
162 db.delete_index("test3", ["SELECT"])
163 db.delete_index("test3", ["SELECT", "eggs"])
164 # Delete the unique index
165 db.delete_index("test3", ["eggs"])
166 db.rollback_transaction()
167 db.delete_table("test3")
169 def test_alter(self):
171 Test altering columns/tables
173 db.create_table("test4", [
174 ('spam', models.BooleanField(default=False)),
175 ('eggs', models.IntegerField()),
177 db.start_transaction()
179 db.add_column("test4", "add1", models.IntegerField(default=3), keep_default=False)
180 # Add a FK with keep_default=False (#69)
181 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={})
182 db.add_column("test4", "user", models.ForeignKey(User), keep_default=False)
184 db.rollback_transaction()
185 db.delete_table("test4")