]> git.decadent.org.uk Git - dak.git/blob - tests/db_test.py
Merge branch 'dbtests' of ftp-master.debian.org:public_html/dak into dbtests
[dak.git] / tests / db_test.py
1 from base_test import DakTestCase, fixture
2
3 from daklib.config import Config
4 from daklib.dbconn import DBConn
5
6 from sqlalchemy import create_engine, __version__
7 from sqlalchemy.exc import SADeprecationWarning
8
9 import pickle
10 import warnings
11
12 # suppress some deprecation warnings in squeeze related to sqlalchemy
13 warnings.filterwarnings('ignore', \
14     "The SQLAlchemy PostgreSQL dialect has been renamed from 'postgres' to 'postgresql'.*", \
15     SADeprecationWarning)
16
17 class DBDakTestCase(DakTestCase):
18     def setUp(self):
19         cnf = Config()
20         if cnf["DB::Name"] in ('backports', 'obscurity', 'projectb'):
21             self.fail("You have configured an invalid database name: '%s'." % \
22                     cnf["DB::Name"])
23         if cnf["DB::Host"]:
24             # TCP/IP
25             connstr = "postgres://%s" % cnf["DB::Host"]
26             if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
27                 connstr += ":%s" % cnf["DB::Port"]
28             connstr += "/%s" % cnf["DB::Name"]
29         else:
30             # Unix Socket
31             connstr = "postgres:///%s" % cnf["DB::Name"]
32             if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
33                 connstr += "?port=%s" % cnf["DB::Port"]
34
35         pickle_filename = 'db-metadata-%s.pkl' % __version__
36         pickle_file = open(fixture(pickle_filename), 'r')
37         self.metadata = pickle.load(pickle_file)
38         self.metadata.ddl_listeners = pickle.load(pickle_file)
39         pickle_file.close()
40         self.metadata.bind = create_engine(connstr)
41         self.metadata.create_all()
42         self.session = DBConn().session()
43
44     def tearDown(self):
45         self.session.close()
46         self.metadata.drop_all()
47