|
1 |
| - |
| 1 | +import uuid |
2 | 2 | import os
|
3 | 3 | import tempfile
|
4 | 4 | from unittest.mock import patch
|
|
7 | 7 | from django.test import TestCase, override_settings
|
8 | 8 | from django.urls import path
|
9 | 9 |
|
10 |
| -from ansible_base.lib.middleware.profiling.profile_request import ProfileRequestMiddleware |
| 10 | +from ansible_base.lib.middleware.profiling.profile_request import ProfileRequestMiddleware, SQLProfilingMiddleware |
| 11 | +from ansible_base.lib.utils.settings import get_setting |
| 12 | +from test_app.models import User |
11 | 13 |
|
12 | 14 | # A simple view for testing middleware
|
13 | 15 | def simple_view(request):
|
14 | 16 | return HttpResponse("OK")
|
15 | 17 |
|
| 18 | +# A view that performs a database query |
| 19 | +def db_view(request): |
| 20 | + # Create a user with a unique username to guarantee a query. |
| 21 | + User.objects.create(username=f"test-{uuid.uuid4()}") |
| 22 | + return HttpResponse("OK") |
| 23 | + |
16 | 24 | # Define URL patterns for the test
|
17 | 25 | urlpatterns = [
|
18 | 26 | path('test/', simple_view),
|
| 27 | + path('test-db/', db_view), |
19 | 28 | ]
|
20 | 29 |
|
21 | 30 | @override_settings(ROOT_URLCONF=__name__)
|
@@ -65,3 +74,59 @@ def test_profile_request_middleware_cprofile_disabled(self):
|
65 | 74 | self.assertNotIn('X-API-CProfile-File', response)
|
66 | 75 |
|
67 | 76 |
|
| 77 | +@override_settings( |
| 78 | + ROOT_URLCONF=__name__, |
| 79 | + MIDDLEWARE=['ansible_base.lib.middleware.profiling.profile_request.SQLProfilingMiddleware'] |
| 80 | +) |
| 81 | +class SQLProfilingMiddlewareTest(TestCase): |
| 82 | + def test_sql_profiling_disabled_by_default(self): |
| 83 | + """ |
| 84 | + Test that the SQLProfilingMiddleware does not add headers when disabled. |
| 85 | + """ |
| 86 | + response = self.client.get('/test-db/') |
| 87 | + self.assertNotIn('X-API-Query-Count', response) |
| 88 | + self.assertNotIn('X-API-Query-Time', response) |
| 89 | + |
| 90 | + @override_settings(ANSIBLE_BASE_SQL_PROFILING=True, DEBUG=True) |
| 91 | + def test_sql_profiling_enabled_with_new_setting(self): |
| 92 | + """ |
| 93 | + Test that the SQLProfilingMiddleware adds headers when ANSIBLE_BASE_SQL_PROFILING is True. |
| 94 | + """ |
| 95 | + response = self.client.get('/test-db/') |
| 96 | + self.assertIn('X-API-Query-Count', response) |
| 97 | + self.assertGreaterEqual(int(response['X-API-Query-Count']), 1) |
| 98 | + self.assertIn('X-API-Query-Time', response) |
| 99 | + self.assertTrue(response['X-API-Query-Time'].endswith('s')) |
| 100 | + try: |
| 101 | + float(response['X-API-Query-Time'][:-1]) |
| 102 | + except ValueError: |
| 103 | + self.fail("X-API-Query-Time value is not a valid float") |
| 104 | + |
| 105 | + @override_settings(SQL_DEBUG=True, DEBUG=True) |
| 106 | + def test_sql_profiling_enabled_with_fallback_setting(self): |
| 107 | + """ |
| 108 | + Test that the SQLProfilingMiddleware adds headers when SQL_DEBUG is True as a fallback. |
| 109 | + """ |
| 110 | + response = self.client.get('/test-db/') |
| 111 | + self.assertIn('X-API-Query-Count', response) |
| 112 | + self.assertGreaterEqual(int(response['X-API-Query-Count']), 1) |
| 113 | + self.assertIn('X-API-Query-Time', response) |
| 114 | + self.assertTrue(response['X-API-Query-Time'].endswith('s')) |
| 115 | + try: |
| 116 | + float(response['X-API-Query-Time'][:-1]) |
| 117 | + except ValueError: |
| 118 | + self.fail("X-API-Query-Time value is not a valid float") |
| 119 | + |
| 120 | + @override_settings(ANSIBLE_BASE_SQL_PROFILING=True, DEBUG=False) |
| 121 | + def test_sql_profiling_logs_warning_if_debug_is_false(self): |
| 122 | + """ |
| 123 | + Test that the SQLProfilingMiddleware logs a warning and does not add headers |
| 124 | + if profiling is enabled but DEBUG is False. |
| 125 | + """ |
| 126 | + with self.assertLogs('ansible_base.lib.middleware.profiling.profile_request', level='WARNING') as cm: |
| 127 | + response = self.client.get('/test-db/') |
| 128 | + self.assertIn("ANSIBLE_BASE_SQL_PROFILING is enabled, but DEBUG is False", cm.output[0]) |
| 129 | + self.assertNotIn('X-API-Query-Count', response) |
| 130 | + self.assertNotIn('X-API-Query-Time', response) |
| 131 | + |
| 132 | + |
0 commit comments