-
Notifications
You must be signed in to change notification settings - Fork 49
test(metrics): add coverage for metrics timing decorator #1003
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
Open
mnadzam
wants to merge
1
commit into
python-wheel-build:main
Choose a base branch
from
mnadzam:tests_metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| import pytest | ||
| from packaging.requirements import Requirement | ||
| from packaging.version import Version | ||
|
|
||
| from fromager import context, metrics | ||
|
|
||
|
|
||
| @metrics.timeit(description="test description") | ||
| def _test_func( | ||
| *, | ||
| ctx: context.WorkContext, | ||
| req: Requirement | None = None, | ||
| version: str | None = None, | ||
| ) -> str: | ||
| return "ok" | ||
|
|
||
|
|
||
| @metrics.timeit(description="test description") | ||
| def _test_returns_version( | ||
| *, | ||
| ctx: context.WorkContext, | ||
| req: Requirement | None = None, | ||
| ) -> tuple[str, Version]: | ||
| return ("http://example.com", Version("1.2.3")) | ||
|
|
||
|
|
||
| @metrics.timeit(description="test description") | ||
| def _test_raises( | ||
| *, | ||
| ctx: context.WorkContext, | ||
| req: Requirement | None = None, | ||
| version: str | None = None, | ||
| ) -> None: | ||
| raise RuntimeError("test error") | ||
|
|
||
|
|
||
| def test_timeit_stores_timing(tmp_context: context.WorkContext) -> None: | ||
| req = Requirement("numpy>=1.0") | ||
|
|
||
| _test_func(ctx=tmp_context, req=req, version="1.26.0") | ||
|
|
||
| key = "numpy==1.26.0" | ||
| assert key in tmp_context.time_store | ||
| assert tmp_context.time_store[key]["_test_func"] > 0 | ||
|
|
||
|
|
||
| def test_timeit_stores_description(tmp_context: context.WorkContext) -> None: | ||
| _test_func(ctx=tmp_context) | ||
|
|
||
| assert tmp_context.time_description_store["_test_func"] == "test description" | ||
|
|
||
|
|
||
| def test_timeit_no_storage_without_req(tmp_context: context.WorkContext) -> None: | ||
| _test_func(ctx=tmp_context, req=None, version="1.0") | ||
|
|
||
| assert len(tmp_context.time_store) == 0 | ||
|
|
||
|
|
||
| def test_timeit_no_storage_without_version(tmp_context: context.WorkContext) -> None: | ||
| req = Requirement("numpy>=1.0") | ||
|
|
||
| _test_func(ctx=tmp_context, req=req) | ||
|
|
||
| assert len(tmp_context.time_store) == 0 | ||
|
|
||
|
|
||
| def test_timeit_returns_original_result(tmp_context: context.WorkContext) -> None: | ||
| result = _test_func(ctx=tmp_context) | ||
|
|
||
| assert result == "ok" | ||
|
|
||
|
|
||
| def test_timeit_extracts_version_from_return( | ||
| tmp_context: context.WorkContext, | ||
| ) -> None: | ||
| req = Requirement("mypkg") | ||
|
|
||
| _test_returns_version(ctx=tmp_context, req=req) | ||
|
|
||
| assert "mypkg==1.2.3" in tmp_context.time_store | ||
|
|
||
|
|
||
| def test_timeit_propagates_exception(tmp_context: context.WorkContext) -> None: | ||
| with pytest.raises(RuntimeError, match="test error"): | ||
| _test_raises(ctx=tmp_context) | ||
|
|
||
|
|
||
| def test_timeit_no_storage_on_exception(tmp_context: context.WorkContext) -> None: | ||
| req = Requirement("numpy>=1.0") | ||
|
|
||
| with pytest.raises(RuntimeError): | ||
| _test_raises(ctx=tmp_context, req=req, version="1.0") | ||
|
|
||
| assert len(tmp_context.time_store) == 0 | ||
|
|
||
|
|
||
| def test_summarize_logs_timing( | ||
| tmp_context: context.WorkContext, | ||
| caplog: pytest.LogCaptureFixture, | ||
| ) -> None: | ||
| req = Requirement("numpy>=1.0") | ||
| _test_func(ctx=tmp_context, req=req, version="1.26.0") | ||
|
|
||
| with caplog.at_level(logging.INFO, logger="fromager.metrics"): | ||
| metrics.summarize(tmp_context, "Building") | ||
|
|
||
| records = [r for r in caplog.records if r.name == "fromager.metrics"] | ||
| assert len(records) == 1 | ||
| msg = records[0].message | ||
| assert "Building" in msg | ||
| assert "numpy==1.26.0" in msg | ||
| assert "test description" in msg | ||
|
|
||
|
|
||
| def test_summarize_empty( | ||
| tmp_context: context.WorkContext, | ||
| caplog: pytest.LogCaptureFixture, | ||
| ) -> None: | ||
| with caplog.at_level(logging.INFO, logger="fromager.metrics"): | ||
| metrics.summarize(tmp_context, "Building") | ||
|
|
||
| assert len(caplog.records) == 0 | ||
|
|
||
|
|
||
| def test_extract_version_from_tuple() -> None: | ||
| ret = ("http://example.com", Version("2.0.0")) | ||
| assert metrics._extract_version_from_return(ret) == Version("2.0.0") | ||
|
|
||
|
|
||
| def test_extract_version_bare() -> None: | ||
| ret = Version("3.0.0") | ||
| assert metrics._extract_version_from_return(ret) == Version("3.0.0") | ||
|
|
||
|
|
||
| def test_extract_version_no_version_in_iterable() -> None: | ||
| ret = ("http://example.com", "not-a-version") | ||
| assert metrics._extract_version_from_return(ret) is None | ||
|
|
||
|
|
||
| def test_extract_version_non_iterable() -> None: | ||
| assert metrics._extract_version_from_return(42) is None | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_extract_version_none() -> None: | ||
| assert metrics._extract_version_from_return(None) is None | ||
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.