fixed assertions to fetch Exceptions and assert message

This commit is contained in:
2025-04-01 23:55:33 +03:00
parent 28b59bb2e7
commit d683ff8165
3 changed files with 14 additions and 9 deletions

1
inputs/input_invalid.txt Normal file
View File

@@ -0,0 +1 @@
1

View File

@@ -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)

View File

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