Refactor DB unit test

Change-Id: I0f4d1a50d010b78bd7a56b39bb4c16315fbe081c
This commit is contained in:
lawrancejing 2015-11-11 12:30:11 +00:00
parent 9c87af0bfc
commit 2b0af4dafb
4 changed files with 36 additions and 99 deletions

View File

@ -1,6 +1,3 @@
# Copyright 2010-2011 OpenStack Foundation
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# 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
@ -13,19 +10,15 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
import testscenarios
import mock
from oslo_config import cfg
from oslo_log import log
from oslotest import base
import pecan
import testscenarios
from evoque.common import context as evoque_context
from evoque.tests.unit import fixture
CONF = cfg.CONF
log.register_options(CONF)
CONF.set_override('use_stderr', False)
@ -39,11 +32,13 @@ class BaseTestCase(testscenarios.WithScenarios, base.BaseTestCase):
self.addCleanup(cfg.CONF.reset)
class TestCase(base.BaseTestCase):
class DBTestCase(base.BaseTestCase):
"""Test case base class for all unit tests."""
def setUp(self):
super(TestCase, self).setUp()
super(DBTestCase, self).setUp()
self.useFixture(fixture.DBTestFixture())
token_info = {
'token': {
'project': {
@ -58,33 +53,3 @@ class TestCase(base.BaseTestCase):
auth_token_info=token_info,
project_id='fake_project',
user_id='fake_user')
def make_context(*args, **kwargs):
# If context hasn't been constructed with token_info
if not kwargs.get('auth_token_info'):
kwargs['auth_token_info'] = copy.deepcopy(token_info)
if not kwargs.get('project_id'):
kwargs['project_id'] = 'fake_project'
if not kwargs.get('user_id'):
kwargs['user_id'] = 'fake_user'
context = evoque_context.RequestContext(*args, **kwargs)
return evoque_context.RequestContext.from_dict(context.to_dict())
p = mock.patch.object(evoque_context, 'make_context',
side_effect=make_context)
self.mock_make_context = p.start()
self.addCleanup(p.stop)
self.useFixture(fixture.ConfigFixture())
def reset_pecan():
pecan.set_config({}, overwrite=True)
self.addCleanup(reset_pecan)
def config(self, **kw):
"""Override config options for a test."""
group = kw.pop('group', None)
for k, v in kw.items():
CONF.set_override(k, v, group)

View File

@ -1,49 +0,0 @@
# Copyright (c) 2012 NTT DOCOMO, INC.
# 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.
"""Evoque DB test base class."""
import fixtures
from oslo_config import cfg
from evoque.db import api as db_api
from evoque.tests.unit import base
CONF = cfg.CONF
_DB_CACHE = None
class Database(fixtures.Fixture):
def __init__(self, db_api):
self.engine = db_api.get_engine()
def _setUp(self):
db_api.db_sync(self.engine)
self.engine.connect()
class DBTestCase(base.TestCase):
def setUp(self):
super(DBTestCase, self).setUp()
self.db_api = db_api
global _DB_CACHE
if not _DB_CACHE:
_DB_CACHE = Database(db_api)
self.useFixture(_DB_CACHE)

View File

@ -15,7 +15,7 @@
"""Tests for manipulating Bays via the DB API"""
from evoque.tests.unit.db import base
from evoque.tests.unit import base
from evoque.tests.unit.db import utils

View File

@ -15,19 +15,40 @@
# under the License.
import fixtures
import sqlalchemy
from oslo_config import cfg
from oslo_log import log
from evoque.db import api as db_api
CONF = cfg.CONF
CONF.import_opt('connection', 'oslo_db.options', group='database')
CONF.import_opt('sqlite_synchronous', 'oslo_db.options', group='database')
class ConfigFixture(fixtures.Fixture):
"""Fixture to manage global conf settings."""
class DBTestFixture(fixtures.Fixture):
def __init__(self):
# Use sqlite as test DB
self.sqlite_db = '/tmp/evoque.db'
CONF.set_default('connection', "sqlite://", group='database')
CONF.set_default('sqlite_db', self.sqlite_db, group='database')
CONF.set_default('sqlite_synchronous', False, group='database')
def _setUp(self):
log.register_options(cfg.CONF)
CONF.set_default('connection', "sqlite://", group='database')
CONF.set_default('sqlite_synchronous', False, group='database')
self.addCleanup(CONF.reset)
self._setup_test_db()
self.addCleanup(self._reset_test_db)
def _setup_test_db(self):
engine = db_api.get_engine()
db_api.db_sync(engine)
engine.connect()
def _reset_test_db(self):
engine = db_api.get_engine()
meta = sqlalchemy.MetaData()
meta.reflect(bind=engine)
for table in reversed(meta.sorted_tables):
if table.name == 'migrate_version':
continue
engine.execute(table.delete())