Repository navigation
Remove unnecessary wrappers from _sentry_monitoring #76
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,7 @@ | ||
| import pytest | ||
| import sentry_sdk | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def sentry_init(): | ||
| sentry_sdk.init(dsn=None, enable_tracing=False) |
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
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 |
|---|---|---|
|
|
@@ -5,70 +5,54 @@ | |
| from psrt_ghsa_bot import _sentry_monitoring as sm | ||
| from psrt_ghsa_bot import app | ||
|
|
||
| DSN = "https://[email protected]/0" | ||
|
|
||
|
|
||
| def test_noop_without_dsn(monkeypatch) -> None: | ||
| monkeypatch.delenv("SENTRY_DSN", raising=False) | ||
| with mock.patch.object(sm.sentry_sdk, "init") as init: | ||
| sm.init_sentry() | ||
| init.assert_not_called() | ||
|
|
||
| with mock.patch.object(sm.crons, "capture_checkin") as capture: | ||
| result = sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK) | ||
| assert result is None | ||
| capture.assert_not_called() | ||
|
|
||
|
|
||
| def test_init_with_dsn(monkeypatch) -> None: | ||
| monkeypatch.setenv("SENTRY_DSN", DSN) | ||
| @pytest.mark.parametrize("dsn", [None, "https://[email protected]/0"]) | ||
| def test_init_with_dsn(monkeypatch, dsn) -> None: | ||
| if dsn: | ||
| monkeypatch.setenv("SENTRY_DSN", dsn) | ||
| else: | ||
| monkeypatch.delenv("SENTRY_DSN", raising=False) | ||
| with mock.patch.object(sm.sentry_sdk, "init") as init: | ||
| sm.init_sentry() | ||
| init.assert_called_once() | ||
| assert init.call_args.kwargs["dsn"] == DSN | ||
|
|
||
|
|
||
| def test_capture_checkin_with_dsn(monkeypatch) -> None: | ||
| monkeypatch.setenv("SENTRY_DSN", DSN) | ||
| with mock.patch.object(sm.crons, "capture_checkin", return_value="stanstan") as capture: | ||
| result = sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK, duration=1.5, check_in_id="stanstan") | ||
| assert result == "stanstan" | ||
| capture.assert_called_once_with( | ||
| monitor_slug=sm.MONITOR_SLUG_GHSA, | ||
| status=sm.STATUS_OK, | ||
| duration=1.5, | ||
| check_in_id="stanstan", | ||
| ) | ||
|
|
||
|
|
||
| def test_capture_checkin_swallow_sdk_errors(monkeypatch) -> None: | ||
| monkeypatch.setenv("SENTRY_DSN", DSN) | ||
| with mock.patch.object(sm.crons, "capture_checkin", side_effect=AttributeError): | ||
| assert sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK) is None | ||
| assert init.call_args.kwargs["dsn"] == dsn | ||
|
|
||
|
|
||
| def test_main_reports_ok_on_success() -> None: | ||
| with ( | ||
| mock.patch("psrt_ghsa_bot.app.init_sentry") as init, | ||
| mock.patch("psrt_ghsa_bot.app.run") as run, | ||
| mock.patch("psrt_ghsa_bot.app.capture_checkin", return_value="cid") as capture, | ||
| mock.patch("sentry_sdk.crons.capture_checkin", return_value="cid") as capture, | ||
| ): | ||
| app.main() | ||
|
|
||
| init.assert_called_once() | ||
| run.assert_called_once() | ||
| assert [c.args[1] for c in capture.call_args_list] == [app.STATUS_IN_PROGRESS, app.STATUS_OK] | ||
| assert [c.kwargs["status"] for c in capture.call_args_list] == [sm.STATUS_IN_PROGRESS, sm.STATUS_OK] | ||
| assert capture.call_args_list[-1].kwargs["check_in_id"] == "cid" | ||
|
|
||
|
|
||
| def test_main_reports_error_and_reraises() -> None: | ||
| with ( | ||
| mock.patch("psrt_ghsa_bot.app.init_sentry"), | ||
| mock.patch("psrt_ghsa_bot.app.run", side_effect=RuntimeError("uh oh")), | ||
| mock.patch("psrt_ghsa_bot.app.capture_checkin", return_value="cid") as capture, | ||
| mock.patch("sentry_sdk.crons.capture_checkin", return_value="cid") as capture, | ||
| ): | ||
| with pytest.raises(RuntimeError, match="uh oh"): | ||
| app.main() | ||
|
|
||
| assert [c.args[1] for c in capture.call_args_list] == [app.STATUS_IN_PROGRESS, app.STATUS_ERROR] | ||
| assert [c.kwargs["status"] for c in capture.call_args_list] == [sm.STATUS_IN_PROGRESS, sm.STATUS_ERROR] | ||
| assert capture.call_args_list[-1].kwargs["check_in_id"] == "cid" | ||
|
|
||
|
|
||
| def test_main_runs_on_checkin_error() -> None: | ||
| with ( | ||
| mock.patch("psrt_ghsa_bot.app.init_sentry") as init, | ||
| mock.patch("psrt_ghsa_bot.app.run") as run, | ||
| mock.patch("sentry_sdk.crons.capture_checkin", side_effect=AttributeError) as capture, | ||
| ): | ||
| app.main() | ||
|
|
||
| init.assert_called_once() | ||
| run.assert_called_once() | ||
| capture.assert_called_once() | ||
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.
Uh oh!
There was an error while loading. Please reload this page.