From 16db6d933bc5ff16394186252cd00409b7f93457 Mon Sep 17 00:00:00 2001 From: cyschneck <22159116+cyschneck@users.noreply.github.com> Date: Tue, 26 Mar 2024 00:29:48 -0600 Subject: [PATCH] pytests for empty right/left banks --- centerline_width/preprocessing.py | 6 ++-- .../pytests/test_verifyPreprocessing.py | 34 +++++++++++++++++++ centerline_width/relativeDistance.py | 9 +++-- 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 centerline_width/pytests/test_verifyPreprocessing.py diff --git a/centerline_width/preprocessing.py b/centerline_width/preprocessing.py index 3f3d682..8bdec65 100644 --- a/centerline_width/preprocessing.py +++ b/centerline_width/preprocessing.py @@ -74,11 +74,9 @@ def generatePolygon(left_bank_lst, recursion_check=False): # Return a shapely polygon based on the position of the river bank points if len(right_bank_lst) == 0: - logger.critical("\nCRITICAL ERROR, right bank data is empty (or NaN)") - exit() + raise ValueError("CRITICAL ERROR, right bank data is empty (or NaN)") if len(left_bank_lst) == 0: - logger.critical("\nCRITICAL ERROR, left bank data is empty (or NaN)") - exit() + raise ValueError("\nCRITICAL ERROR, left bank data is empty (or NaN)") circular_list_of_banks = left_bank_lst + right_bank_lst[::-1] + [ left_bank_lst[0] ] diff --git a/centerline_width/pytests/test_verifyPreprocessing.py b/centerline_width/pytests/test_verifyPreprocessing.py new file mode 100644 index 0000000..cda3890 --- /dev/null +++ b/centerline_width/pytests/test_verifyPreprocessing.py @@ -0,0 +1,34 @@ +# centerline-width/: python -m pytest -v +# Pytests to Compare and Verify Expected Outputs +from io import StringIO +import re + +# External Python libraries (installed via pip install) +import pytest + +# Internal centerline-width reference to access functions, global variables, and error handling +import centerline_width + + +def test_preprocessing_emptyRightBankCSV(): + with pytest.raises( + ValueError, + match=re.escape( + "CRITICAL ERROR, right bank data is empty (or NaN)")): + csv_example = StringIO() + csv_example.write("llat,llon,rlat,rlon\n") + csv_example.write("30.037581,-92.868569,,\n") + csv_example.seek(0) + centerline_width.riverCenterline(csv_data=csv_example) + + +def test_preprocessing_emptyLeftBankCSV(): + with pytest.raises( + ValueError, + match=re.escape( + "CRITICAL ERROR, left bank data is empty (or NaN)")): + csv_example = StringIO() + csv_example.write("llat,llon,rlat,rlon\n") + csv_example.write(",,30.037441,-92.867476\n") + csv_example.seek(0) + centerline_width.riverCenterline(csv_data=csv_example) diff --git a/centerline_width/relativeDistance.py b/centerline_width/relativeDistance.py index 34b2e6c..3c08c22 100644 --- a/centerline_width/relativeDistance.py +++ b/centerline_width/relativeDistance.py @@ -23,11 +23,14 @@ def relativeSingleCoordinate(first_point, lat_lon_coord, ellipsoid): def relativeBankCoordinates(left_lon_lat_coordinates, right_lon_lat_coordinates, ellipsoid): # Convert bank latitude/longtiude coordinates to relative coordinates - first_point = left_lon_lat_coordinates[ - 0] # first point is the first point on the left bank - if left_lon_lat_coordinates is None or right_lon_lat_coordinates is None: return None, None + if len(left_lon_lat_coordinates) == 0 or len( + right_lon_lat_coordinates) == 0: + return None, None + + first_point = left_lon_lat_coordinates[ + 0] # first point is the first point on the left bank left_relative_coordinates = [] for left_point in left_lon_lat_coordinates: