-
Notifications
You must be signed in to change notification settings - Fork 10
Added Abstract Endpoint #8
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d13d52e
Added Abstract Endpoint
isivaselvan f9b5080
feat: base endpoint and response class
taru-garg-2000 bcac434
update comment
taru-garg-2000 ab1f369
Added exceptional handling for http error
isivaselvan b1a5808
Updated ruff format
isivaselvan 8ac4189
Reduced redundant testcases and Moved utils
isivaselvan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| """Tests for the endpoint module.""" | ||
|
|
||
| import pytest | ||
| from requests import Session, exceptions | ||
| from requests.models import Response as RequestResponse | ||
|
|
||
| from tfe.endpoint import Endpoint | ||
| from tfe.exception import ( | ||
| TFEConnectionException, | ||
| TFEEndpointException, | ||
| TFETimeoutException, | ||
| TFEUnauthorizedException, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_session(mocker): | ||
| """Create a mock session for testing.""" | ||
| return mocker.Mock(spec=Session) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def endpoint(mock_session): | ||
| """Create an Endpoint instance for testing.""" | ||
| return Endpoint(client=mock_session) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_response(mocker): | ||
| """Create a mock HTTP response.""" | ||
| response = mocker.Mock(spec=RequestResponse) | ||
| response.status_code = 200 | ||
| response.json.return_value = {"data": "test"} | ||
| return response | ||
|
|
||
|
|
||
| class TestEndpoint: | ||
| """Test cases for the Endpoint class.""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "method,expected_method,json_data,expected_call", | ||
| [ | ||
| ("GET", "get", None, ("get", "/test/path")), | ||
| ("POST", "post", {"key": "value"}, ("post", "/test/path")), | ||
| ("PUT", "put", {"key": "value"}, ("put", "/test/path")), | ||
| ("PATCH", "patch", {"key": "value"}, ("patch", "/test/path")), | ||
| ("DELETE", "delete", None, ("delete", "/test/path")), | ||
| ("get", "get", None, ("get", "/test/path")), # Test case insensitive | ||
| ( | ||
| "post", | ||
| "post", | ||
| {"data": "test"}, | ||
| ("post", "/test/path"), | ||
| ), # Test case insensitive | ||
| ], | ||
| ) | ||
| def test_make_request_all_methods( | ||
| self, endpoint, mock_response, method, expected_method, json_data, expected_call | ||
| ): | ||
| """Test _make_request with all supported HTTP methods.""" | ||
| # Setup the mock method to return our mock response | ||
| getattr(endpoint._http_client, expected_method).return_value = mock_response | ||
|
|
||
| # Make the request | ||
| if json_data: | ||
| response = endpoint._make_request(method, "/test/path", json=json_data) | ||
| # Verify the correct method was called with the correct arguments | ||
| getattr(endpoint._http_client, expected_method).assert_called_once_with( | ||
| "/test/path", json=json_data | ||
| ) | ||
| else: | ||
| response = endpoint._make_request(method, "/test/path") | ||
| # Verify the correct method was called with the correct arguments | ||
| getattr(endpoint._http_client, expected_method).assert_called_once_with( | ||
| "/test/path" | ||
| ) | ||
|
|
||
| # Verify the response is returned correctly | ||
| assert response == mock_response | ||
|
|
||
| def test_make_request_unsupported_method(self, endpoint): | ||
| """Test that unsupported HTTP methods raise TFEEndpointException.""" | ||
| with pytest.raises( | ||
| TFEEndpointException, | ||
| match="Unexpected error occurred during INVALID request", | ||
| ): | ||
| endpoint._make_request("INVALID", "/test/path") | ||
|
|
||
|
|
||
| class TestEndpointErrorHandling: | ||
| """Test cases for endpoint error handling.""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "method,http_method,exception_class,request_exception,expected_message,expected_cause_type", | ||
| [ | ||
| ( | ||
| "GET", | ||
| "get", | ||
| TFEConnectionException, | ||
| exceptions.ConnectionError("Connection failed"), | ||
| "Failed to connect to TFE API", | ||
| exceptions.ConnectionError, | ||
| ), | ||
| ( | ||
| "POST", | ||
| "post", | ||
| TFETimeoutException, | ||
| exceptions.Timeout("Request timed out"), | ||
| "Request timed out", | ||
| exceptions.Timeout, | ||
| ), | ||
| ( | ||
| "PUT", | ||
| "put", | ||
| TFEEndpointException, | ||
| exceptions.RequestException("Request failed"), | ||
| "Request failed: Request failed", | ||
| exceptions.RequestException, | ||
| ), | ||
| ( | ||
| "DELETE", | ||
| "delete", | ||
| TFEEndpointException, | ||
| ValueError("Unexpected error"), | ||
| "Unexpected error occurred during DELETE request", | ||
| ValueError, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_error_handling( | ||
| self, | ||
| endpoint, | ||
| method, | ||
| http_method, | ||
| exception_class, | ||
| request_exception, | ||
| expected_message, | ||
| expected_cause_type, | ||
| ): | ||
| """Test various error handling scenarios.""" | ||
| path = "/test/path" | ||
| getattr(endpoint._http_client, http_method).side_effect = request_exception | ||
|
|
||
| with pytest.raises(exception_class) as exc_info: | ||
| endpoint._make_request(method, path) | ||
|
|
||
| exception = exc_info.value | ||
| assert exception.message == expected_message | ||
| assert exception.method == method | ||
| assert exception.path == path | ||
| assert isinstance(exception.cause, expected_cause_type) | ||
|
|
||
| def test_http_error_handling(self, endpoint, mocker): | ||
| """Test HTTP error handling (delegated to error_utils).""" | ||
| method, path = "GET", "/test/path" | ||
|
|
||
| # Mock the handle_http_error function to raise an exception | ||
| mock_handle_http_error = mocker.patch("tfe.endpoint.handle_http_error") | ||
| mock_handle_http_error.side_effect = TFEUnauthorizedException( | ||
| message="Authentication failed", status_code=401, method=method, path=path | ||
| ) | ||
|
|
||
| # Create mock response that raises HTTPError | ||
| mock_response = mocker.Mock() | ||
| mock_response.status_code = 401 | ||
| mock_response.json.return_value = {"error": "Unauthorized"} | ||
| mock_response.raise_for_status.side_effect = exceptions.HTTPError( | ||
| "401 Unauthorized" | ||
| ) | ||
| mock_response.raise_for_status.side_effect.response = mock_response | ||
|
|
||
| endpoint._http_client.get.return_value = mock_response | ||
|
|
||
| # This should call handle_http_error and raise TFEUnauthorizedException | ||
| with pytest.raises(TFEUnauthorizedException): | ||
| endpoint._make_request(method, path) | ||
|
|
||
| # Verify that handle_http_error was called | ||
| mock_handle_http_error.assert_called_once() | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again no test cases for error codes