X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=tests%2Fdb_test.py;h=d95f0cbfde4d5fda34f7837d844e9d4c526a46e3;hb=e09846088b4af7a5958230c2705ca39ca6d3bfa2;hp=d8c976af3a8692b3d4312e9d72b94fef4c2a90d9;hpb=f6b04008439d80637207a989366c488294fd97a4;p=dak.git diff --git a/tests/db_test.py b/tests/db_test.py index d8c976af..d95f0cbf 100644 --- a/tests/db_test.py +++ b/tests/db_test.py @@ -5,6 +5,7 @@ from daklib.dbconn import DBConn from sqlalchemy import create_engine, __version__ from sqlalchemy.exc import SADeprecationWarning +from sqlalchemy.schema import DDL import pickle import warnings @@ -14,7 +15,35 @@ warnings.filterwarnings('ignore', \ "The SQLAlchemy PostgreSQL dialect has been renamed from 'postgres' to 'postgresql'.*", \ SADeprecationWarning) +all_tables = ['architecture', 'archive', 'bin_associations', 'bin_contents', + 'binaries', 'binary_acl', 'binary_acl_map', 'build_queue', 'build_queue_files', + 'changes', 'changes_pending_binaries', 'changes_pending_files', + 'changes_pending_files_map', 'changes_pending_source', + 'changes_pending_source_files', 'changes_pool_files', 'component', 'config', + 'dsc_files', 'files', 'fingerprint', 'keyring_acl_map', 'keyrings', 'location', + 'maintainer', 'new_comments', 'override', 'override_type', 'policy_queue', + 'priority', 'section', 'source', 'source_acl', 'src_associations', + 'src_format', 'src_uploaders', 'suite', 'suite_architectures', + 'suite_build_queue_copy', 'suite_src_formats', 'uid', 'upload_blocks'] + +drop_plpgsql = "DROP LANGUAGE IF EXISTS plpgsql CASCADE" +create_plpgsql = "CREATE LANGUAGE plpgsql" +create_function = """CREATE OR REPLACE FUNCTION tfunc_set_modified() RETURNS trigger AS $$ + BEGIN NEW.modified = now(); return NEW; END; + $$ LANGUAGE 'plpgsql'""" +create_trigger = """CREATE TRIGGER modified_%s BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE tfunc_set_modified()""" + class DBDakTestCase(DakTestCase): + def execute(self, statement): + DDL(statement).execute(self.metadata.bind) + + def create_all_triggers(self): + for statement in (drop_plpgsql, create_plpgsql, create_function): + self.execute(statement) + for table in all_tables: + self.execute(create_trigger % (table, table)) + def setUp(self): cnf = Config() if cnf["DB::Name"] in ('backports', 'obscurity', 'projectb'): @@ -39,10 +68,21 @@ class DBDakTestCase(DakTestCase): pickle_file.close() self.metadata.bind = create_engine(connstr) self.metadata.create_all() + self.create_all_triggers() self.session = DBConn().session() + def classes_to_clean(self): + """ + The function classes_to_clean() returns a list of classes. All objects + of each class will be deleted from the database in tearDown(). This + function should be overridden in derived test cases as needed. + """ + return () + def tearDown(self): - #pass - self.session.close() + self.session.rollback() + for class_ in self.classes_to_clean(): + self.session.query(class_).delete() + self.session.commit() #self.metadata.drop_all()