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

Proposal: Writing unit tests for api/ #506

Open
2 tasks
thesujai opened this issue Aug 8, 2024 · 7 comments
Open
2 tasks

Proposal: Writing unit tests for api/ #506

thesujai opened this issue Aug 8, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@thesujai
Copy link
Contributor

thesujai commented Aug 8, 2024

Why do we need this ?

If I can correctly infer the backend/ directory structure, I observe tests/ directory is structured to give tests as per the mhq/ dir. I only see tests for service/. No api tests are there.
Tests looks like good-to-have but it is a critical part of code imo.

Acceptance Criteria

  • Configure test client
  • Write unit tests

Further Comments / References

I have some good experience writing tests(not more than a year though:)). I can provide pretty good coverage for this.

@thesujai thesujai added the enhancement New feature or request label Aug 8, 2024
@thesujai
Copy link
Contributor Author

thesujai commented Aug 8, 2024

@jayantbh cc

@jayantbh
Copy link
Contributor

jayantbh commented Aug 8, 2024

Thank you for opening the issue, @thesujai. :)
I agree. We've not been doing great in the testing area, but are catching up on it.

Any help is welcome. :)

@jayantbh jayantbh added good first issue Good for newcomers help wanted Extra attention is needed labels Aug 8, 2024
@thesujai
Copy link
Contributor Author

thesujai commented Aug 8, 2024

I am taking this on, you can assign me.

I will create one PR for a .py file in api/(or adjust it not letting a PR be too long and non-reviewable).

@samad-yar-khan
Copy link
Contributor

@thesujai Would like to know how you plan to achieve this before you start working on this. Although the current tests are not for APIs, they are for most of the core analytical functions that are used across the code base and in the APIs. API tests do make sense, would love to know your approach.

@jayantbh
Copy link
Contributor

jayantbh commented Aug 8, 2024

@thesujai ^ would like to know that.

@thesujai
Copy link
Contributor Author

thesujai commented Aug 8, 2024

@jayantbh @samad-yar-khan
With my approach, we would first create a test_client (This will ensure that the main application is not affected by API requests, nor will we have to keep running the backend server for the test period).
Flask and pytest allow us to create a test client like below:

@pytest.fixture
def client(self):
        app = Flask(__name__)
        app.testing = True
        with app.test_client() as client:
            yield client

Now for each API endpoint we will use this client to make requests, and match the actual response with the expected response.

For example if we take the endpoint /teams/<team_id>/lead_time/prs we would write test like this:

test_get_lead_time_prs(some_mocks):
   # we setup the mocks here, like what should some of the function return when it is an actual API call
   # And we also define expected output, based on how we mock
   # then make a call like below
   response = client.get(
            f"/teams/team1/lead_time/prs",
            query_string={
                "from_time": from_time,
                "to_time": to_time,
                "pr_filter": json.dumps({"key": "value"})
            }
        )
   assert response.status_code = 200
   assert response.get_json() = expected_output

The above one I showed is a success case. There can be some 400, or 500 cases which also need to be checked. So for them, I will be asserting, for a bad request, if the server responding with an error(Or is the server behaving unexpectedly)

One thing I can say is writing tests won't be as straightforward as it looks, lots of API endpoints are a little tricky and would require some complex mocking. Anyway, I would try to do some actual data testing(Avoiding mocks in someplace).

@thesujai
Copy link
Contributor Author

thesujai commented Aug 8, 2024

Please let me know what you think about this approach, open to suggestions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants