-
Notifications
You must be signed in to change notification settings - Fork 0
π§ͺ [Add test coverage for _save_best_model_to_disk] #379
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,41 @@ | ||||||||||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||||||||||
| from unittest.mock import patch, mock_open | ||||||||||||||||||||||||||||||||||||||||
| from router import main | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def test_save_best_model_to_disk_success(): | ||||||||||||||||||||||||||||||||||||||||
| best_model = {"model_name": "agent-gemma", "score": 100} | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| mock_file = mock_open() | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| with patch("builtins.open", mock_file), \ | ||||||||||||||||||||||||||||||||||||||||
| patch("json.dump") as mock_json_dump, \ | ||||||||||||||||||||||||||||||||||||||||
| patch("datetime.datetime") as mock_datetime: | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # Setup mock datetime | ||||||||||||||||||||||||||||||||||||||||
| mock_now = mock_datetime.now.return_value | ||||||||||||||||||||||||||||||||||||||||
| mock_now.isoformat.return_value = "2023-10-25T12:00:00+00:00" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| main._save_best_model_to_disk(best_model) | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π― Functional Correctness | π‘ Minor | β‘ Quick win Assert that the timestamp is generated in UTC. The test verifies the Suggested assertion+import datetime
+
...
main._save_best_model_to_disk(best_model)
+ mock_datetime.now.assert_called_once_with(datetime.timezone.utc)π Committable suggestion
Suggested change
π€ Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # Verify file opened correctly | ||||||||||||||||||||||||||||||||||||||||
| mock_file.assert_called_once_with("/config/router_dir/best_free_model.json", "w") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # Verify json.dump called with correct payload | ||||||||||||||||||||||||||||||||||||||||
| expected_payload = { | ||||||||||||||||||||||||||||||||||||||||
| "model_name": "agent-gemma", | ||||||||||||||||||||||||||||||||||||||||
| "score": 100, | ||||||||||||||||||||||||||||||||||||||||
| "updated_at": "2023-10-25T12:00:00Z" | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| mock_json_dump.assert_called_once_with(expected_payload, mock_file(), indent=2) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def test_save_best_model_to_disk_exception_handled(): | ||||||||||||||||||||||||||||||||||||||||
| best_model = {"model_name": "agent-gemma"} | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| mock_file = mock_open() | ||||||||||||||||||||||||||||||||||||||||
| mock_file.side_effect = PermissionError("Cannot write to disk") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # Should not raise exception | ||||||||||||||||||||||||||||||||||||||||
| with patch("builtins.open", mock_file): | ||||||||||||||||||||||||||||||||||||||||
| main._save_best_model_to_disk(best_model) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| mock_file.assert_called_once_with("/config/router_dir/best_free_model.json", "w") | ||||||||||||||||||||||||||||||||||||||||
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.
suggestion (testing): Strengthen the exception-path test and consider additional error conditions.
The PermissionError test only asserts that
openwas called; pytest will fail on unexpected exceptions, but it would be clearer to assert the functionβs return value (e.g.,None) or thatjson.dumpis not called. If_save_best_model_to_diskis meant to handle other filesystem errors (such asFileNotFoundErrororOSError), consider parametrized tests for these cases to ensure all error paths are explicitly covered.Suggested implementation:
_save_best_model_to_diskdoes not currently returnNoneon error, update its implementation accordingly or adjust the assertion to match the intended return value._save_best_model_to_diskis actually catching and handlingPermissionError,FileNotFoundError, andOSError; if not, add the appropriatetry/exceptblocks inrouter/main.pyso these tests pass.