-
Notifications
You must be signed in to change notification settings - Fork 1
Chore: [ELI-751] - Error handling #227
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
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fe840ad
Test SSH signed commit
feyisayo-afolabi-nhs fa55341
Merge remote-tracking branch 'origin/main'
feyisayo-afolabi-nhs 30f0738
Merge remote-tracking branch 'origin/main'
feyisayo-afolabi-nhs aa19cca
CHORE: [ELI-751] Added Error handling.
feyisayo-afolabi-nhs f65987a
CHORE: [ELI-751] Added Error handling performance.
feyisayo-afolabi-nhs b6b8d8f
CHORE: [ELI-751] Added Error handling locust.
feyisayo-afolabi-nhs b6d540c
Merge branch 'main' into feature/Eli-751-error-handling
feyisayo-afolabi-nhs 62a35e5
CHORE: [ELI-751] Added Error handling final.
feyisayo-afolabi-nhs e90c862
CHORE: [ELI-751] Added Error handling test passing.
feyisayo-afolabi-nhs 71460b5
Merge branch 'main' into feature/Eli-751-error-handling
feyisayo-afolabi-nhs 2fc4c81
CHORE: [ELI-751] fixed Quality Gate issues.
feyisayo-afolabi-nhs 750f204
CHORE: [ELI-751] fixed Quality Gate issues.
feyisayo-afolabi-nhs fecbbc1
CHORE: [ELI-751] fixed Quality Gate issues.
feyisayo-afolabi-nhs a94edc4
CHORE: [ELI-751] fixed data_helper.
feyisayo-afolabi-nhs ab1e34e
Merge branch 'main' into feature/Eli-751-error-handling
feyisayo-afolabi-nhs 3f67292
CHORE: [ELI-751] fixed data_helper fixed sonarqube.
feyisayo-afolabi-nhs a355606
CHORE: [ELI-751] fixed PR comments.
feyisayo-afolabi-nhs 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
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
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,136 @@ | ||
| import os | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| from botocore.exceptions import ClientError | ||
|
|
||
| from utils.data_helper import load_all_expected_responses, load_all_test_scenarios | ||
| from utils.dynamo_helper import file_backup_exists | ||
| from utils.eligibility_api_client import EligibilityApiClient | ||
| from tests.performance_tests.validate_inputs import parse_run_time_to_seconds | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 1. data_helper.py — load_all_expected_responses | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_load_all_expected_responses_skips_bad_json(tmp_path): | ||
| """A corrupt JSON file is skipped; valid files in the same folder still load.""" | ||
| (tmp_path / "bad.json").write_text("NOT JSON", encoding="utf-8") | ||
| (tmp_path / "good.json").write_text('{"id": "123", "data": "ok"}', encoding="utf-8") | ||
|
|
||
| result = load_all_expected_responses(tmp_path) | ||
|
|
||
| assert "good.json" in result | ||
| assert "bad.json" not in result | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 2. data_helper.py — load_all_test_scenarios | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_load_all_test_scenarios_skips_missing_data_key(tmp_path): | ||
| """A scenario file that has no 'data' key is skipped; the result is an empty dict. | ||
|
|
||
| The TemplateEngine is mocked so that no real template files are needed. | ||
| Its apply() return value is configured to return the input unchanged so | ||
| that placeholder resolution does not fail if the 'data' key were present. | ||
| """ | ||
| (tmp_path / "scenario.json").write_text('{"other_key": "value"}', encoding="utf-8") | ||
|
|
||
| mock_engine = MagicMock() | ||
| mock_engine.apply.side_effect = lambda data: data # pass-through | ||
|
|
||
| with patch("utils.data_helper.TemplateEngine.create", return_value=mock_engine): | ||
| result = load_all_test_scenarios(tmp_path) | ||
|
|
||
| assert result == {} | ||
|
|
||
|
|
||
| def test_load_all_test_scenarios_skips_bad_json(tmp_path): | ||
| """A scenario file with invalid JSON is skipped; the result is an empty dict.""" | ||
| (tmp_path / "scenario.json").write_text("{ bad json", encoding="utf-8") | ||
|
|
||
| mock_engine = MagicMock() | ||
| mock_engine.apply.side_effect = lambda data: data | ||
|
|
||
| with patch("utils.data_helper.TemplateEngine.create", return_value=mock_engine): | ||
| result = load_all_test_scenarios(tmp_path) | ||
|
|
||
| assert result == {} | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 3. dynamo_helper.py — file_backup_exists | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @patch("utils.dynamo_helper.load_from_file") | ||
| def test_file_backup_exists_catches_json_decode_error(mock_load_from_file): | ||
| """file_backup_exists returns False (not an exception) when a backup file is corrupted.""" | ||
| # All load_from_file calls return a string that is not valid JSON | ||
| mock_load_from_file.return_value = "NOT JSON" | ||
|
|
||
| mock_db = MagicMock() | ||
| mock_db.environment = "test" | ||
|
|
||
| result = file_backup_exists(mock_db) | ||
|
|
||
| assert result is False | ||
|
|
||
|
|
||
| @patch.dict(os.environ, {"BASE_URL": "http://localhost"}, clear=True) | ||
| @patch("utils.eligibility_api_client.boto3.client") | ||
| def test_api_client_handles_ssm_client_error(mock_boto_client): | ||
| """A boto3 ClientError from SSM is wrapped in a descriptive RuntimeError.""" | ||
| mock_ssm = MagicMock() | ||
| error_response = { | ||
| "Error": {"Code": "AccessDeniedException", "Message": "Access Denied"} | ||
| } | ||
| mock_ssm.get_parameter.side_effect = ClientError(error_response, "GetParameter") | ||
| mock_boto_client.return_value = mock_ssm | ||
|
|
||
| # Force _ensure_certs_present to think no certs exist on disk | ||
| with patch("pathlib.Path.exists", return_value=False): | ||
| with pytest.raises(RuntimeError, match="Error retrieving .* from SSM"): | ||
| EligibilityApiClient() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 4. validate_inputs.py — parse_run_time_to_seconds and main() | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_parse_run_time_to_seconds_valid(): | ||
| """Seconds and minutes are parsed correctly.""" | ||
| assert parse_run_time_to_seconds("30s") == 30 | ||
| assert parse_run_time_to_seconds("5m") == 300 | ||
|
|
||
|
|
||
| @patch("tests.performance_tests.validate_inputs.fail") | ||
| def test_parse_run_time_to_seconds_invalid(mock_fail): | ||
| """An unsupported unit calls fail() with the right message and returns 0 (not an exception).""" | ||
| result = parse_run_time_to_seconds("10h") | ||
|
|
||
| mock_fail.assert_called_once_with( | ||
| "Invalid run_time format", | ||
| "run_time must look like 30s or 5m (seconds or minutes only). Got: '10h'", | ||
| ) | ||
| # After fail() is mocked (no sys.exit), the function returns the sentinel 0 | ||
| assert result == 0 | ||
|
|
||
|
|
||
| @patch("tests.performance_tests.validate_inputs.get_env") | ||
| @patch("tests.performance_tests.validate_inputs.fail") | ||
| def test_validate_inputs_main_catches_value_error(mock_fail, mock_get_env): | ||
| """A non-integer USERS value calls fail() and returns early — no NameError or crash.""" | ||
| from tests.performance_tests.validate_inputs import main | ||
|
|
||
| # USERS returns a non-integer; all other env vars return a valid integer string | ||
| mock_get_env.side_effect = lambda name: "NOT_AN_INT" if name == "USERS" else "1" | ||
|
|
||
| # Should complete without raising any exception | ||
| main() | ||
|
|
||
| mock_fail.assert_called_once_with("Invalid input", "USERS must be an integer.") |
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
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 |
|---|---|---|
| @@ -1,16 +1,49 @@ | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| def save_to_file(file_name: str, data, directory: str = None): | ||
| if directory is not None: | ||
| Path.mkdir(Path(directory), parents=True, exist_ok=True) | ||
| else: | ||
| directory = Path.cwd() | ||
|
|
||
| with Path.open(Path(directory) / file_name, "w") as f: | ||
| f.write(data) | ||
| def save_to_file(file_name: str, data: str, directory: str | None = None) -> None: | ||
| """Write ``data`` to ``directory/file_name``, creating the directory if needed. | ||
|
|
||
| Args: | ||
| file_name: Name of the file to write. | ||
| data: String content to write. | ||
| directory: Optional directory path. Defaults to the current working directory. | ||
|
|
||
| def load_from_file(file_name): | ||
| with Path.open(file_name, "r") as f: | ||
| return f.read() | ||
| Raises: | ||
| OSError: If the file cannot be written. | ||
| """ | ||
| target_dir = Path(directory) if directory is not None else Path.cwd() | ||
| try: | ||
| target_dir.mkdir(parents=True, exist_ok=True) | ||
| with (target_dir / file_name).open("w", encoding="utf-8") as f: | ||
| f.write(data) | ||
| except (OSError, IOError) as e: | ||
| logger.error("Failed to save file %s to %s: %s", file_name, target_dir, e) | ||
| raise | ||
|
|
||
|
|
||
| def load_from_file(file_name: str) -> str: | ||
| """Read and return the full text content of ``file_name``. | ||
|
|
||
| Args: | ||
| file_name: Path to the file to read. | ||
|
|
||
| Returns: | ||
| The file contents as a string. | ||
|
|
||
| Raises: | ||
| FileNotFoundError: If the file does not exist. | ||
| OSError: If the file cannot be read. | ||
| """ | ||
| try: | ||
| with Path(file_name).open("r", encoding="utf-8") as f: | ||
| return f.read() | ||
| except FileNotFoundError: | ||
| logger.error("File not found: %s", file_name) | ||
| raise | ||
| except (OSError, IOError) as e: | ||
| logger.error("Failed to read file %s: %s", file_name, e) | ||
| raise |
Oops, something went wrong.
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.