|
| 1 | +# Copyright 2017, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from collections import namedtuple |
| 17 | + |
| 18 | +import django |
| 19 | +import mock |
| 20 | +import pytest |
| 21 | +from django.test.utils import teardown_test_environment |
| 22 | + |
| 23 | +from opencensus.trace import execution_context |
| 24 | + |
| 25 | + |
| 26 | +class TestOpencensusDatabaseMiddleware(unittest.TestCase): |
| 27 | + def setUp(self): |
| 28 | + from django.conf import settings as django_settings |
| 29 | + from django.test.utils import setup_test_environment |
| 30 | + |
| 31 | + if not django_settings.configured: |
| 32 | + django_settings.configure() |
| 33 | + setup_test_environment() |
| 34 | + |
| 35 | + def tearDown(self): |
| 36 | + execution_context.clear() |
| 37 | + teardown_test_environment() |
| 38 | + |
| 39 | + def test_process_request(self): |
| 40 | + if django.VERSION < (2, 0): |
| 41 | + pytest.skip("Wrong version of Django") |
| 42 | + |
| 43 | + from opencensus.ext.django import middleware |
| 44 | + |
| 45 | + sql = "SELECT * FROM users" |
| 46 | + |
| 47 | + MockConnection = namedtuple('Connection', ('vendor', 'alias')) |
| 48 | + connection = MockConnection('mysql', 'default') |
| 49 | + |
| 50 | + mock_execute = mock.Mock() |
| 51 | + mock_execute.return_value = "Mock result" |
| 52 | + |
| 53 | + middleware.OpencensusMiddleware() |
| 54 | + |
| 55 | + patch_no_tracer = mock.patch( |
| 56 | + 'opencensus.ext.django.middleware._get_current_tracer', |
| 57 | + return_value=None) |
| 58 | + with patch_no_tracer: |
| 59 | + result = middleware._trace_db_call( |
| 60 | + mock_execute, sql, params=[], many=False, |
| 61 | + context={'connection': connection}) |
| 62 | + self.assertEqual(result, "Mock result") |
| 63 | + |
| 64 | + mock_tracer = mock.Mock() |
| 65 | + mock_tracer.return_value = mock_tracer |
| 66 | + patch = mock.patch( |
| 67 | + 'opencensus.ext.django.middleware._get_current_tracer', |
| 68 | + return_value=mock_tracer) |
| 69 | + with patch: |
| 70 | + result = middleware._trace_db_call( |
| 71 | + mock_execute, sql, params=[], many=False, |
| 72 | + context={'connection': connection}) |
| 73 | + |
| 74 | + (mock_sql, mock_params, mock_many, |
| 75 | + mock_context) = mock_execute.call_args[0] |
| 76 | + |
| 77 | + self.assertEqual(mock_sql, sql) |
| 78 | + self.assertEqual(mock_params, []) |
| 79 | + self.assertEqual(mock_many, False) |
| 80 | + self.assertEqual(mock_context, {'connection': connection}) |
| 81 | + self.assertEqual(result, "Mock result") |
| 82 | + |
| 83 | + result = middleware._trace_db_call( |
| 84 | + mock_execute, sql, params=[], many=True, |
| 85 | + context={'connection': connection}) |
| 86 | + |
| 87 | + (mock_sql, mock_params, mock_many, |
| 88 | + mock_context) = mock_execute.call_args[0] |
| 89 | + self.assertEqual(mock_many, True) |
0 commit comments