Added setting extra_info on Book models during import.
[wolnelektury.git] / apps / south / tests / db.py
1 import unittest
2
3 from south.db import db
4 from django.db import connection, models
5
6 # Create a list of error classes from the various database libraries
7 errors = []
8 try:
9     from psycopg2 import ProgrammingError
10     errors.append(ProgrammingError)
11 except ImportError:
12     pass
13 errors = tuple(errors)
14
15 class TestOperations(unittest.TestCase):
16
17     """
18     Tests if the various DB abstraction calls work.
19     Can only test a limited amount due to DB differences.
20     """
21
22     def setUp(self):
23         db.debug = False
24
25     def test_create(self):
26         """
27         Test creation and deletion of tables.
28         """
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()
36         # And should exist
37         cursor.execute("SELECT * FROM test1")
38         # Make sure we can't do the same query on an empty table
39         try:
40             cursor.execute("SELECT * FROM nottheretest1")
41             self.fail("Non-existent table could be selected!")
42         except:
43             pass
44         # Clear the dirty transaction
45         db.rollback_transaction()
46         db.start_transaction()
47         # Remove the table
48         db.delete_table("test1")
49         # Make sure it went
50         try:
51             cursor.execute("SELECT * FROM test1")
52             self.fail("Just-deleted table could be selected!")
53         except:
54             pass
55         # Clear the dirty transaction
56         db.rollback_transaction()
57         db.start_transaction()
58         # Try deleting a nonexistent one
59         try:
60             db.delete_table("nottheretest1")
61             self.fail("Non-existent table could be deleted!")
62         except:
63             pass
64         db.rollback_transaction()
65     
66     def test_rename(self):
67         """
68         Test column renaming
69         """
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")
75         # Rename it
76         db.rename_column("test2", "spam", "eggs")
77         cursor.execute("SELECT eggs FROM test2")
78         try:
79             cursor.execute("SELECT spam FROM test2")
80             self.fail("Just-renamed column could be selected!")
81         except:
82             pass
83         db.rollback_transaction()