From 0ceb4ba8b6bc5d30a304e8c2a60d7dbe6e89ae14 Mon Sep 17 00:00:00 2001 From: Roman Podolyaka Date: Fri, 31 May 2013 11:04:41 +0300 Subject: Add a fixture for using of SQLite in-memory DB. Currently, SQLite DB in a file is used for testing of DB related code. Thus, in each test case we have to ensure that tearDown() drops all created tables, etc. It is much easier and cleaner to guarantee that DB is in the original state by creating it in memory instead. Change-Id: I6f0a9735a0fb31bef30842afff50c6089fa2bf92 --- tests/unit/db/sqlalchemy/base.py | 47 +++++++++++++++++++++++++++++ tests/unit/db/sqlalchemy/test_sqlalchemy.py | 24 +++------------ 2 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 tests/unit/db/sqlalchemy/base.py (limited to 'tests') 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(): -- cgit