/usr/lib/python2.7/dist-packages/oslo_db/tests/sqlalchemy/test_fixtures.py is in python-oslo.db 4.33.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | # 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 mock
import os
import testresources
import testscenarios
import unittest
from oslo_db import exception
from oslo_db.sqlalchemy import enginefacade
from oslo_db.sqlalchemy import provision
from oslo_db.sqlalchemy import test_base as legacy_test_base
from oslo_db.sqlalchemy import test_fixtures
from oslotest import base as oslo_test_base
start_dir = os.path.dirname(__file__)
class BackendSkipTest(oslo_test_base.BaseTestCase):
def test_skip_no_dbapi(self):
class FakeDatabaseOpportunisticFixture(
test_fixtures.OpportunisticDbFixture):
DRIVER = 'postgresql'
class SomeTest(test_fixtures.OpportunisticDBTestMixin,
oslo_test_base.BaseTestCase):
FIXTURE = FakeDatabaseOpportunisticFixture
def runTest(self):
pass
st = SomeTest()
# patch in replacement lookup dictionaries to avoid
# leaking from/to other tests
with mock.patch(
"oslo_db.sqlalchemy.provision."
"Backend.backends_by_database_type", {
"postgresql":
provision.Backend("postgresql", "postgresql://")}):
st._database_resources = {}
st._db_not_available = {}
st._schema_resources = {}
with mock.patch(
"sqlalchemy.create_engine",
mock.Mock(side_effect=ImportError())):
self.assertEqual([], st.resources)
ex = self.assertRaises(
self.skipException,
st.setUp
)
self.assertEqual(
"Backend 'postgresql' is unavailable: No DBAPI installed",
str(ex)
)
def test_skip_no_such_backend(self):
class FakeDatabaseOpportunisticFixture(
test_fixtures.OpportunisticDbFixture):
DRIVER = 'postgresql+nosuchdbapi'
class SomeTest(test_fixtures.OpportunisticDBTestMixin,
oslo_test_base.BaseTestCase):
FIXTURE = FakeDatabaseOpportunisticFixture
def runTest(self):
pass
st = SomeTest()
ex = self.assertRaises(
self.skipException,
st.setUp
)
self.assertEqual(
"Backend 'postgresql+nosuchdbapi' is unavailable: No such backend",
str(ex)
)
def test_skip_no_dbapi_legacy(self):
class FakeDatabaseOpportunisticFixture(
legacy_test_base.DbFixture):
DRIVER = 'postgresql'
class SomeTest(legacy_test_base.DbTestCase):
FIXTURE = FakeDatabaseOpportunisticFixture
def runTest(self):
pass
st = SomeTest()
# patch in replacement lookup dictionaries to avoid
# leaking from/to other tests
with mock.patch(
"oslo_db.sqlalchemy.provision."
"Backend.backends_by_database_type", {
"postgresql":
provision.Backend("postgresql", "postgresql://")}):
st._database_resources = {}
st._db_not_available = {}
st._schema_resources = {}
with mock.patch(
"sqlalchemy.create_engine",
mock.Mock(side_effect=ImportError())):
self.assertEqual([], st.resources)
ex = self.assertRaises(
self.skipException,
st.setUp
)
self.assertEqual(
"Backend 'postgresql' is unavailable: No DBAPI installed",
str(ex)
)
def test_skip_no_such_backend_legacy(self):
class FakeDatabaseOpportunisticFixture(
legacy_test_base.DbFixture):
DRIVER = 'postgresql+nosuchdbapi'
class SomeTest(legacy_test_base.DbTestCase):
FIXTURE = FakeDatabaseOpportunisticFixture
def runTest(self):
pass
st = SomeTest()
ex = self.assertRaises(
self.skipException,
st.setUp
)
self.assertEqual(
"Backend 'postgresql+nosuchdbapi' is unavailable: No such backend",
str(ex)
)
class EnginefacadeIntegrationTest(oslo_test_base.BaseTestCase):
def test_db_fixture(self):
normal_mgr = enginefacade.transaction_context()
normal_mgr.configure(
connection="sqlite://",
sqlite_fk=True,
mysql_sql_mode="FOOBAR",
max_overflow=38
)
class MyFixture(test_fixtures.OpportunisticDbFixture):
def get_enginefacade(self):
return normal_mgr
test = mock.Mock(SCHEMA_SCOPE=None)
fixture = MyFixture(test=test)
resources = fixture._get_resources()
testresources.setUpResources(test, resources, None)
self.addCleanup(
testresources.tearDownResources,
test, resources, None
)
fixture.setUp()
self.addCleanup(fixture.cleanUp)
self.assertTrue(normal_mgr._factory._started)
test.engine = normal_mgr.writer.get_engine()
self.assertEqual("sqlite://", str(test.engine.url))
self.assertIs(test.engine, normal_mgr._factory._writer_engine)
engine_args = normal_mgr._factory._engine_args_for_conf(None)
self.assertTrue(engine_args['sqlite_fk'])
self.assertEqual("FOOBAR", engine_args["mysql_sql_mode"])
self.assertEqual(38, engine_args["max_overflow"])
fixture.cleanUp()
fixture._clear_cleanups() # so the real cleanUp works
self.assertFalse(normal_mgr._factory._started)
class LegacyBaseClassTest(oslo_test_base.BaseTestCase):
def test_new_db_is_provisioned_by_default_pg(self):
self._test_new_db_is_provisioned_by_default(
legacy_test_base.PostgreSQLOpportunisticTestCase
)
def test_new_db_is_provisioned_by_default_mysql(self):
self._test_new_db_is_provisioned_by_default(
legacy_test_base.MySQLOpportunisticTestCase
)
def _test_new_db_is_provisioned_by_default(self, base_cls):
try:
provision.DatabaseResource(base_cls.FIXTURE.DRIVER)
except exception.BackendNotAvailable:
self.skipTest("Backend %s is not available" %
base_cls.FIXTURE.DRIVER)
class SomeTest(base_cls):
def runTest(self):
pass
st = SomeTest()
db_resource = dict(st.resources)['db']
self.assertTrue(db_resource.provision_new_database)
class TestLoadHook(unittest.TestCase):
"""Test the 'load_tests' hook supplied by test_base.
The purpose of this loader is to organize tests into an
OptimisingTestSuite using the standard unittest load_tests hook.
The hook needs to detect if it is being invoked at the module
level or at the package level. It has to behave completely differently
in these two cases.
"""
def test_module_level(self):
load_tests = test_fixtures.optimize_module_test_loader()
loader = unittest.TestLoader()
found_tests = loader.discover(start_dir, pattern="test_fixtures.py")
new_loader = load_tests(loader, found_tests, "test_fixtures.py")
self.assertTrue(
isinstance(new_loader, testresources.OptimisingTestSuite)
)
actual_tests = unittest.TestSuite(
testscenarios.generate_scenarios(found_tests)
)
self.assertEqual(
new_loader.countTestCases(), actual_tests.countTestCases()
)
def test_package_level(self):
self._test_package_level(test_fixtures.optimize_package_test_loader)
def _test_package_level(self, fn):
load_tests = fn(
os.path.join(start_dir, "__init__.py"))
loader = unittest.TestLoader()
new_loader = load_tests(
loader, unittest.suite.TestSuite(), "test_fixtures.py")
self.assertTrue(
isinstance(new_loader, testresources.OptimisingTestSuite)
)
actual_tests = unittest.TestSuite(
testscenarios.generate_scenarios(
loader.discover(start_dir, pattern="test_fixtures.py"))
)
self.assertEqual(
new_loader.countTestCases(), actual_tests.countTestCases()
)
class TestWScenarios(unittest.TestCase):
"""a 'do nothing' test suite.
Should generate exactly four tests when testscenarios is used.
"""
def test_one(self):
pass
def test_two(self):
pass
scenarios = [
('scenario1', dict(scenario='scenario 1')),
('scenario2', dict(scenario='scenario 2'))
]
|