-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add unit tests to 'default_location()' function with mocked API response #156
Conversation
- Added pytest-mock = "*" to the dependencies in pyproject.toml. - Updated poetry.lock to reflect the new dependency. - pytest-mock is required for mocking functionality in unit tests.
- Add 2 unit tests using API mocking to handle a successful response and a bad request response. - Mocking the API call helps isolate the function's behavior, providing faster, more reliable tests without making real HTTP requests.
Reviewer's Guide by SourceryThe PR adds unit tests for the Class diagram for test structure with mocked API responseclassDiagram
class TestAPI {
+test_default_location_mocked(mocker, status_code, json_data, expected_result)
}
class Mock {
+status_code
+json()
}
class Mocker {
+patch(target, return_value)
}
TestAPI --> Mock : uses
TestAPI --> Mocker : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
👋 Hi! Thanks for submitting your first pull request!
• We appreciate your effort to improve this project.
• If you're enjoying your experience, please consider giving us a star ⭐
• It helps us grow and motivates us to keep improving! 🚀
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.
Hey @latiffetahaj - I've reviewed your changes - here's some feedback:
Overall Comments:
- Please pin the pytest-mock version in pyproject.toml instead of using '*' to ensure build reproducibility
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟡 Testing: 1 issue found
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
# Arrange: Mock the response from the API | ||
mock_response = Mock() | ||
mock_response.status_code = status_code | ||
mock_response.json = Mock(return_value=json_data) |
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.
suggestion (testing): Consider testing API timeout scenario
Add a test case where the API request times out (can be simulated by making the mock raise requests.exceptions.Timeout). This is important as the code specifies a timeout parameter of 10 seconds.
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.
Nice, this looks good. Mocking was desperately needed in the project
Hey @latiffetahaj, this looks great! I haven't mocked any API calls before so reviewing your PR was a learning experience. It looks well done and now im realizing theres a good chunk of functions in the codebase that need to be mocked eventually (gpt tests for ex). Good work, thanks for your contribution & teaching me something! 🙇 |
@all-contributors please add @latiffetahaj for code and unit tests |
I've put up a pull request to add @latiffetahaj! 🎉 |
Codecov ReportAll modified and coverable lines are covered by tests ✅ |
default_location
function usingpytest
andpytest-mock
.pyproject.toml
to includepytest-mock
as a dependency.poetry.lock
to reflect dependency changes.This ensures the function processes API responses correctly and improves test reliability by decoupling from external services.
Summary by Sourcery
Add unit tests for the 'default_location' function with mocked API responses to ensure correct processing of API responses and improve test reliability by decoupling from external services.
Build:
Tests: