Repository navigation
Add test coverage #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Hector Hernandez (hectorhdzg)
merged 2 commits into
microsoft:main
from
hectorhdzg:hectorhdzg/testcoverage
Apr 10, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| pytest>=8.0 | ||
| pytest-cov>=5.0 | ||
| black>=24.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader._config import BrowserSDKConfig | ||
|
|
||
| _MW_PATH = ( | ||
| "microsoft.opentelemetry._azure_monitor._browser_sdk_loader" | ||
| ".django_middleware.ApplicationInsightsWebSnippetMiddleware" | ||
| ) | ||
|
|
||
|
|
||
| class TestSetupSnippetInjection(unittest.TestCase): | ||
| def setUp(self): | ||
| self.config = BrowserSDKConfig( | ||
| enabled=True, | ||
| connection_string="InstrumentationKey=test;IngestionEndpoint=https://test.in.ai.azure.com/", | ||
| ) | ||
|
|
||
| @patch("microsoft.opentelemetry._azure_monitor._browser_sdk_loader._setup_django_injection") | ||
| def test_setup_snippet_injection_calls_django(self, mock_django): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import setup_snippet_injection | ||
|
|
||
| setup_snippet_injection(self.config) | ||
| mock_django.assert_called_once_with(self.config) | ||
|
|
||
| @patch( | ||
| "microsoft.opentelemetry._azure_monitor._browser_sdk_loader._setup_django_injection", | ||
| side_effect=Exception("boom"), | ||
| ) | ||
| def test_setup_snippet_injection_handles_exception(self, mock_django): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import setup_snippet_injection | ||
|
|
||
| # Should not raise | ||
| setup_snippet_injection(self.config) | ||
|
|
||
|
|
||
| class TestSetupDjangoInjection(unittest.TestCase): | ||
| def setUp(self): | ||
| self.config = BrowserSDKConfig( | ||
| enabled=True, | ||
| connection_string="InstrumentationKey=test;IngestionEndpoint=https://test.in.ai.azure.com/", | ||
| ) | ||
|
|
||
| @patch.dict("sys.modules", {"django": None, "django.conf": None}) | ||
| def test_django_not_available(self): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import _setup_django_injection | ||
|
|
||
| # Should not raise when django is not importable | ||
| _setup_django_injection(self.config) | ||
|
|
||
| @patch("microsoft.opentelemetry._azure_monitor._browser_sdk_loader._register_django_middleware") | ||
| def test_django_available_and_configured(self, mock_register): | ||
| settings_mock = MagicMock() | ||
| settings_mock.configured = True | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "django": MagicMock(), | ||
| "django.conf": MagicMock(settings=settings_mock), | ||
| }, | ||
| ): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import _setup_django_injection | ||
|
|
||
| _setup_django_injection(self.config) | ||
| mock_register.assert_called_once_with(self.config) | ||
|
|
||
| def test_django_not_configured(self): | ||
| settings_mock = MagicMock() | ||
| settings_mock.configured = False | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "django": MagicMock(), | ||
| "django.conf": MagicMock(settings=settings_mock), | ||
| }, | ||
| ): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import _setup_django_injection | ||
|
|
||
| # Should return early without raising | ||
| _setup_django_injection(self.config) | ||
|
|
||
|
|
||
| class TestRegisterDjangoMiddleware(unittest.TestCase): | ||
| def setUp(self): | ||
| self.config = BrowserSDKConfig( | ||
| enabled=True, | ||
| connection_string="InstrumentationKey=test;IngestionEndpoint=https://test.in.ai.azure.com/", | ||
| ) | ||
|
|
||
| def test_register_with_middleware_attr(self): | ||
| settings_mock = MagicMock(spec_set=["MIDDLEWARE"]) | ||
| settings_mock.MIDDLEWARE = ["django.middleware.common.CommonMiddleware"] | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "django": MagicMock(), | ||
| "django.conf": MagicMock(settings=settings_mock), | ||
| }, | ||
| ): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import _register_django_middleware | ||
|
|
||
| _register_django_middleware(self.config) | ||
| # Our middleware should have been appended | ||
| middleware_list = settings_mock.MIDDLEWARE | ||
| self.assertIn(_MW_PATH, middleware_list) | ||
|
|
||
| def test_register_with_middleware_classes_attr(self): | ||
| settings_mock = MagicMock(spec=[]) | ||
| settings_mock.MIDDLEWARE_CLASSES = ["django.middleware.common.CommonMiddleware"] | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "django": MagicMock(), | ||
| "django.conf": MagicMock(settings=settings_mock), | ||
| }, | ||
| ): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import _register_django_middleware | ||
|
|
||
| _register_django_middleware(self.config) | ||
| self.assertIn(_MW_PATH, settings_mock.MIDDLEWARE_CLASSES) | ||
|
|
||
| def test_already_registered(self): | ||
| middleware_path = _MW_PATH | ||
| settings_mock = MagicMock() | ||
| settings_mock.MIDDLEWARE = [middleware_path] | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "django": MagicMock(), | ||
| "django.conf": MagicMock(settings=settings_mock), | ||
| }, | ||
| ): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import _register_django_middleware | ||
|
|
||
| _register_django_middleware(self.config) | ||
| # Should not duplicate | ||
| self.assertEqual(settings_mock.MIDDLEWARE.count(middleware_path), 1) | ||
|
|
||
|
|
||
| class TestStoreDjangoConfig(unittest.TestCase): | ||
| def setUp(self): | ||
| self.config = BrowserSDKConfig( | ||
| enabled=True, | ||
| connection_string="InstrumentationKey=test;IngestionEndpoint=https://test.in.ai.azure.com/", | ||
| ) | ||
|
|
||
| def test_store_config(self): | ||
| settings_mock = MagicMock(spec=[]) | ||
| # No AZURE_MONITOR_WEB_SNIPPET_CONFIG attribute yet | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "django": MagicMock(), | ||
| "django.conf": MagicMock(settings=settings_mock), | ||
| }, | ||
| ): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import _store_django_config | ||
|
|
||
| _store_django_config(self.config) | ||
| self.assertEqual(settings_mock.AZURE_MONITOR_WEB_SNIPPET_CONFIG, self.config) | ||
|
|
||
| def test_store_config_already_set(self): | ||
| existing_config = BrowserSDKConfig(enabled=False) | ||
| settings_mock = MagicMock() | ||
| settings_mock.AZURE_MONITOR_WEB_SNIPPET_CONFIG = existing_config | ||
|
|
||
| with patch.dict( | ||
| "sys.modules", | ||
| { | ||
| "django": MagicMock(), | ||
| "django.conf": MagicMock(settings=settings_mock), | ||
| }, | ||
| ): | ||
| from microsoft.opentelemetry._azure_monitor._browser_sdk_loader import _store_django_config | ||
|
|
||
| _store_django_config(self.config) | ||
| # Should not overwrite | ||
| self.assertEqual(settings_mock.AZURE_MONITOR_WEB_SNIPPET_CONFIG, existing_config) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.