Skip to content
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

Merged
merged 2 commits into from
Dec 2, 2024

Conversation

latiffetahaj
Copy link
Contributor

@latiffetahaj latiffetahaj commented Dec 1, 2024

  • Introduced two tests for the default_location function using pytest and pytest-mock.
  • Mocked API responses to verify function behavior under two scenarios: success, and failure.
  • Updated pyproject.toml to include pytest-mock as a dependency.
  • Regenerated 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:

  • Updated 'pyproject.toml' to include 'pytest-mock' as a dependency and regenerated 'poetry.lock' to reflect this change.

Tests:

  • Added unit tests for the 'default_location' function using 'pytest' and 'pytest-mock' to simulate API responses for success and failure scenarios.

- 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.
Copy link
Contributor

sourcery-ai bot commented Dec 1, 2024

Reviewer's Guide by Sourcery

The PR adds unit tests for the default_location() function by implementing mocked API responses using pytest-mock. The implementation uses parametrized testing to cover both successful and failed API response scenarios, while also adding the necessary testing dependencies to the project configuration.

Class diagram for test structure with mocked API response

classDiagram
    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
Loading

File-Level Changes

Change Details Files
Added parametrized unit tests for the default_location function with mocked API responses
  • Implemented test cases for successful API response (HTTP 200) and failed response (HTTP 400)
  • Used pytest.mark.parametrize to test multiple scenarios with different inputs and expected outputs
  • Mocked requests.get method to simulate API responses
  • Added assertions to verify both return values and correct API endpoint calls
tests/test_api.py
Updated project dependencies to support mocked testing
  • Added pytest-mock as a development dependency
  • Updated dependency lock file to reflect new changes
pyproject.toml
poetry.lock

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@github-actions github-actions bot left a 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! 🚀

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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)
Copy link
Contributor

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.

Copy link
Owner

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

@ryansurf
Copy link
Owner

ryansurf commented Dec 2, 2024

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! 🙇

@ryansurf ryansurf merged commit baac3f4 into ryansurf:main Dec 2, 2024
4 checks passed
@ryansurf
Copy link
Owner

ryansurf commented Dec 2, 2024

@all-contributors please add @latiffetahaj for code and unit tests

Copy link
Contributor

@ryansurf

I've put up a pull request to add @latiffetahaj! 🎉

Copy link

codecov bot commented Dec 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

see 1 file with indirect coverage changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants