fixed assertions to fetch Exceptions and assert message
This commit is contained in:
1
inputs/input_invalid.txt
Normal file
1
inputs/input_invalid.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
1
|
||||||
@@ -26,11 +26,12 @@ Pairs : ( 4, 86) ( 23, 67) have sum : 90
|
|||||||
def main() -> None:
|
def main() -> None:
|
||||||
argc = len(sys.argv)
|
argc = len(sys.argv)
|
||||||
argv = 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
|
# Read input from the file and process the data
|
||||||
try:
|
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])
|
input_data = read_input(argv[1])
|
||||||
print_pairs_with_same_sum(input_data)
|
print_pairs_with_same_sum(input_data)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
@@ -57,7 +58,7 @@ def print_pairs_with_same_sum(arr: List[int]) -> None:
|
|||||||
|
|
||||||
# Not a valid array
|
# Not a valid array
|
||||||
if len(arr) < 2:
|
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)
|
sum_map = defaultdict(list)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
import pytest
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from nordhealth_test.solution import print_pairs_with_same_sum
|
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
|
# Test case: Array with exactly one element
|
||||||
def test_single_element():
|
def test_single_element():
|
||||||
arr = [10]
|
arr = [10]
|
||||||
expected_output = "No pairs can be formed from the input file.\n"
|
expected_output = "No pairs can be formed from the input file."
|
||||||
result = capture_output(print_pairs_with_same_sum, arr)
|
with pytest.raises(ValueError) as val_error:
|
||||||
assert result == expected_output
|
capture_output(print_pairs_with_same_sum, arr)
|
||||||
|
assert str(val_error.value) == expected_output
|
||||||
|
|
||||||
# Test case: Empty array
|
# Test case: Empty array
|
||||||
def test_empty_array():
|
def test_empty_array():
|
||||||
arr = []
|
arr = []
|
||||||
expected_output = "No pairs can be formed from the input file.\n"
|
expected_output = "No pairs can be formed from the input file."
|
||||||
result = capture_output(print_pairs_with_same_sum, arr)
|
with pytest.raises(ValueError) as val_error:
|
||||||
assert result == expected_output
|
capture_output(print_pairs_with_same_sum, arr)
|
||||||
|
assert str(val_error.value) == expected_output
|
||||||
Reference in New Issue
Block a user