Debian Codegraph @

tests/backends/base/test_base.py

raw
1 from unittest.mock import MagicMock, patch
2
3 from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction
4 from django.db.backends.base.base import BaseDatabaseWrapper
5 from django.test import (
6 SimpleTestCase,
7 TestCase,
8 TransactionTestCase,
9 skipUnlessDBFeature,
10 )
11 from django.test.utils import CaptureQueriesContext, override_settings
12
13 from ..models import Person, Square
14
15
16 class DatabaseWrapperTests(SimpleTestCase):
17 def test_repr(self):
18 conn = connections[DEFAULT_DB_ALIAS]
19 self.assertEqual(
20 repr(conn),
21 f"<DatabaseWrapper vendor={connection.vendor!r} alias='default'>",
22 )
23
24 def test_initialization_class_attributes(self):
25 """
26 The "initialization" class attributes like client_class and
27 creation_class should be set on the class and reflected in the
28 corresponding instance attributes of the instantiated backend.
29 """
30 conn = connections[DEFAULT_DB_ALIAS]
31 conn_class = type(conn)
32 attr_names = [
33 ("client_class", "client"),
34 ("creation_class", "creation"),
35 ("features_class", "features"),
36 ("introspection_class", "introspection"),
37 ("ops_class", "ops"),
38 ("validation_class", "validation"),
39 ]
40 for class_attr_name, instance_attr_name in attr_names:
41 class_attr_value = getattr(conn_class, class_attr_name)
42 self.assertIsNotNone(class_attr_value)
43 instance_attr_value = getattr(conn, instance_attr_name)
44 self.assertIsInstance(instance_attr_value, class_attr_value)
45
46 def test_initialization_display_name(self):
47 self.assertEqual(BaseDatabaseWrapper.display_name, "unknown")
48 self.assertNotEqual(connection.display_name, "unknown")
49
50 def test_get_database_version(self):
51 with patch.object(BaseDatabaseWrapper, "__init__", return_value=None):
52 msg = (
53 "subclasses of BaseDatabaseWrapper may require a "
54 "get_database_version() method."
55 )
56 with self.assertRaisesMessage(NotImplementedError, msg):
57 BaseDatabaseWrapper().get_database_version()
58
59 def test_check_database_version_supported_with_none_as_database_version(self):
60 with patch.object(connection.features, "minimum_database_version", None):
61 connection.check_database_version_supported()
62
63
64 class DatabaseWrapperLoggingTests(TransactionTestCase):
65 available_apps = ["backends"]
66
67 @override_settings(DEBUG=True)
68 def test_commit_debug_log(self):
69 conn = connections[DEFAULT_DB_ALIAS]
70 with CaptureQueriesContext(conn):
71 with self.assertLogs("django.db.backends", "DEBUG") as cm:
72 with transaction.atomic():
73 Person.objects.create(first_name="first", last_name="last")
74
75 self.assertGreaterEqual(len(conn.queries_log), 3)
76 self.assertEqual(conn.queries_log[-3]["sql"], "BEGIN")
77 self.assertRegex(
78 cm.output[0],
79 r"DEBUG:django.db.backends:\(\d+.\d{3}\) "
80 rf"BEGIN; args=None; alias={DEFAULT_DB_ALIAS}",
81 )
82 self.assertEqual(conn.queries_log[-1]["sql"], "COMMIT")
83 self.assertRegex(
84 cm.output[-1],
85 r"DEBUG:django.db.backends:\(\d+.\d{3}\) "
86 rf"COMMIT; args=None; alias={DEFAULT_DB_ALIAS}",
87 )
88
89 @override_settings(DEBUG=True)
90 def test_rollback_debug_log(self):
91 conn = connections[DEFAULT_DB_ALIAS]
92 with CaptureQueriesContext(conn):
93 with self.assertLogs("django.db.backends", "DEBUG") as cm:
94 with self.assertRaises(Exception), transaction.atomic():
95 Person.objects.create(first_name="first", last_name="last")
96 raise Exception("Force rollback")
97
98 self.assertEqual(conn.queries_log[-1]["sql"], "ROLLBACK")
99 self.assertRegex(
100 cm.output[-1],
101 r"DEBUG:django.db.backends:\(\d+.\d{3}\) "
102 rf"ROLLBACK; args=None; alias={DEFAULT_DB_ALIAS}",
103 )
104
105 def test_no_logs_without_debug(self):
106 with self.assertNoLogs("django.db.backends", "DEBUG"):
107 with self.assertRaises(Exception), transaction.atomic():
108 Person.objects.create(first_name="first", last_name="last")
109 raise Exception("Force rollback")
110
111 conn = connections[DEFAULT_DB_ALIAS]
112 self.assertEqual(len(conn.queries_log), 0)
113
114
115 class ExecuteWrapperTests(TestCase):
116 @staticmethod
117 def call_execute(connection, params=None):
118 ret_val = "1" if params is None else "%s"
119 sql = "SELECT " + ret_val + connection.features.bare_select_suffix
120 with connection.cursor() as cursor:
121 cursor.execute(sql, params)
122
123 def call_executemany(self, connection, params=None):
124 # executemany() must use an update query. Make sure it does nothing
125 # by putting a false condition in the WHERE clause.
126 sql = "DELETE FROM {} WHERE 0=1 AND 0=%s".format(Square._meta.db_table)
127 if params is None:
128 params = [(i,) for i in range(3)]
129 with connection.cursor() as cursor:
130 cursor.executemany(sql, params)
131
132 @staticmethod
133 def mock_wrapper():
134 return MagicMock(side_effect=lambda execute, *args: execute(*args))
135
136 def test_wrapper_invoked(self):
137 wrapper = self.mock_wrapper()
138 with connection.execute_wrapper(wrapper):
139 self.call_execute(connection)
140 self.assertTrue(wrapper.called)
141 (_, sql, params, many, context), _ = wrapper.call_args
142 self.assertIn("SELECT", sql)
143 self.assertIsNone(params)
144 self.assertIs(many, False)
145 self.assertEqual(context["connection"], connection)
146
147 def test_wrapper_invoked_many(self):
148 wrapper = self.mock_wrapper()
149 with connection.execute_wrapper(wrapper):
150 self.call_executemany(connection)
151 self.assertTrue(wrapper.called)
152 (_, sql, param_list, many, context), _ = wrapper.call_args
153 self.assertIn("DELETE", sql)
154 self.assertIsInstance(param_list, (list, tuple))
155 self.assertIs(many, True)
156 self.assertEqual(context["connection"], connection)
157
158 def test_database_queried(self):
159 wrapper = self.mock_wrapper()
160 with connection.execute_wrapper(wrapper):
161 with connection.cursor() as cursor:
162 sql = "SELECT 17" + connection.features.bare_select_suffix
163 cursor.execute(sql)
164 seventeen = cursor.fetchall()
165 self.assertEqual(list(seventeen), [(17,)])
166 self.call_executemany(connection)
167
168 def test_nested_wrapper_invoked(self):
169 outer_wrapper = self.mock_wrapper()
170 inner_wrapper = self.mock_wrapper()
171 with (
172 connection.execute_wrapper(outer_wrapper),
173 connection.execute_wrapper(inner_wrapper),
174 ):
175 self.call_execute(connection)
176 self.assertEqual(inner_wrapper.call_count, 1)
177 self.call_executemany(connection)
178 self.assertEqual(inner_wrapper.call_count, 2)
179
180 def test_outer_wrapper_blocks(self):
181 def blocker(*args):
182 pass
183
184 wrapper = self.mock_wrapper()
185 c = connection # This alias shortens the next line.
186 with (
187 c.execute_wrapper(wrapper),
188 c.execute_wrapper(blocker),
189 c.execute_wrapper(wrapper),
190 ):
191 with c.cursor() as cursor:
192 cursor.execute("The database never sees this")
193 self.assertEqual(wrapper.call_count, 1)
194 cursor.executemany("The database never sees this %s", [("either",)])
195 self.assertEqual(wrapper.call_count, 2)
196
197 def test_wrapper_gets_sql(self):
198 wrapper = self.mock_wrapper()
199 sql = "SELECT 'aloha'" + connection.features.bare_select_suffix
200 with connection.execute_wrapper(wrapper), connection.cursor() as cursor:
201 cursor.execute(sql)
202 (_, reported_sql, _, _, _), _ = wrapper.call_args
203 self.assertEqual(reported_sql, sql)
204
205 def test_wrapper_connection_specific(self):
206 wrapper = self.mock_wrapper()
207 with connections["other"].execute_wrapper(wrapper):
208 self.assertEqual(connections["other"].execute_wrappers, [wrapper])
209 self.call_execute(connection)
210 self.assertFalse(wrapper.called)
211 self.assertEqual(connection.execute_wrappers, [])
212 self.assertEqual(connections["other"].execute_wrappers, [])
213
214 def test_wrapper_debug(self):
215 def wrap_with_comment(execute, sql, params, many, context):
216 return execute(f"/* My comment */ {sql}", params, many, context)
217
218 with CaptureQueriesContext(connection) as ctx:
219 with connection.execute_wrapper(wrap_with_comment):
220 list(Person.objects.all())
221 last_query = ctx.captured_queries[-1]["sql"]
222 self.assertTrue(last_query.startswith("/* My comment */"))
223
224
225 class ConnectionHealthChecksTests(SimpleTestCase):
226 databases = {"default"}
227
228 def setUp(self):
229 # All test cases here need newly configured and created connections.
230 # Use the default db connection for convenience.
231 connection.close()
232 self.addCleanup(connection.close)
233
234 def patch_settings_dict(self, conn_health_checks):
235 self.settings_dict_patcher = patch.dict(
236 connection.settings_dict,
237 {
238 **connection.settings_dict,
239 "CONN_MAX_AGE": None,
240 "CONN_HEALTH_CHECKS": conn_health_checks,
241 },
242 )
243 self.settings_dict_patcher.start()
244 self.addCleanup(self.settings_dict_patcher.stop)
245
246 def run_query(self):
247 with connection.cursor() as cursor:
248 cursor.execute("SELECT 42" + connection.features.bare_select_suffix)
249
250 @skipUnlessDBFeature("test_db_allows_multiple_connections")
251 def test_health_checks_enabled(self):
252 self.patch_settings_dict(conn_health_checks=True)
253 self.assertIsNone(connection.connection)
254 # Newly created connections are considered healthy without performing
255 # the health check.
256 with patch.object(connection, "is_usable", side_effect=AssertionError):
257 self.run_query()
258
259 old_connection = connection.connection
260 # Simulate request_finished.
261 connection.close_if_unusable_or_obsolete()
262 self.assertIs(old_connection, connection.connection)
263
264 # Simulate connection health check failing.
265 with patch.object(
266 connection, "is_usable", return_value=False
267 ) as mocked_is_usable:
268 self.run_query()
269 new_connection = connection.connection
270 # A new connection is established.
271 self.assertIsNot(new_connection, old_connection)
272 # Only one health check per "request" is performed, so the next
273 # query will carry on even if the health check fails. Next query
274 # succeeds because the real connection is healthy and only the
275 # health check failure is mocked.
276 self.run_query()
277 self.assertIs(new_connection, connection.connection)
278 self.assertEqual(mocked_is_usable.call_count, 1)
279
280 # Simulate request_finished.
281 connection.close_if_unusable_or_obsolete()
282 # The underlying connection is being reused further with health checks
283 # succeeding.
284 self.run_query()
285 self.run_query()
286 self.assertIs(new_connection, connection.connection)
287
288 @skipUnlessDBFeature("test_db_allows_multiple_connections")
289 def test_health_checks_enabled_errors_occurred(self):
290 self.patch_settings_dict(conn_health_checks=True)
291 self.assertIsNone(connection.connection)
292 # Newly created connections are considered healthy without performing
293 # the health check.
294 with patch.object(connection, "is_usable", side_effect=AssertionError):
295 self.run_query()
296
297 old_connection = connection.connection
298 # Simulate errors_occurred.
299 connection.errors_occurred = True
300 # Simulate request_started (the connection is healthy).
301 connection.close_if_unusable_or_obsolete()
302 # Persistent connections are enabled.
303 self.assertIs(old_connection, connection.connection)
304 # No additional health checks after the one in
305 # close_if_unusable_or_obsolete() are executed during this "request"
306 # when running queries.
307 with patch.object(connection, "is_usable", side_effect=AssertionError):
308 self.run_query()
309
310 @skipUnlessDBFeature("test_db_allows_multiple_connections")
311 def test_health_checks_disabled(self):
312 self.patch_settings_dict(conn_health_checks=False)
313 self.assertIsNone(connection.connection)
314 # Newly created connections are considered healthy without performing
315 # the health check.
316 with patch.object(connection, "is_usable", side_effect=AssertionError):
317 self.run_query()
318
319 old_connection = connection.connection
320 # Simulate request_finished.
321 connection.close_if_unusable_or_obsolete()
322 # Persistent connections are enabled (connection is not).
323 self.assertIs(old_connection, connection.connection)
324 # Health checks are not performed.
325 with patch.object(connection, "is_usable", side_effect=AssertionError):
326 self.run_query()
327 # Health check wasn't performed and the connection is unchanged.
328 self.assertIs(old_connection, connection.connection)
329 self.run_query()
330 # The connection is unchanged after the next query either during
331 # the current "request".
332 self.assertIs(old_connection, connection.connection)
333
334 @skipUnlessDBFeature("test_db_allows_multiple_connections")
335 def test_set_autocommit_health_checks_enabled(self):
336 self.patch_settings_dict(conn_health_checks=True)
337 self.assertIsNone(connection.connection)
338 # Newly created connections are considered healthy without performing
339 # the health check.
340 with patch.object(connection, "is_usable", side_effect=AssertionError):
341 # Simulate outermost atomic block: changing autocommit for
342 # a connection.
343 connection.set_autocommit(False)
344 self.run_query()
345 connection.commit()
346 connection.set_autocommit(True)
347
348 old_connection = connection.connection
349 # Simulate request_finished.
350 connection.close_if_unusable_or_obsolete()
351 # Persistent connections are enabled.
352 self.assertIs(old_connection, connection.connection)
353
354 # Simulate connection health check failing.
355 with patch.object(
356 connection, "is_usable", return_value=False
357 ) as mocked_is_usable:
358 # Simulate outermost atomic block: changing autocommit for
359 # a connection.
360 connection.set_autocommit(False)
361 new_connection = connection.connection
362 self.assertIsNot(new_connection, old_connection)
363 # Only one health check per "request" is performed, so a query will
364 # carry on even if the health check fails. This query succeeds
365 # because the real connection is healthy and only the health check
366 # failure is mocked.
367 self.run_query()
368 connection.commit()
369 connection.set_autocommit(True)
370 # The connection is unchanged.
371 self.assertIs(new_connection, connection.connection)
372 self.assertEqual(mocked_is_usable.call_count, 1)
373
374 # Simulate request_finished.
375 connection.close_if_unusable_or_obsolete()
376 # The underlying connection is being reused further with health checks
377 # succeeding.
378 connection.set_autocommit(False)
379 self.run_query()
380 connection.commit()
381 connection.set_autocommit(True)
382 self.assertIs(new_connection, connection.connection)
383
384
385 class MultiDatabaseTests(TestCase):
386 databases = {"default", "other"}
387
388 def test_multi_database_init_connection_state_called_once(self):
389 for db in self.databases:
390 with self.subTest(database=db):
391 with patch.object(connections[db], "commit", return_value=None):
392 with patch.object(
393 connections[db],
394 "check_database_version_supported",
395 ) as mocked_check_database_version_supported:
396 connections[db].init_connection_state()
397 after_first_calls = len(
398 mocked_check_database_version_supported.mock_calls
399 )
400 connections[db].init_connection_state()
401 self.assertEqual(
402 len(mocked_check_database_version_supported.mock_calls),
403 after_first_calls,
404 )