-
Notifications
You must be signed in to change notification settings - Fork 14
Feature/add python tests #2
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
Merged
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
213410e
Move Python tests to new directory
simonkurtz-MSFT 96a408d
Add passing tests
simonkurtz-MSFT 1e6da13
Add basic working tests
simonkurtz-MSFT 4d57874
Move code coverage files
simonkurtz-MSFT eaf1a6f
Update Python Tests workflow
simonkurtz-MSFT 204e092
Add test scripts, instructions, and update Readme
simonkurtz-MSFT 4c24e90
Update test information
simonkurtz-MSFT 5298dfe
Add Python tests badge
simonkurtz-MSFT 26bc173
Enable workflow dispatch
simonkurtz-MSFT d142cb9
Fix module path
simonkurtz-MSFT d90587e
Separate test results by python version
simonkurtz-MSFT fb02f8f
Update tests/python/test_apimrequests.py
simonkurtz-MSFT 3a6bfa2
JUnit test results
simonkurtz-MSFT 5d89692
Publish Unit Test Results to the PR
simonkurtz-MSFT feb99d0
Fix permissions
simonkurtz-MSFT 7b86a49
Customize test results title
simonkurtz-MSFT 9208c9c
Fix extract_json docstring
simonkurtz-MSFT 74ad544
Ensure code coverage and test comments still occur upon test failures
simonkurtz-MSFT 84aacdf
Fix is_string_json
simonkurtz-MSFT 0876c5d
Rename test run files
simonkurtz-MSFT d9da993
Add tests, fix failing code
simonkurtz-MSFT 7b7d6d0
Clean up
simonkurtz-MSFT 86141d2
Clean up
simonkurtz-MSFT f0d281e
Add infra deployment cleanup
simonkurtz-MSFT 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
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
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -2,4 +2,5 @@ | |
|
|
||
| requests | ||
| setuptools | ||
| pytest | ||
| pytest | ||
| pytest-cov | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
| [run] | ||
| branch = True | ||
| source = | ||
| shared/python | ||
| omit = | ||
| */__init__.py | ||
| */__pycache__/* | ||
|
|
||
| [report] | ||
| show_missing = True | ||
| skip_covered = True |
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,3 @@ | ||
| """ | ||
| Unit tests package for API Management samples. | ||
| """ |
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,36 @@ | ||
| """ | ||
| Shared test configuration and fixtures for pytest. | ||
| """ | ||
| import os | ||
| import sys | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| # Add the shared/python directory to the Python path for all tests | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../shared/python'))) | ||
|
|
||
|
|
||
| # ------------------------------ | ||
| # SHARED FIXTURES | ||
| # ------------------------------ | ||
|
|
||
| @pytest.fixture(scope="session") | ||
| def shared_python_path() -> str: | ||
| """Provide the path to the shared Python modules.""" | ||
| return os.path.abspath(os.path.join(os.path.dirname(__file__), '../../shared/python')) | ||
|
|
||
| @pytest.fixture(scope="session") | ||
| def test_data_path() -> str: | ||
| """Provide the path to test data files.""" | ||
| return os.path.abspath(os.path.join(os.path.dirname(__file__), 'data')) | ||
|
|
||
| @pytest.fixture | ||
| def sample_test_data() -> dict[str, Any]: | ||
| """Provide sample test data for use across tests.""" | ||
| return { | ||
| "test_url": "https://test-apim.azure-api.net", | ||
| "test_subscription_key": "test-subscription-key-12345", | ||
| "test_resource_group": "rg-test-apim-01", | ||
| "test_location": "eastus2" | ||
| } |
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,13 @@ | ||
| [pytest] | ||
| markers = | ||
| apimtypes: tests for apimtypes module | ||
| apimrequests: tests for apimrequests module | ||
| utils: tests for utils module | ||
| integration: integration tests that require external services | ||
| slow: tests that take a long time to run | ||
| unit: marks tests as unit tests | ||
| http: marks tests that mock or use HTTP | ||
| testpaths = | ||
| tests/python | ||
| python_files = | ||
| test_*.py |
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,3 @@ | ||
| # PowerShell script to run pytest with coverage and store .coverage in tests/python | ||
| $env:COVERAGE_FILE = "tests/python/.coverage" | ||
| pytest --cov=shared/python --cov-config=tests/python/.coveragerc --cov-report=html:tests/python/htmlcov tests/python/ |
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,4 @@ | ||
| # Shell script to run pytest with coverage and store .coverage in tests/python | ||
| COVERAGE_FILE=tests/python/.coverage | ||
| export COVERAGE_FILE | ||
| pytest --cov=shared/python --cov-config=tests/python/.coveragerc --cov-report=html:tests/python/htmlcov tests/python/ |
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,124 @@ | ||
| import pytest | ||
| import requests | ||
| from unittest.mock import patch, MagicMock | ||
| from shared.python.apimrequests import ApimRequests | ||
| from shared.python.apimtypes import HTTP_VERB, SUBSCRIPTION_KEY_PARAMETER_NAME | ||
|
|
||
| # Sample values for tests | ||
| default_url = "https://example.com/apim/" | ||
| default_key = "test-key" | ||
| default_path = "/test" | ||
| default_headers = {"Custom-Header": "Value"} | ||
| default_data = {"foo": "bar"} | ||
|
|
||
| @pytest.fixture | ||
| def apim(): | ||
| return ApimRequests(default_url, default_key) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_init_sets_headers(): | ||
| """Test that headers are set correctly when subscription key is provided.""" | ||
| apim = ApimRequests(default_url, default_key) | ||
| assert apim.url == default_url | ||
| assert apim.apimSubscriptionKey == default_key | ||
| assert apim.headers[SUBSCRIPTION_KEY_PARAMETER_NAME] == default_key | ||
| assert apim.headers["Accept"] == "application/json" | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_init_no_key(): | ||
| """Test that headers are set correctly when no subscription key is provided.""" | ||
| apim = ApimRequests(default_url) | ||
| assert apim.url == default_url | ||
| assert apim.apimSubscriptionKey is None | ||
| assert "Ocp-Apim-Subscription-Key" not in apim.headers | ||
| assert apim.headers["Accept"] == "application/json" | ||
|
|
||
| @pytest.mark.http | ||
| @patch("shared.python.apimrequests.requests.request") | ||
| @patch("shared.python.apimrequests.utils.print_message") | ||
| @patch("shared.python.apimrequests.utils.print_info") | ||
| @patch("shared.python.apimrequests.utils.print_error") | ||
| def test_single_get_success(mock_print_error, mock_print_info, mock_print_message, mock_request, apim): | ||
| mock_response = MagicMock() | ||
| mock_response.status_code = 200 | ||
| mock_response.headers = {"Content-Type": "application/json"} | ||
| mock_response.json.return_value = {"result": "ok"} | ||
| mock_response.text = '{"result": "ok"}' | ||
| mock_response.raise_for_status.return_value = None | ||
| mock_request.return_value = mock_response | ||
|
|
||
| with patch.object(apim, "_print_response") as mock_print_response: | ||
| result = apim.singleGet(default_path, printResponse=True) | ||
| assert result == '{\n "result": "ok"\n}' | ||
| mock_print_response.assert_called_once_with(mock_response) | ||
| mock_print_error.assert_not_called() | ||
|
|
||
| @pytest.mark.http | ||
| @patch("shared.python.apimrequests.requests.request") | ||
| @patch("shared.python.apimrequests.utils.print_message") | ||
| @patch("shared.python.apimrequests.utils.print_info") | ||
| @patch("shared.python.apimrequests.utils.print_error") | ||
| def test_single_get_error(mock_print_error, mock_print_info, mock_print_message, mock_request, apim): | ||
| mock_request.side_effect = requests.exceptions.RequestException("fail") | ||
| result = apim.singleGet(default_path, printResponse=True) | ||
| assert result is None | ||
| mock_print_error.assert_called_once() | ||
|
|
||
| @pytest.mark.http | ||
| @patch("shared.python.apimrequests.requests.request") | ||
| @patch("shared.python.apimrequests.utils.print_message") | ||
| @patch("shared.python.apimrequests.utils.print_info") | ||
| @patch("shared.python.apimrequests.utils.print_error") | ||
| def test_single_post_success(mock_print_error, mock_print_info, mock_print_message, mock_request, apim): | ||
| mock_response = MagicMock() | ||
| mock_response.status_code = 201 | ||
| mock_response.headers = {"Content-Type": "application/json"} | ||
| mock_response.json.return_value = {"created": True} | ||
| mock_response.text = '{"created": true}' | ||
| mock_response.raise_for_status.return_value = None | ||
| mock_request.return_value = mock_response | ||
|
|
||
| with patch.object(apim, "_print_response") as mock_print_response: | ||
| result = apim.singlePost(default_path, data=default_data, printResponse=True) | ||
| assert result == '{\n "created": true\n}' | ||
| mock_print_response.assert_called_once_with(mock_response) | ||
| mock_print_error.assert_not_called() | ||
|
|
||
| @pytest.mark.http | ||
| @patch("shared.python.apimrequests.requests.Session") | ||
| @patch("shared.python.apimrequests.utils.print_message") | ||
| @patch("shared.python.apimrequests.utils.print_info") | ||
| def test_multi_get_success(mock_print_info, mock_print_message, mock_session, apim): | ||
| mock_sess = MagicMock() | ||
| mock_response = MagicMock() | ||
| mock_response.status_code = 200 | ||
| mock_response.headers = {"Content-Type": "application/json"} | ||
| mock_response.json.return_value = {"result": "ok"} | ||
| mock_response.text = '{"result": "ok"}' | ||
| mock_response.raise_for_status.return_value = None | ||
| mock_sess.request.return_value = mock_response | ||
| mock_session.return_value = mock_sess | ||
|
|
||
| with patch.object(apim, "_print_response_code") as mock_print_code: | ||
| result = apim.multiGet(default_path, runs=2, printResponse=True) | ||
| assert len(result) == 2 | ||
| for run in result: | ||
| assert run["status_code"] == 200 | ||
| assert run["response"] == '{\n "result": "ok"\n}' | ||
| assert mock_sess.request.call_count == 2 | ||
| mock_print_code.assert_called() | ||
|
|
||
| @pytest.mark.http | ||
| @patch("shared.python.apimrequests.requests.Session") | ||
| @patch("shared.python.apimrequests.utils.print_message") | ||
| @patch("shared.python.apimrequests.utils.print_info") | ||
| def test_multi_get_error(mock_print_info, mock_print_message, mock_session, apim): | ||
| mock_sess = MagicMock() | ||
| mock_sess.request.side_effect = requests.exceptions.RequestException("fail") | ||
| mock_session.return_value = mock_sess | ||
| with patch.object(apim, "_print_response_code"): | ||
| # Should raise inside the loop, but function should still close session and return what it has | ||
simonkurtz-MSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| with pytest.raises(requests.exceptions.RequestException): | ||
| apim.multiGet(default_path, runs=1, printResponse=True) | ||
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.