Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion test_validate_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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')
9 changes: 7 additions & 2 deletions validate_report.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import json

def validate():
with open('testing_report.json', 'r') as f:
def validate(filepath='testing_report.json'):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved code clarity and to enable static type checking, consider adding a type hint to the filepath parameter. This makes the function signature more explicit about the expected type and allows static analysis tools to catch potential bugs.

Suggested change
def validate(filepath='testing_report.json'):
def validate(filepath: str = 'testing_report.json'):
References
  1. PEP 484 introduced type hints to Python. While optional, using them is a modern best practice that improves code readability and allows for static analysis to catch type-related errors before runtime. (link)

"""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"