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

Replace request mocks with pook in tests #2783

Merged
merged 5 commits into from
Aug 15, 2023

Conversation

ashiramin
Copy link
Contributor

@ashiramin ashiramin commented Aug 6, 2023

Fixes

Part of #2256 by @sarayourfriend (cannot close the issue to fully remove requests mocks due to h2non/pook#55, described below).

Description

Replaced request mocks with pook in the following files:

  • test_slack.py (ingestion server)
  • test_image_proxy.py

Had to add pook in ingestion_server so also updated Pipfile

I couldn't update tests in the following files because pook doesn't support binary response. Please see here

  • test_watermark
  • test_waveform
  • test_image_views

Testing Instructions

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (if applicable).

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@ashiramin ashiramin requested a review from a team as a code owner August 6, 2023 03:05
@ashiramin ashiramin requested review from krysal and obulat August 6, 2023 03:05
@openverse-bot openverse-bot added the 🚦 status: awaiting triage Has not been triaged & therefore, not ready for work label Aug 6, 2023
@ashiramin ashiramin mentioned this pull request Aug 6, 2023
5 tasks
Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start, and exciting! I've opened a PR in pook to add support for binary bodies:

h2non/pook#88

I've left some initial requests for changes.

Comment on lines 451 to 458
pook.on()
pook.head(image_url, reply=200, response_headers=headers)
with pytest.raises(UnsupportedMediaType):
photon_get(image_url, image.identifier)

key = f"media:{image.identifier}:thumb_type"
assert redis.get(key) == expected_cache_val
pook.off()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the pook.on decorator for unit tests, otherwise pook won't get turned off properly after the test runs if any of the assertions or code-paths fail (causing pook to leak into other tests that might not expect it).

Although, IIRC, we can't use the decorator here because we need pook to avoid intercepting the Elasticsearch client's HTTP requests in ImageFactory.create. In that case, you can see some fixtures I wrote to handle this case for a PR that explored other changes: https://github.com/WordPress/openverse/blob/980ef96e0e16fc758f78be9f2aadf595f75948f6/api/test/unit/conftest.py#L144C1-L171

To use those fixtures, add then to conftest.py as they are in the linked commit and then add pook_off to this test and call pook.on when needed.

Alternatively, use pook.use as a context manager around photon_get: https://pook.readthedocs.io/en/latest/examples.html#context-manager-for-isolated-mocking

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for providing the detailed answer and good call on pook not getting turned off properly in case of failure. I'll make the updates soon :)

Comment on lines 51 to 62
pook.on()
if webhook:
mock_post = pook.post(webhook)
slack._message(text, summary)
assert mock_post.called == should_alert
if not should_alert:
return
data = mock_post.call_args.kwargs["json"]
assert data["blocks"][0]["text"]["text"] == text
assert data["text"] == expected_summary
if environment:
assert data["username"].endswith(environment.upper())
if should_alert:
assert mock_post.calls > 0
data = json.loads(mock_post.matches[0].body)
assert data["blocks"][0]["text"]["text"] == text
assert data["text"] == expected_summary
if environment:
assert data["username"].endswith(environment.upper())
pook.off()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the other comment, we need pook to turn off safely if the test fails. Either use a context manager, the decorator, or the fixtures I shared.

@sarayourfriend sarayourfriend added 🟩 priority: low Low priority and doesn't need to be rushed 💻 aspect: code Concerns the software code in the repository 🧰 goal: internal improvement Improvement that benefits maintainers, not users 🧱 stack: api Related to the Django API 🧱 stack: ingestion server Related to the ingestion/data refresh server and removed 🚦 status: awaiting triage Has not been triaged & therefore, not ready for work labels Aug 7, 2023
@openverse-bot
Copy link
Collaborator

Based on the low urgency of this PR, the following reviewers are being gently reminded to review this PR:

@krysal
@obulat
This reminder is being automatically generated due to the urgency configuration.

Excluding weekend1 days, this PR was ready for review 5 day(s) ago. PRs labelled with low urgency are expected to be reviewed within 5 weekday(s)2.

@ashiramin, if this PR is not ready for a review, please draft it to prevent reviewers from getting further unnecessary pings.

Footnotes

  1. Specifically, Saturday and Sunday.

  2. For the purpose of these reminders we treat Monday - Friday as weekdays. Please note that the operation that generates these reminders runs at midnight UTC on Monday - Friday. This means that depending on your timezone, you may be pinged outside of the expected range.

@sarayourfriend sarayourfriend marked this pull request as draft August 14, 2023 00:19
@sarayourfriend
Copy link
Collaborator

Drafting to avoid re-pings. @ashiramin please mark the PR ready for review once you've made the requested changes 🙏

@ashiramin ashiramin marked this pull request as ready for review August 15, 2023 03:49
@ashiramin
Copy link
Contributor Author

@sarayourfriend I made the changes. Decided to go with context manager. Let me know what you think

Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sarayourfriend
Copy link
Collaborator

Thank you! Approved 👍 I've updated the PR description to prevent the issue from closing. We'll update the issue to be blocked on the related pook issue.

Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution, @ashiramin!

@obulat obulat merged commit 312c9f8 into WordPress:main Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🧰 goal: internal improvement Improvement that benefits maintainers, not users 🟩 priority: low Low priority and doesn't need to be rushed 🧱 stack: api Related to the Django API 🧱 stack: ingestion server Related to the ingestion/data refresh server
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants