summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-06-17 06:47:02 +0000
committerGerrit Code Review <review@openstack.org>2013-06-17 06:47:02 +0000
commit49552a75bbe14c4fbce1de40efe985fe5832816e (patch)
treea173b5a9655cd5329be409e674315be4d573712c
parentb0c51ec300a58f42f283108ae5f651dfa8bd744b (diff)
parent0ceb4ba8b6bc5d30a304e8c2a60d7dbe6e89ae14 (diff)
downloadoslo-49552a75bbe14c4fbce1de40efe985fe5832816e.tar.gz
oslo-49552a75bbe14c4fbce1de40efe985fe5832816e.tar.xz
oslo-49552a75bbe14c4fbce1de40efe985fe5832816e.zip
Merge "Add a fixture for using of SQLite in-memory DB."
-rw-r--r--tests/unit/db/sqlalchemy/base.py47
-rw-r--r--tests/unit/db/sqlalchemy/test_sqlalchemy.py24
2 files changed, 52 insertions, 19 deletions
diff --git a/tests/unit/db/sqlalchemy/base.py b/tests/unit/db/sqlalchemy/base.py
new file mode 100644
index 0000000..487af67
--- /dev/null
+++ b/tests/unit/db/sqlalchemy/base.py
@@ -0,0 +1,47 @@
+# Copyright (c) 2013 OpenStack Foundation
+# All Rights Reserved.
+#
+# Licensed 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.
+
+
+import fixtures
+from oslo.config import cfg
+
+from openstack.common.db.sqlalchemy import session
+from tests import utils as test_utils
+
+
+class SqliteInMemoryFixture(fixtures.Fixture):
+ """SQLite in-memory DB recreated for each test case."""
+
+ def __init__(self):
+ self.conf = cfg.CONF
+ self.conf.import_opt('connection',
+ 'openstack.common.db.sqlalchemy.session',
+ group='database')
+
+ def setUp(self):
+ super(SqliteInMemoryFixture, self).setUp()
+
+ self.conf.set_default('connection', "sqlite://", group='database')
+ self.addCleanup(self.conf.reset)
+ self.addCleanup(session.cleanup)
+
+
+class DbTestCase(test_utils.BaseTestCase):
+ """Base class for testing of DB code (uses in-memory SQLite DB fixture)."""
+
+ def setUp(self):
+ super(DbTestCase, self).setUp()
+
+ self.useFixture(SqliteInMemoryFixture())
diff --git a/tests/unit/db/sqlalchemy/test_sqlalchemy.py b/tests/unit/db/sqlalchemy/test_sqlalchemy.py
index e22476b..ac178b8 100644
--- a/tests/unit/db/sqlalchemy/test_sqlalchemy.py
+++ b/tests/unit/db/sqlalchemy/test_sqlalchemy.py
@@ -25,8 +25,10 @@ from sqlalchemy.ext.declarative import declarative_base
from openstack.common.db import exception as db_exc
from openstack.common.db.sqlalchemy import models
from openstack.common.db.sqlalchemy import session
+from tests.unit.db.sqlalchemy import base as test_base
from tests import utils as test_utils
+
BASE = declarative_base()
_TABLE_NAME = '__tmp__test__tmp__'
@@ -37,7 +39,7 @@ class TmpTable(BASE, models.ModelBase):
foo = Column(Integer)
-class SessionParametersTestCase(test_utils.BaseTestCase):
+class SessionParametersTestCase(test_base.DbTestCase):
def test_deprecated_session_parameters(self):
paths = self.create_tempfiles([('test', """[DEFAULT]
@@ -84,7 +86,7 @@ pool_timeout=7
self.assertEquals(test_utils.CONF.database.pool_timeout, 7)
-class SessionErrorWrapperTestCase(test_utils.BaseTestCase):
+class SessionErrorWrapperTestCase(test_base.DbTestCase):
def setUp(self):
super(SessionErrorWrapperTestCase, self).setUp()
meta = MetaData()
@@ -100,14 +102,6 @@ class SessionErrorWrapperTestCase(test_utils.BaseTestCase):
UniqueConstraint('foo', name='uniq_foo'))
test_table.create()
- def tearDown(self):
- super(SessionErrorWrapperTestCase, self).tearDown()
- meta = MetaData()
- meta.bind = session.get_engine()
- test_table = Table(_TABLE_NAME, meta, autoload=True)
- test_table.drop()
- session.cleanup()
-
def test_flush_wrapper(self):
tbl = TmpTable()
tbl.update({'foo': 10})
@@ -141,7 +135,7 @@ class RegexpTable(BASE, models.ModelBase):
bar = Column(String(255))
-class RegexpFilterTestCase(test_utils.BaseTestCase):
+class RegexpFilterTestCase(test_base.DbTestCase):
def setUp(self):
super(RegexpFilterTestCase, self).setUp()
@@ -153,14 +147,6 @@ class RegexpFilterTestCase(test_utils.BaseTestCase):
Column('bar', String(255)))
test_table.create()
- def tearDown(self):
- super(RegexpFilterTestCase, self).tearDown()
- meta = MetaData()
- meta.bind = session.get_engine()
- test_table = Table(_REGEXP_TABLE_NAME, meta, autoload=True)
- test_table.drop()
- session.cleanup()
-
def _test_regexp_filter(self, regexp, expected):
_session = session.get_session()
with _session.begin():