Files
neutron-lib/neutron_lib/tests/unit/db/_base.py
Henry Gessau 8be88da5c1 Base DB: rehome model_base
This change 'rehomes' model_base.py from neutron.

This is not quite a verbatim copy due to the following differences:

 - The field sizes for columns are replaced with the new constants
   from neutron_lib/db/constants.py

 - The tenant_id deprecation message is adjusted slightly.

 - The StandardAttribute table is not included.

We also add some base classes and fixtures for DB unit tests.

Partially Implements: Blueprint neutron-lib

Change-Id: I2087c6f5f66154cdaa4d8fa3d86f5e493f1d24d9
2016-08-19 08:47:37 -04:00

52 lines
1.6 KiB
Python

# 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.
"""
Base classes for unit tests needing DB backend.
Only sqlite is supported in neutron-lib.
"""
import fixtures
from neutron_lib.db import _api as db_api
from neutron_lib.db import model_base
from neutron_lib.tests import _base as base
class SqlFixture(fixtures.Fixture):
# flag to indicate that the models have been loaded
_TABLES_ESTABLISHED = False
def _setUp(self):
# Register all data models
engine = db_api.get_engine()
if not SqlFixture._TABLES_ESTABLISHED:
model_base.BASEV2.metadata.create_all(engine)
SqlFixture._TABLES_ESTABLISHED = True
def clear_tables():
with engine.begin() as conn:
for table in reversed(
model_base.BASEV2.metadata.sorted_tables):
conn.execute(table.delete())
self.addCleanup(clear_tables)
class SqlTestCase(base.BaseTestCase):
def setUp(self):
super(SqlTestCase, self).setUp()
self.useFixture(SqlFixture())