Author: astaric Date: Fri Apr 26 11:56:33 2013 New Revision: 1476152 URL: http://svn.apache.org/r1476152 Log: Added upgrade tests for postgres backend.
Added: bloodhound/trunk/bloodhound_multiproduct/tests/upgrade_postgres.py Modified: bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py Modified: bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py?rev=1476152&r1=1476151&r2=1476152&view=diff ============================================================================== --- bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py (original) +++ bloodhound/trunk/bloodhound_multiproduct/tests/upgrade.py Fri Apr 26 11:56:33 2013 @@ -20,6 +20,7 @@ from sqlite3 import OperationalError from contextlib import contextmanager import os +import shutil import tempfile import unittest import uuid @@ -51,23 +52,25 @@ TABLES_WITH_PRODUCT_FIELD = ( class EnvironmentUpgradeTestCase(unittest.TestCase): - def setUp(self): + def setUp(self, options=()): self.env_path = tempfile.mkdtemp('multiproduct-tempenv') - self.env = Environment(self.env_path, create=True) + self.env = Environment(self.env_path, create=True, options=options) DummyPlugin.version = 1 + def tearDown(self): + shutil.rmtree(self.env_path) + def test_can_upgrade_environment_with_multi_product_disabled(self): self.env.upgrade() # Multiproduct was not enabled so multiproduct tables should not exist - with self.env.db_direct_transaction as db: - for table in BLOODHOUND_TABLES: - with self.assertFailsWithMissingTable(): - db("SELECT * FROM %s" % table) + for table in BLOODHOUND_TABLES: + with self.assertFailsWithMissingTable(): + self.env.db_direct_query("SELECT * FROM %s" % table) - for table in TABLES_WITH_PRODUCT_FIELD: - with self.assertFailsWithMissingColumn(): - db("SELECT product FROM %s" % table) + for table in TABLES_WITH_PRODUCT_FIELD: + with self.assertFailsWithMissingColumn(): + self.env.db_direct_query("SELECT product FROM %s" % table) def test_upgrade_creates_multi_product_tables_and_adds_product_column(self): self._enable_multiproduct() @@ -91,8 +94,8 @@ class EnvironmentUpgradeTestCase(unittes self._add_custom_field('custom_field') with self.env.db_direct_transaction as db: db("""INSERT INTO ticket (id) VALUES (1)""") - db("""INSERT INTO attachment (type, id) - VALUES ('ticket', '1')""") + db("""INSERT INTO attachment (type, id, filename) + VALUES ('ticket', '1', '')""") db("""INSERT INTO ticket_custom (ticket, name, value) VALUES (1, 'custom_field', '42')""") db("""INSERT INTO ticket_change (ticket, time, field) @@ -114,8 +117,8 @@ class EnvironmentUpgradeTestCase(unittes def test_upgrade_moves_custom_wikis_to_default_product(self): with self.env.db_direct_transaction as db: db("""INSERT INTO wiki (name, version) VALUES ('MyPage', 1)""") - db("""INSERT INTO attachment (type, id) - VALUES ('wiki', 'MyPage')""") + db("""INSERT INTO attachment (type, id, filename) + VALUES ('wiki', 'MyPage', '')""") self._enable_multiproduct() self.env.upgrade() @@ -131,8 +134,8 @@ class EnvironmentUpgradeTestCase(unittes def test_upgrade_duplicates_system_wikis_to_products(self): with self.env.db_direct_transaction as db: db("""INSERT INTO wiki (name, version) VALUES ('WikiStart', 1)""") - db("""INSERT INTO attachment (type, id) - VALUES ('wiki', 'WikiStart')""") + db("""INSERT INTO attachment (type, id, filename) + VALUES ('wiki', 'WikiStart', '')""") self._enable_multiproduct() self.env.upgrade() @@ -227,18 +230,18 @@ class EnvironmentUpgradeTestCase(unittes def test_can_upgrade_database_with_ticket_attachment_with_text_ids(self): with self.env.db_direct_transaction as db: - db("""INSERT INTO attachment (id, type) - VALUES ('abc', 'ticket')""") + db("""INSERT INTO attachment (id, type, filename) + VALUES ('abc', 'ticket', '')""") self._enable_multiproduct() self.env.upgrade() def test_can_upgrade_database_with_orphaned_attachments(self): with self.env.db_direct_transaction as db: - db("""INSERT INTO attachment (id, type) - VALUES ('5', 'ticket')""") - db("""INSERT INTO attachment (id, type) - VALUES ('MyWiki', 'wiki')""") + db("""INSERT INTO attachment (id, type, filename) + VALUES ('5', 'ticket', '')""") + db("""INSERT INTO attachment (id, type, filename) + VALUES ('MyWiki', 'wiki', '')""") self._enable_multiproduct() self.env.upgrade() Added: bloodhound/trunk/bloodhound_multiproduct/tests/upgrade_postgres.py URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_multiproduct/tests/upgrade_postgres.py?rev=1476152&view=auto ============================================================================== --- bloodhound/trunk/bloodhound_multiproduct/tests/upgrade_postgres.py (added) +++ bloodhound/trunk/bloodhound_multiproduct/tests/upgrade_postgres.py Fri Apr 26 11:56:33 2013 @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +try: + import psycopg2 + import uuid + conn = psycopg2.connect(host='localhost', database='test') + cur = conn.cursor() + schema = str(uuid.uuid4()).replace('-', '') + cur.execute('CREATE SCHEMA "%s"' % schema) + cur.execute('DROP SCHEMA "%s"' % schema) + conn.close() + database_available = True +except Exception as err: + print err + database_available = False + +from contextlib import contextmanager +import unittest + +import upgrade + +@unittest.skipUnless(database_available, "Postgres database not available.") +class PostgresEnvironmentUpgradeTestCase(upgrade.EnvironmentUpgradeTestCase): + def setUp(self): + self.schema = str(uuid.uuid4()).replace('-', '') + super(PostgresEnvironmentUpgradeTestCase, self).setUp( + (('trac', 'database', + 'postgres://localhost/test?schema=%s' % self.schema),) + ) + + def tearDown(self): + super(PostgresEnvironmentUpgradeTestCase, self).tearDown() + conn = psycopg2.connect(host='localhost', database='test') + cur = conn.cursor() + cur.execute('DROP SCHEMA "%s" CASCADE' % self.schema) + conn.commit() + conn.close() + + @contextmanager + def assertFailsWithMissingTable(self): + with self.assertRaises(psycopg2.ProgrammingError) as cm: + yield + self.assertIn("relation", str(cm.exception)) + self.assertIn("does not exist", str(cm.exception)) + + @contextmanager + def assertFailsWithMissingColumn(self): + with self.assertRaises(psycopg2.ProgrammingError) as cm: + yield + self.assertIn("column", str(cm.exception)) + self.assertIn("does not exist", str(cm.exception))