Repository navigation
demo: decode running typeId in get_personal_record's interactive display #425
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
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 |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| """Regression tests for demo.py's personal-records interactive helper.""" | ||
|
|
||
| import sys | ||
| from types import SimpleNamespace | ||
|
|
||
| sys.modules.setdefault("readchar", SimpleNamespace(readkey=lambda: "q")) # type: ignore[arg-type] | ||
|
|
||
| import demo # noqa: E402 | ||
|
|
||
|
|
||
| class _FakeAPI: | ||
| def __init__(self): | ||
| self.records = [ | ||
| { | ||
| "typeId": 4, | ||
| "activityType": "running", | ||
| "value": 2400, | ||
| "activityId": 111, | ||
| }, | ||
| {"typeId": 7, "activityType": "running", "value": 5000}, | ||
| {"typeId": 12, "activityType": None, "value": 26246.0}, | ||
| {"typeId": 15, "activityType": None, "value": 1.0}, | ||
| {"typeId": 1, "activityType": "cycling", "value": 999}, | ||
| {"typeId": 99, "activityType": None, "value": 42.0}, | ||
| ] | ||
| self.get_activity_calls: list[str] = [] | ||
|
|
||
| def get_personal_record(self): | ||
| return self.records | ||
|
|
||
| def get_activity(self, activity_id): | ||
| self.get_activity_calls.append(activity_id) | ||
| return {"activityId": activity_id} | ||
|
|
||
|
|
||
| def _typed_inputs(*values): | ||
| it = iter(values) | ||
| return lambda prompt="": next(it) | ||
|
|
||
|
|
||
| def test_decodes_running_time_record(monkeypatch, capsys): | ||
| api = _FakeAPI() | ||
| monkeypatch.setattr("builtins.input", _typed_inputs("")) | ||
|
|
||
| demo.get_personal_records_data(api) | ||
|
|
||
| out = capsys.readouterr().out | ||
| assert "10 km — 40:00" in out | ||
| assert "typeId=4, raw value=2400" in out | ||
|
|
||
|
|
||
| def test_decodes_running_distance_record(monkeypatch, capsys): | ||
| api = _FakeAPI() | ||
| monkeypatch.setattr("builtins.input", _typed_inputs("")) | ||
|
|
||
| demo.get_personal_records_data(api) | ||
|
|
||
| out = capsys.readouterr().out | ||
| assert "Longest run — 5.00 km" in out | ||
|
|
||
|
|
||
| def test_decodes_step_count_record(monkeypatch, capsys): | ||
| api = _FakeAPI() | ||
| monkeypatch.setattr("builtins.input", _typed_inputs("")) | ||
|
|
||
| demo.get_personal_records_data(api) | ||
|
|
||
| out = capsys.readouterr().out | ||
| assert "Most steps in a day — 26,246" in out | ||
|
|
||
|
|
||
| def test_decodes_goal_streak_singular_day(monkeypatch, capsys): | ||
| api = _FakeAPI() | ||
| monkeypatch.setattr("builtins.input", _typed_inputs("")) | ||
|
|
||
| demo.get_personal_records_data(api) | ||
|
|
||
| out = capsys.readouterr().out | ||
| assert "Longest goal streak — 1 day " in out | ||
| assert "1 days" not in out | ||
|
|
||
|
|
||
| def test_unconfirmed_types_are_shown_not_hidden(monkeypatch, capsys): | ||
| """Records outside both confirmed typeId tables must still be printed | ||
| in full (typeId + raw value), not dropped or given a made-up label. | ||
| """ | ||
| api = _FakeAPI() | ||
| monkeypatch.setattr("builtins.input", _typed_inputs("")) | ||
|
|
||
| demo.get_personal_records_data(api) | ||
|
|
||
| out = capsys.readouterr().out | ||
| assert ( | ||
| "Unconfirmed record type (typeId=1, activityType='cycling', raw value=999)" | ||
| in out | ||
| ) | ||
| assert ( | ||
| "Unconfirmed record type (typeId=99, activityType=None, raw value=42.0)" in out | ||
| ) | ||
|
|
||
|
|
||
| def test_looks_up_source_activity_by_index(monkeypatch): | ||
| api = _FakeAPI() | ||
| monkeypatch.setattr("builtins.input", _typed_inputs("0")) | ||
|
|
||
| demo.get_personal_records_data(api) | ||
|
|
||
| assert api.get_activity_calls == ["111"] | ||
|
|
||
|
|
||
| def test_negative_index_is_rejected(monkeypatch, capsys): | ||
| api = _FakeAPI() | ||
| monkeypatch.setattr("builtins.input", _typed_inputs("-1")) | ||
|
|
||
| demo.get_personal_records_data(api) | ||
|
|
||
| assert api.get_activity_calls == [] | ||
| assert "Invalid index" in capsys.readouterr().out | ||
|
|
||
|
|
||
| def test_entry_without_activity_id_is_reported(monkeypatch, capsys): | ||
| api = _FakeAPI() | ||
| monkeypatch.setattr("builtins.input", _typed_inputs("1")) | ||
|
|
||
| demo.get_personal_records_data(api) | ||
|
|
||
| assert api.get_activity_calls == [] | ||
| assert "no activityId" in capsys.readouterr().out |
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.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Show the empty-record message for a successful empty response.
Line 3520 returns when
recordsis[]. This makes theif not entriesbranch at lines 3524-3526 unreachable. Return only when the API call fails, then let an empty list reach the existing message.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents