-
Notifications
You must be signed in to change notification settings - Fork 550
Increase test coverage #4393
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
Increase test coverage #4393
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
915b7af
test(utils): Add test for `datetime_from_isoformat`
mgaligniana cdfaac7
test(utils): Add test for `safe_str` when it fails
mgaligniana fa19927
test(attachments): Complete 100% coverage for `Attachment`
mgaligniana 0b57e7b
test(utils): Add test for `strip_string`
mgaligniana b46e6d1
test(utils): Add test for `_make_threadlocal_contextvars`
mgaligniana a2e9dd3
test(utils): Add test for `Dsn` validations
mgaligniana 98cf1fc
test(utils): Add test for `to_string`
mgaligniana 202caa3
test(utils): Add test for `get_default_release`
mgaligniana e4ccac4
test(utils): Add test for `filename_for_module`
mgaligniana 1b868c8
test(utils): Add test for `exc_info_from_error`
mgaligniana 8d7bfab
test(utils): Add test for `get_lines_from_file`
mgaligniana d066f03
test(utils): Add test for `sanitize_url`
mgaligniana 3d98364
test(utils): Add test for `package_version`
mgaligniana 4f7bfec
Merge branch 'master' into GH-3515
antonpirker 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,10 @@ | |
_get_installed_modules, | ||
_generate_installed_modules, | ||
ensure_integration_enabled, | ||
to_string, | ||
exc_info_from_error, | ||
get_lines_from_file, | ||
package_version, | ||
) | ||
|
||
|
||
|
@@ -61,55 +65,72 @@ def _normalize_distribution_name(name): | |
return re.sub(r"[-_.]+", "-", name).lower() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("input_str", "expected_output"), | ||
isoformat_inputs_and_datetime_outputs = ( | ||
( | ||
( | ||
"2021-01-01T00:00:00.000000Z", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), # UTC time | ||
( | ||
"2021-01-01T00:00:00.000000", | ||
datetime(2021, 1, 1).astimezone(timezone.utc), | ||
), # No TZ -- assume local but convert to UTC | ||
( | ||
"2021-01-01T00:00:00Z", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), # UTC - No milliseconds | ||
( | ||
"2021-01-01T00:00:00.000000+00:00", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2021-01-01T00:00:00.000000-00:00", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2021-01-01T00:00:00.000000+0000", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2021-01-01T00:00:00.000000-0000", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2020-12-31T00:00:00.000000+02:00", | ||
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=2))), | ||
), # UTC+2 time | ||
( | ||
"2020-12-31T00:00:00.000000-0200", | ||
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))), | ||
), # UTC-2 time | ||
( | ||
"2020-12-31T00:00:00-0200", | ||
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))), | ||
), # UTC-2 time - no milliseconds | ||
"2021-01-01T00:00:00.000000Z", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), # UTC time | ||
( | ||
"2021-01-01T00:00:00.000000", | ||
datetime(2021, 1, 1).astimezone(timezone.utc), | ||
), # No TZ -- assume local but convert to UTC | ||
( | ||
"2021-01-01T00:00:00Z", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), # UTC - No milliseconds | ||
( | ||
"2021-01-01T00:00:00.000000+00:00", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2021-01-01T00:00:00.000000-00:00", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2021-01-01T00:00:00.000000+0000", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2021-01-01T00:00:00.000000-0000", | ||
datetime(2021, 1, 1, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2020-12-31T00:00:00.000000+02:00", | ||
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=2))), | ||
), # UTC+2 time | ||
( | ||
"2020-12-31T00:00:00.000000-0200", | ||
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))), | ||
), # UTC-2 time | ||
( | ||
"2020-12-31T00:00:00-0200", | ||
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))), | ||
), # UTC-2 time - no milliseconds | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("input_str", "expected_output"), | ||
isoformat_inputs_and_datetime_outputs, | ||
) | ||
def test_datetime_from_isoformat(input_str, expected_output): | ||
assert datetime_from_isoformat(input_str) == expected_output, input_str | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("input_str", "expected_output"), | ||
isoformat_inputs_and_datetime_outputs, | ||
) | ||
def test_datetime_from_isoformat_with_py_36_or_lower(input_str, expected_output): | ||
""" | ||
`fromisoformat` was added in Python version 3.7 | ||
""" | ||
with mock.patch("sentry_sdk.utils.datetime") as datetime_mocked: | ||
datetime_mocked.fromisoformat.side_effect = AttributeError() | ||
datetime_mocked.strptime = datetime.strptime | ||
assert datetime_from_isoformat(input_str) == expected_output, input_str | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"env_var_value,strict,expected", | ||
[ | ||
|
@@ -346,6 +367,12 @@ def test_sanitize_url_and_split(url, expected_result): | |
assert sanitized_url.fragment == expected_result.fragment | ||
|
||
|
||
def test_sanitize_url_remove_authority_is_false(): | ||
url = "https://usr:[email protected]" | ||
sanitized_url = sanitize_url(url, remove_authority=False) | ||
assert sanitized_url == url | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("url", "sanitize", "expected_url", "expected_query", "expected_fragment"), | ||
[ | ||
|
@@ -629,6 +656,17 @@ def test_get_error_message(error, expected_result): | |
assert get_error_message(exc_value) == expected_result(exc_value) | ||
|
||
|
||
def test_safe_str_fails(): | ||
class ExplodingStr: | ||
def __str__(self): | ||
raise Exception | ||
|
||
obj = ExplodingStr() | ||
result = safe_str(obj) | ||
|
||
assert result == repr(obj) | ||
|
||
|
||
def test_installed_modules(): | ||
try: | ||
from importlib.metadata import distributions, version | ||
|
@@ -712,6 +750,20 @@ def test_default_release_empty_string(): | |
assert release is None | ||
|
||
|
||
def test_get_default_release_sentry_release_env(monkeypatch): | ||
monkeypatch.setenv("SENTRY_RELEASE", "sentry-env-release") | ||
assert get_default_release() == "sentry-env-release" | ||
|
||
|
||
def test_get_default_release_other_release_env(monkeypatch): | ||
monkeypatch.setenv("SOURCE_VERSION", "other-env-release") | ||
|
||
with mock.patch("sentry_sdk.utils.get_git_revision", return_value=""): | ||
release = get_default_release() | ||
|
||
assert release == "other-env-release" | ||
|
||
|
||
def test_ensure_integration_enabled_integration_enabled(sentry_init): | ||
def original_function(): | ||
return "original" | ||
|
@@ -973,3 +1025,55 @@ def test_function(): ... | |
sentry_sdk.utils.qualname_from_function(test_function) | ||
== "test_qualname_from_function_none_name.<locals>.test_function" | ||
) | ||
|
||
|
||
def test_to_string_unicode_decode_error(): | ||
class BadStr: | ||
def __str__(self): | ||
raise UnicodeDecodeError("utf-8", b"", 0, 1, "reason") | ||
|
||
obj = BadStr() | ||
result = to_string(obj) | ||
assert result == repr(obj)[1:-1] | ||
|
||
|
||
def test_exc_info_from_error_dont_get_an_exc(): | ||
class NotAnException: | ||
pass | ||
|
||
with pytest.raises(ValueError) as exc: | ||
exc_info_from_error(NotAnException()) | ||
|
||
assert "Expected Exception object to report, got <class" in str(exc.value) | ||
|
||
|
||
def test_get_lines_from_file_handle_linecache_errors(): | ||
expected_result = ([], None, []) | ||
|
||
class Loader: | ||
@staticmethod | ||
def get_source(module): | ||
raise IOError("something went wrong") | ||
|
||
result = get_lines_from_file("filename", 10, loader=Loader()) | ||
assert result == expected_result | ||
|
||
with mock.patch( | ||
"sentry_sdk.utils.linecache.getlines", | ||
side_effect=OSError("something went wrong"), | ||
): | ||
result = get_lines_from_file("filename", 10) | ||
assert result == expected_result | ||
|
||
lines = ["line1", "line2", "line3"] | ||
|
||
def fake_getlines(filename): | ||
return lines | ||
|
||
with mock.patch("sentry_sdk.utils.linecache.getlines", fake_getlines): | ||
result = get_lines_from_file("filename", 10) | ||
assert result == expected_result | ||
|
||
|
||
def test_package_version_is_none(): | ||
assert package_version("non_existent_package") is None |
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 |
---|---|---|
|
@@ -74,13 +74,26 @@ def test_filename(): | |
|
||
assert x("bogus", "bogus") == "bogus" | ||
|
||
assert x("bogus", "bogus.pyc") == "bogus.py" | ||
|
||
assert x("os", os.__file__) == "os.py" | ||
|
||
assert x("foo.bar", "path/to/foo/bar.py") == "path/to/foo/bar.py" | ||
|
||
import sentry_sdk.utils | ||
|
||
assert x("sentry_sdk.utils", sentry_sdk.utils.__file__) == "sentry_sdk/utils.py" | ||
|
||
|
||
def test_filename_module_file_is_none(): | ||
class DummyModule: | ||
__file__ = None | ||
|
||
os.sys.modules["foo"] = DummyModule() | ||
|
||
assert filename_for_module("foo.bar", "path/to/foo/bar.py") == "path/to/foo/bar.py" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"given,expected_envelope", | ||
[ | ||
|
@@ -120,6 +133,21 @@ def test_parse_invalid_dsn(dsn): | |
dsn = Dsn(dsn) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dsn,error_message", | ||
[ | ||
("foo://[email protected]", "Unsupported scheme 'foo'"), | ||
("https://foobar@", "Missing hostname"), | ||
("https://@sentry.io", "Missing public key"), | ||
], | ||
) | ||
def test_dsn_validations(dsn, error_message): | ||
with pytest.raises(BadDsn) as e: | ||
dsn = Dsn(dsn) | ||
|
||
assert str(e.value) == error_message | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"frame,in_app_include,in_app_exclude,project_root,resulting_frame", | ||
[ | ||
|
@@ -578,6 +606,17 @@ def test_failed_base64_conversion(input): | |
value="é...", metadata={"len": 8, "rem": [["!limit", "x", 2, 5]]} | ||
), | ||
], | ||
[ | ||
"\udfff\udfff\udfff\udfff\udfff\udfff", | ||
5, | ||
AnnotatedValue( | ||
value="\udfff\udfff...", | ||
metadata={ | ||
"len": 6, | ||
"rem": [["!limit", "x", 5 - 3, 5]], | ||
}, | ||
), | ||
], | ||
], | ||
) | ||
def test_strip_string(input, max_length, result): | ||
|
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.
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.
🎗️ For some reason when I run it alone the coverage is 100%. When I run the whole test suite I get 95%
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.
🔍 👣 Found it. Couple of tests fail running the entire suite