From af7deb77953827d42ca9b886497948b3dd520d89 Mon Sep 17 00:00:00 2001 From: JenR8ed <1014672+JenR8ed@users.noreply.github.com> Date: Wed, 8 Apr 2026 19:49:47 +0000 Subject: [PATCH] Refactor validate_report.py to accept filepath as a parameter - Update validate() to take an optional filepath parameter - Add docstring to validate() for improved maintainability - Update test_validate_report.py with a new test for custom filepath - Ensure backward compatibility with default parameter value Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- test_validate_report.py | 10 +++++++++- validate_report.py | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/test_validate_report.py b/test_validate_report.py index 3ee29d8..eddfb1e 100644 --- a/test_validate_report.py +++ b/test_validate_report.py @@ -19,7 +19,7 @@ def test_validate_not_a_list(): assert str(excinfo.value) == "Root should be a JSON array" def test_validate_file_not_found(): - """Test validate() when testing_report.json is missing.""" + """Test validate() when file is missing.""" with patch("builtins.open", side_effect=FileNotFoundError): with pytest.raises(FileNotFoundError): validate() @@ -30,3 +30,11 @@ def test_validate_invalid_json(): with patch("builtins.open", mock_open(read_data=mock_data)): with pytest.raises(json.JSONDecodeError): validate() + +def test_validate_custom_filepath(): + """Test validate() with a custom filepath.""" + mock_data = json.dumps([]) + custom_path = "custom_report.json" + with patch("builtins.open", mock_open(read_data=mock_data)) as mocked_open: + validate(filepath=custom_path) + mocked_open.assert_called_once_with(custom_path, 'r') diff --git a/validate_report.py b/validate_report.py index 52fce78..9049656 100644 --- a/validate_report.py +++ b/validate_report.py @@ -1,7 +1,12 @@ import json -def validate(): - with open('testing_report.json', 'r') as f: +def validate(filepath='testing_report.json'): + """Validates that the given JSON file has a list at its root. + + Args: + filepath (str): The path to the JSON file to validate. + """ + with open(filepath, 'r') as f: data = json.load(f) assert isinstance(data, list), "Root should be a JSON array"