Debian Codegraph @

oslo_db/tests/sqlalchemy/test_provision.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import os
14 from unittest import mock
15
16 from sqlalchemy.engine import url as sqla_url
17 from sqlalchemy import exc as sa_exc
18 from sqlalchemy import inspect
19 from sqlalchemy import schema
20 from sqlalchemy import types
21
22 from oslo_db import exception
23 from oslo_db.sqlalchemy import enginefacade
24 from oslo_db.sqlalchemy import provision
25 from oslo_db.sqlalchemy import test_fixtures
26 from oslo_db.tests import base as test_base
27 from oslo_db.tests.sqlalchemy import base as db_test_base
28
29
30 class DropAllObjectsTest(db_test_base._DbTestCase):
31
32 def setUp(self):
33 super().setUp()
34
35 self.metadata = metadata = schema.MetaData()
36 schema.Table(
37 'a', metadata,
38 schema.Column('id', types.Integer, primary_key=True),
39 mysql_engine='InnoDB'
40 )
41 schema.Table(
42 'b', metadata,
43 schema.Column('id', types.Integer, primary_key=True),
44 schema.Column('a_id', types.Integer, schema.ForeignKey('a.id')),
45 mysql_engine='InnoDB'
46 )
47 schema.Table(
48 'c', metadata,
49 schema.Column('id', types.Integer, primary_key=True),
50 schema.Column('b_id', types.Integer, schema.ForeignKey('b.id')),
51 schema.Column(
52 'd_id', types.Integer,
53 schema.ForeignKey('d.id', use_alter=True, name='c_d_fk')),
54 mysql_engine='InnoDB'
55 )
56 schema.Table(
57 'd', metadata,
58 schema.Column('id', types.Integer, primary_key=True),
59 schema.Column('c_id', types.Integer, schema.ForeignKey('c.id')),
60 mysql_engine='InnoDB'
61 )
62
63 metadata.create_all(self.engine, checkfirst=False)
64 # will drop nothing if the test worked
65 self.addCleanup(metadata.drop_all, self.engine, checkfirst=True)
66
67 def test_drop_all(self):
68 insp = inspect(self.engine)
69 self.assertEqual(
70 {'a', 'b', 'c', 'd'},
71 set(insp.get_table_names())
72 )
73
74 self._get_default_provisioned_db().\
75 backend.drop_all_objects(self.engine)
76
77 insp = inspect(self.engine)
78 self.assertEqual(
79 [],
80 insp.get_table_names()
81 )
82
83
84 class BackendNotAvailableTest(test_base.BaseTestCase):
85 def test_no_dbapi(self):
86 backend = provision.Backend(
87 "postgresql", "postgresql+nosuchdbapi://hostname/dsn")
88
89 with mock.patch(
90 "sqlalchemy.create_engine",
91 mock.Mock(side_effect=ImportError("nosuchdbapi"))):
92
93 # NOTE(zzzeek): Call and test the _verify function twice, as it
94 # exercises a different code path on subsequent runs vs.
95 # the first run
96 ex = self.assertRaises(
97 exception.BackendNotAvailable,
98 backend._verify)
99 self.assertEqual(
100 "Backend 'postgresql+nosuchdbapi' is unavailable: "
101 "No DBAPI installed", str(ex))
102
103 ex = self.assertRaises(
104 exception.BackendNotAvailable,
105 backend._verify)
106 self.assertEqual(
107 "Backend 'postgresql+nosuchdbapi' is unavailable: "
108 "No DBAPI installed", str(ex))
109
110 def test_cant_connect(self):
111 backend = provision.Backend(
112 "postgresql", "postgresql+nosuchdbapi://hostname/dsn")
113
114 with mock.patch(
115 "sqlalchemy.create_engine",
116 mock.Mock(return_value=mock.Mock(connect=mock.Mock(
117 side_effect=sa_exc.OperationalError(
118 "can't connect", None, None))
119 ))
120 ):
121
122 # NOTE(zzzeek): Call and test the _verify function twice, as it
123 # exercises a different code path on subsequent runs vs.
124 # the first run
125 ex = self.assertRaises(
126 exception.BackendNotAvailable,
127 backend._verify)
128 self.assertEqual(
129 "Backend 'postgresql+nosuchdbapi' is unavailable: "
130 "Could not connect", str(ex))
131
132 ex = self.assertRaises(
133 exception.BackendNotAvailable,
134 backend._verify)
135 self.assertEqual(
136 "Backend 'postgresql+nosuchdbapi' is unavailable: "
137 "Could not connect", str(ex))
138
139
140 class MySQLDropAllObjectsTest(
141 DropAllObjectsTest, db_test_base._MySQLOpportunisticTestCase,
142 ):
143 pass
144
145
146 class PostgreSQLDropAllObjectsTest(
147 DropAllObjectsTest, db_test_base._PostgreSQLOpportunisticTestCase,
148 ):
149 pass
150
151
152 class AdHocURLTest(test_base.BaseTestCase):
153 def test_sqlite_setup_teardown(self):
154
155 fixture = test_fixtures.AdHocDbFixture("sqlite:///foo.db")
156
157 fixture.setUp()
158
159 self.assertEqual(
160 enginefacade._context_manager._factory._writer_engine.url,
161 sqla_url.make_url("sqlite:///foo.db")
162 )
163
164 self.assertTrue(os.path.exists("foo.db"))
165 fixture.cleanUp()
166
167 self.assertFalse(os.path.exists("foo.db"))
168
169 def test_mysql_setup_teardown(self):
170 try:
171 mysql_backend = provision.Backend.backend_for_database_type(
172 "mysql")
173 except exception.BackendNotAvailable:
174 self.skipTest("mysql backend not available")
175
176 mysql_backend.create_named_database("adhoc_test")
177 self.addCleanup(
178 mysql_backend.drop_named_database, "adhoc_test"
179 )
180 url = mysql_backend.provisioned_database_url("adhoc_test")
181
182 fixture = test_fixtures.AdHocDbFixture(url)
183
184 fixture.setUp()
185
186 self.assertEqual(
187 enginefacade._context_manager._factory._writer_engine.url,
188 url
189 )
190
191 fixture.cleanUp()