| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import os |
| 14 |
import testresources |
| 15 |
import testscenarios |
| 16 |
import unittest |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
from oslo_db.sqlalchemy import enginefacade |
| 20 |
from oslo_db.sqlalchemy import provision |
| 21 |
from oslo_db.sqlalchemy import test_fixtures |
| 22 |
from oslo_db.tests import base as test_base |
| 23 |
|
| 24 |
start_dir = os.path.dirname(__file__) |
| 25 |
|
| 26 |
|
| 27 |
class BackendSkipTest(test_base.BaseTestCase): |
| 28 |
|
| 29 |
def test_skip_no_dbapi(self): |
| 30 |
|
| 31 |
class FakeDatabaseOpportunisticFixture( |
| 32 |
test_fixtures.OpportunisticDbFixture): |
| 33 |
DRIVER = 'postgresql' |
| 34 |
|
| 35 |
class SomeTest(test_fixtures.OpportunisticDBTestMixin, |
| 36 |
test_base.BaseTestCase): |
| 37 |
FIXTURE = FakeDatabaseOpportunisticFixture |
| 38 |
|
| 39 |
def runTest(self): |
| 40 |
pass |
| 41 |
|
| 42 |
st = SomeTest() |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
with mock.patch( |
| 47 |
"oslo_db.sqlalchemy.provision." |
| 48 |
"Backend.backends_by_database_type", { |
| 49 |
"postgresql": |
| 50 |
provision.Backend("postgresql", "postgresql://")}): |
| 51 |
st._database_resources = {} |
| 52 |
st._db_not_available = {} |
| 53 |
st._schema_resources = {} |
| 54 |
|
| 55 |
with mock.patch( |
| 56 |
"sqlalchemy.create_engine", |
| 57 |
mock.Mock(side_effect=ImportError())): |
| 58 |
|
| 59 |
self.assertEqual([], st.resources) |
| 60 |
|
| 61 |
ex = self.assertRaises( |
| 62 |
self.skipException, |
| 63 |
st.setUp |
| 64 |
) |
| 65 |
|
| 66 |
self.assertEqual( |
| 67 |
"Backend 'postgresql' is unavailable: No DBAPI installed", |
| 68 |
str(ex) |
| 69 |
) |
| 70 |
|
| 71 |
def test_skip_no_such_backend(self): |
| 72 |
|
| 73 |
class FakeDatabaseOpportunisticFixture( |
| 74 |
test_fixtures.OpportunisticDbFixture): |
| 75 |
DRIVER = 'postgresql+nosuchdbapi' |
| 76 |
|
| 77 |
class SomeTest(test_fixtures.OpportunisticDBTestMixin, |
| 78 |
test_base.BaseTestCase): |
| 79 |
|
| 80 |
FIXTURE = FakeDatabaseOpportunisticFixture |
| 81 |
|
| 82 |
def runTest(self): |
| 83 |
pass |
| 84 |
|
| 85 |
st = SomeTest() |
| 86 |
|
| 87 |
ex = self.assertRaises( |
| 88 |
self.skipException, |
| 89 |
st.setUp |
| 90 |
) |
| 91 |
|
| 92 |
self.assertEqual( |
| 93 |
"Backend 'postgresql+nosuchdbapi' is unavailable: No such backend", |
| 94 |
str(ex) |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
class EnginefacadeIntegrationTest(test_base.BaseTestCase): |
| 99 |
def test_db_fixture(self): |
| 100 |
normal_mgr = enginefacade.transaction_context() |
| 101 |
normal_mgr.configure( |
| 102 |
connection="sqlite://", |
| 103 |
sqlite_fk=True, |
| 104 |
mysql_sql_mode="FOOBAR", |
| 105 |
max_overflow=38 |
| 106 |
) |
| 107 |
|
| 108 |
class MyFixture(test_fixtures.OpportunisticDbFixture): |
| 109 |
def get_enginefacade(self): |
| 110 |
return normal_mgr |
| 111 |
|
| 112 |
test = mock.Mock(SCHEMA_SCOPE=None) |
| 113 |
fixture = MyFixture(test=test) |
| 114 |
resources = fixture._get_resources() |
| 115 |
|
| 116 |
testresources.setUpResources(test, resources, None) |
| 117 |
self.addCleanup( |
| 118 |
testresources.tearDownResources, |
| 119 |
test, resources, None |
| 120 |
) |
| 121 |
fixture.setUp() |
| 122 |
self.addCleanup(fixture.cleanUp) |
| 123 |
|
| 124 |
self.assertTrue(normal_mgr._factory._started) |
| 125 |
|
| 126 |
test.engine = normal_mgr.writer.get_engine() |
| 127 |
self.assertEqual("sqlite://", str(test.engine.url)) |
| 128 |
self.assertIs(test.engine, normal_mgr._factory._writer_engine) |
| 129 |
engine_args = normal_mgr._factory._engine_args_for_conf(None) |
| 130 |
self.assertTrue(engine_args['sqlite_fk']) |
| 131 |
self.assertEqual("FOOBAR", engine_args["mysql_sql_mode"]) |
| 132 |
self.assertEqual(38, engine_args["max_overflow"]) |
| 133 |
|
| 134 |
fixture.cleanUp() |
| 135 |
fixture._clear_cleanups() |
| 136 |
self.assertFalse(normal_mgr._factory._started) |
| 137 |
|
| 138 |
|
| 139 |
class TestLoadHook(unittest.TestCase): |
| 140 |
"""Test the 'load_tests' hook supplied by test_base. |
| 141 |
|
| 142 |
The purpose of this loader is to organize tests into an |
| 143 |
OptimisingTestSuite using the standard unittest load_tests hook. |
| 144 |
The hook needs to detect if it is being invoked at the module |
| 145 |
level or at the package level. It has to behave completely differently |
| 146 |
in these two cases. |
| 147 |
|
| 148 |
""" |
| 149 |
|
| 150 |
def test_module_level(self): |
| 151 |
load_tests = test_fixtures.optimize_module_test_loader() |
| 152 |
|
| 153 |
loader = unittest.TestLoader() |
| 154 |
|
| 155 |
found_tests = loader.discover(start_dir, pattern="test_fixtures.py") |
| 156 |
new_loader = load_tests(loader, found_tests, "test_fixtures.py") |
| 157 |
|
| 158 |
self.assertIsInstance(new_loader, testresources.OptimisingTestSuite) |
| 159 |
|
| 160 |
actual_tests = unittest.TestSuite( |
| 161 |
testscenarios.generate_scenarios(found_tests) |
| 162 |
) |
| 163 |
|
| 164 |
self.assertEqual( |
| 165 |
new_loader.countTestCases(), actual_tests.countTestCases() |
| 166 |
) |
| 167 |
|
| 168 |
def test_package_level(self): |
| 169 |
self._test_package_level(test_fixtures.optimize_package_test_loader) |
| 170 |
|
| 171 |
def _test_package_level(self, fn): |
| 172 |
load_tests = fn( |
| 173 |
os.path.join(start_dir, "__init__.py")) |
| 174 |
|
| 175 |
loader = unittest.TestLoader() |
| 176 |
|
| 177 |
new_loader = load_tests( |
| 178 |
loader, unittest.suite.TestSuite(), "test_fixtures.py") |
| 179 |
|
| 180 |
self.assertIsInstance(new_loader, testresources.OptimisingTestSuite) |
| 181 |
|
| 182 |
actual_tests = unittest.TestSuite( |
| 183 |
testscenarios.generate_scenarios( |
| 184 |
loader.discover(start_dir, pattern="test_fixtures.py")) |
| 185 |
) |
| 186 |
|
| 187 |
self.assertEqual( |
| 188 |
new_loader.countTestCases(), actual_tests.countTestCases() |
| 189 |
) |
| 190 |
|
| 191 |
|
| 192 |
class TestWScenarios(unittest.TestCase): |
| 193 |
"""a 'do nothing' test suite. |
| 194 |
|
| 195 |
Should generate exactly four tests when testscenarios is used. |
| 196 |
|
| 197 |
""" |
| 198 |
|
| 199 |
def test_one(self): |
| 200 |
pass |
| 201 |
|
| 202 |
def test_two(self): |
| 203 |
pass |
| 204 |
|
| 205 |
scenarios = [ |
| 206 |
('scenario1', dict(scenario='scenario 1')), |
| 207 |
('scenario2', dict(scenario='scenario 2')) |
| 208 |
] |