diff --git a/inputs/input_invalid.txt b/inputs/input_invalid.txt new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/inputs/input_invalid.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/nordhealth_test/solution.py b/nordhealth_test/solution.py index 164bcff..5f9aa1c 100755 --- a/nordhealth_test/solution.py +++ b/nordhealth_test/solution.py @@ -26,11 +26,12 @@ Pairs : ( 4, 86) ( 23, 67) have sum : 90 def main() -> None: argc = len(sys.argv) argv = sys.argv - if argc != 2: - raise ValueError("Invalid input. You need to provide a single path to the input file. \n Ex.: `./solution.py ./input1.txt`") # Read input from the file and process the data try: + if argc != 2: + raise ValueError("Invalid input. You need to provide a single path to the input file. \n Ex.: `./solution.py ./input1.txt`") + input_data = read_input(argv[1]) print_pairs_with_same_sum(input_data) except ValueError as e: @@ -57,7 +58,7 @@ def print_pairs_with_same_sum(arr: List[int]) -> None: # Not a valid array if len(arr) < 2: - print("No pairs can be formed from the input file.") + raise ValueError("No pairs can be formed from the input file.") sum_map = defaultdict(list) diff --git a/tests/test_solution.py b/tests/test_solution.py index c38c47d..8b023fb 100644 --- a/tests/test_solution.py +++ b/tests/test_solution.py @@ -1,4 +1,5 @@ import sys +import pytest from io import StringIO from nordhealth_test.solution import print_pairs_with_same_sum @@ -43,13 +44,15 @@ def test_no_valid_pairs(): # Test case: Array with exactly one element def test_single_element(): arr = [10] - expected_output = "No pairs can be formed from the input file.\n" - result = capture_output(print_pairs_with_same_sum, arr) - assert result == expected_output + expected_output = "No pairs can be formed from the input file." + with pytest.raises(ValueError) as val_error: + capture_output(print_pairs_with_same_sum, arr) + assert str(val_error.value) == expected_output # Test case: Empty array def test_empty_array(): arr = [] - expected_output = "No pairs can be formed from the input file.\n" - result = capture_output(print_pairs_with_same_sum, arr) - assert result == expected_output \ No newline at end of file + expected_output = "No pairs can be formed from the input file." + with pytest.raises(ValueError) as val_error: + capture_output(print_pairs_with_same_sum, arr) + assert str(val_error.value) == expected_output \ No newline at end of file