Skip to content

Commit

Permalink
update tests with new function/variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
cyschneck committed Aug 19, 2024
1 parent b9855c7 commit 564a31a
Show file tree
Hide file tree
Showing 10 changed files with 888 additions and 146 deletions.
6 changes: 6 additions & 0 deletions centerline_width/error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ def errrorHandlingConvertColumnsToCSV(txt_input: str = None,

def errorHandlingExtractPointsToTextFile(left_kml: str = None,
right_kml: str = None,
flip_direction: bool = None,
csv_output: str = None,
text_output_name: str = None) -> None:
# Error Handling for extractPointsToTextFile()
Expand Down Expand Up @@ -501,6 +502,11 @@ def errorHandlingExtractPointsToTextFile(left_kml: str = None,
f"right_kml and left_kml are set to the same file (needs a separate left and right bank): right_kml='{right_kml}' and left_kml='{left_kml}'"
)

if type(flip_direction) != bool:
raise ValueError(
f"[flip_direction]: Must be a bool, current type = '{type(flip_direction)}'"
)

if csv_output is None and text_output_name is None:
raise ValueError(
"[csv_output/text_output_name]: Requires output file name")
Expand Down
6 changes: 5 additions & 1 deletion centerline_width/getCoordinatesKML.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ def extractPointsToTextFile(left_kml: str = None,


def convertColumnsToCSV(text_file: str = None,
txt_input: str = None,
flip_direction: bool = False) -> None:
### Pending Deprecation for function name: replaced with txt_to_csv
warnings.warn(
"convertColumnsToCSV() has been replaced with kml_to_csv() and will be removed in the future",
FutureWarning,
stacklevel=2)
txt_to_csv(text_file=text_file, flip_direction=flip_direction)
txt_to_csv(text_file=text_file,
txt_input=txt_input,
flip_direction=flip_direction)


def kml_to_csv(left_kml: str = None,
Expand All @@ -66,6 +69,7 @@ def kml_to_csv(left_kml: str = None,
centerline_width.errorHandlingExtractPointsToTextFile(
left_kml=left_kml,
right_kml=right_kml,
flip_direction=flip_direction,
csv_output=csv_output,
text_output_name=text_output_name)

Expand Down
99 changes: 52 additions & 47 deletions centerline_width/pytests/test_errorGetCoordinatesKML.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,135 +20,140 @@
("testing_string", "<class 'str'>")]


## extractPointsToTextFile() #####################################################
def test_extractPointsToTextFile_leftKMLRequired():
## kml_to_csv() #####################################################
def test_kmlToCSV_leftKMLRequired():
with pytest.raises(ValueError,
match=re.escape("[left_kml]: Requires left_kml file")):
centerline_width.extractPointsToTextFile(left_kml=None)
centerline_width.kml_to_csv(left_kml=None)


@pytest.mark.parametrize("invalid_input, error_output",
invalid_non_str_options)
def test_plotCenterline_leftKMLInvalidTypes(invalid_input, error_output):
def test_kmlToCSV_leftKMLInvalidTypes(invalid_input, error_output):
with pytest.raises(
ValueError,
match=re.escape(
f"[left_kml]: Must be a str, current type = '{error_output}'")
):
centerline_width.extractPointsToTextFile(left_kml=invalid_input)
centerline_width.kml_to_csv(left_kml=invalid_input)


def test_extractPointsToTextFile_leftKMLInvalidExtension():
def test_kmlToCSV_leftKMLInvalidExtension():
with pytest.raises(
ValueError,
match=re.escape(
"[left_kml]: Extension must be a .kml file, current extension = 'txt'"
)):
centerline_width.extractPointsToTextFile(left_kml="left_kml.txt")
centerline_width.kml_to_csv(left_kml="left_kml.txt")


def test_extractPointsToTextFile_rightKMLRequired():
def test_kmlToCSV_rightKMLRequired():
with pytest.raises(
ValueError,
match=re.escape("[right_kml]: Requires right_kml file")):
centerline_width.extractPointsToTextFile(left_kml="left_kml.kml",
right_kml=None)
centerline_width.kml_to_csv(left_kml="left_kml.kml", right_kml=None)


@pytest.mark.parametrize("invalid_input, error_output",
invalid_non_str_options)
def test_plotCenterline_rightKMLInvalidTypes(invalid_input, error_output):
def test_kmlToCSV_rightKMLInvalidTypes(invalid_input, error_output):
with pytest.raises(
ValueError,
match=re.escape(
f"[right_kml]: Must be a str, current type = '{error_output}'")
):
centerline_width.extractPointsToTextFile(left_kml="left_kml.kml",
right_kml=invalid_input)
centerline_width.kml_to_csv(left_kml="left_kml.kml",
right_kml=invalid_input)


def test_extractPointsToTextFile_rightKMLInvalidExtension():
def test_kmlToCSV_rightKMLInvalidExtension():
with pytest.raises(
ValueError,
match=re.escape(
"[right_kml]: Extension must be a .kml file, current extension = 'txt'"
)):
centerline_width.extractPointsToTextFile(left_kml="left_kml.kml",
right_kml="right_kml.txt")
centerline_width.kml_to_csv(left_kml="left_kml.kml",
right_kml="right_kml.txt")


def test_extractPointsToTextFile_textOutputNameRequired():
def test_kmlToCSV_textOutputNameRequired():
with pytest.raises(
ValueError,
match=re.escape(
"[csv_output/text_output_name]: Requires output file name")):
# Update Pending Deprecation ValueError
centerline_width.extractPointsToTextFile(left_kml="left_kml.kml",
right_kml="right_kml.kml",
text_output_name=None)
centerline_width.kml_to_csv(left_kml="left_kml.kml",
right_kml="right_kml.kml",
text_output_name=None)


@pytest.mark.parametrize("invalid_input, error_output",
invalid_non_str_options)
def test_plotCenterline_textOutputNameInvalidTypes(invalid_input,
error_output):
def test_kmlToCSV_CSVOutputNameInvalidTypes(invalid_input, error_output):
with pytest.raises(
ValueError,
match=re.escape(
f"[text_output_name]: Must be a str, current type = '{error_output}'"
f"[csv_output]: Must be a str, current type = '{error_output}'"
)):
centerline_width.extractPointsToTextFile(
left_kml="left_kml.kml",
right_kml="right_kml.kml",
text_output_name=invalid_input)
centerline_width.kml_to_csv(left_kml="left_kml.kml",
right_kml="right_kml.kml",
csv_output=invalid_input)


def test_extractPointsToTextFile_rightAndLeftKMLMatchInvalid():
def test_kmlToCSV_rightAndLeftKMLMatchInvalid():
with pytest.raises(
ValueError,
match=re.escape(
"right_kml and left_kml are set to the same file (needs a separate left and right bank): right_kml='same_kml.kml' and left_kml='same_kml.kml'"
)):
centerline_width.extractPointsToTextFile(left_kml="same_kml.kml",
right_kml="same_kml.kml",
text_output_name=None)
centerline_width.kml_to_csv(left_kml="same_kml.kml",
right_kml="same_kml.kml",
csv_output=None)


## convertColumnsToCSV() #####################################################
def test_convertColumnsToCSV_textFileRequired():
with pytest.raises(ValueError,
match=re.escape('[txt_input]: Requires text file')):
centerline_width.convertColumnsToCSV(text_file=None)
def test_kmlToCSV_textFileRequired():
with pytest.raises(
ValueError,
match=re.escape(
'[csv_output/text_output_name]: Requires output file name')):
centerline_width.kml_to_csv(left_kml="left_kml.kml",
right_kml="right_kml.kml",
csv_output=None)


@pytest.mark.parametrize("invalid_input, error_output",
invalid_non_str_options)
def test_convertColumnsToCSV_textFileInvalidTypes(invalid_input, error_output):
def test_kmlToCSV_textFileInvalidTypes(invalid_input, error_output):
with pytest.raises(
ValueError,
match=re.escape(
f"[txt_input]: Must be a str, current type = '{error_output}'")
):
centerline_width.convertColumnsToCSV(text_file=invalid_input)
f"[csv_output]: Must be a str, current type = '{error_output}'"
)):
centerline_width.kml_to_csv(left_kml="left_kml.kml",
right_kml="right_kml.kml",
csv_output=invalid_input)


def test_convertColumnsToCSV_textFileInvalidExtensions():
def test_kmlToCSV_textFileInvalidExtensions():
with pytest.raises(
ValueError,
match=re.escape(
"[txt_input]: Extension must be a .txt file, current extension = 'csv'"
"[csv_output]: Extension must be a .csv file, current extension = 'txt'"
)):
centerline_width.convertColumnsToCSV(text_file="text_file.csv")
centerline_width.kml_to_csv(left_kml="left_kml.kml",
right_kml="right_kml.kml",
csv_output="text_file.txt")


@pytest.mark.parametrize("invalid_input, error_output",
invalid_non_bool_options)
def test_convertColumnsToCSV_flipDirectionInvalidTypes(invalid_input,
error_output):
def test_kmlToCSV_flipDirectionInvalidTypes(invalid_input, error_output):
with pytest.raises(
ValueError,
match=re.escape(
f"[flip_direction]: Must be a bool, current type = '{error_output}'"
)):
centerline_width.convertColumnsToCSV(text_file="text_file.txt",
flip_direction=invalid_input)
centerline_width.kml_to_csv(csv_output="text_file.csv",
left_kml="left_kml.kml",
right_kml="right_kml.kml",
flip_direction=invalid_input)
124 changes: 115 additions & 9 deletions centerline_width/pytests/test_verifyGetCoordinatesKML.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Verify Outputs from getCoordinatesKML.py
# centerline-width/: python -m pytest -v
# python -m pytest -k test_verifyGetCoordinatesKML -xv
# python -m pytest -k test_verifyGetCoordinatesKML.py -xv

# Pytests to Compare and Verify Expected Outputs

# External Python libraries (installed via pip install)
import re
import pytest
import pandas as pd

Expand Down Expand Up @@ -156,15 +157,14 @@ def generate_kmlFile_left(tmpdir):

@pytest.fixture(scope="function")
def generate_kmlFile(tmpdir, generate_kmlFile_right, generate_kmlFile_left):
temp_txt_file = tmpdir.join("pytest.txt")
centerline_width.extractPointsToTextFile(
left_kml=str(generate_kmlFile_left),
right_kml=str(generate_kmlFile_right),
text_output_name=str(temp_txt_file))
return temp_txt_file
temp_csv_file = tmpdir.join("pytest.csv")
centerline_width.kml_to_csv(left_kml=str(generate_kmlFile_left),
right_kml=str(generate_kmlFile_right),
csv_output=str(temp_csv_file))
return temp_csv_file


def test_getCoordinatesKML_extractPointsToTextFile(generate_kmlFile):
def test_getCoordinatesKML_extractPointsToCSV(generate_kmlFile):
expected_df = pd.DataFrame({
'llat': [
30.03758064742554, 30.03761289873068, 30.03764767910492,
Expand All @@ -191,9 +191,115 @@ def test_getCoordinatesKML_extractPointsToTextFile(generate_kmlFile):
-92.86847053431237
]
})
kml_output_df = pd.read_csv(generate_kmlFile, sep='\\s+')
kml_output_df = pd.read_csv(generate_kmlFile)
assert expected_df.columns.tolist() == kml_output_df.columns.tolist()
assert list(expected_df["llat"]) == list(kml_output_df["llat"])
assert list(expected_df["llon"]) == list(kml_output_df["llon"])
assert list(expected_df["rlat"]) == list(kml_output_df["rlat"])
assert list(expected_df["rlon"]) == list(kml_output_df["rlon"])


def test_txtToCSV_futureWarning_functionName(tmpdir):
# Pending Deprecation: To Be Removed
with pytest.warns(
FutureWarning,
match=re.escape(
"convertColumnsToCSV() has been replaced with kml_to_csv() and will be removed in the future"
)):
temp_text_file = tmpdir.join("pytest.txt")
with open(str(temp_text_file), "w") as text_file:
text_file.write("llat,llon,rlat,rlon\n")
text_file.write(",,30.037441,-92.867476\n")
text_file.write(",,30.037441,-92.867476\n")
text_file.write(",,30.037441,-92.867476\n")
centerline_width.convertColumnsToCSV(txt_input=str(temp_text_file),
flip_direction=False)


def test_txtToCSV_futureWarning_variableName(tmpdir):
# Pending Deprecation: To Be Removed
with pytest.warns(
FutureWarning,
match=re.escape(
"text_file has been replaced with txt_input and will be removed in the future"
)):
temp_text_file = tmpdir.join("pytest.txt")
with open(str(temp_text_file), "w") as text_file:
text_file.write("llat,llon,rlat,rlon\n")
text_file.write(",,30.037441,-92.867476\n")
text_file.write(",,30.037441,-92.867476\n")
text_file.write(",,30.037441,-92.867476\n")
centerline_width.txt_to_csv(text_file=str(temp_text_file),
flip_direction=False)


@pytest.fixture(scope="function")
def generate_txtToCSV_noFlip(tmpdir):
temp_text_file = tmpdir.join("pytest.txt")
with open(str(temp_text_file), "w") as text_file:
text_file.write("llat,llon,rlat,rlon\n")
text_file.write("30.037581,-92.868569,30.037441,-92.867476\n")
text_file.write("30.137581,-92.868569,30.037441,-92.867476\n")
text_file.write("30.237581,-92.868569,30.037441,-92.867476\n")
return temp_text_file


def test_txtToCSV_convertColumnsToCSV(generate_txtToCSV_noFlip):
expected_df = pd.DataFrame({
'llat': [30.037581, 30.137581, 30.237581],
'llon': [-92.868569, -92.868569, -92.868569],
'rlat': [30.037441, 30.037441, 30.037441],
'rlon': [-92.867476, -92.867476, -92.867476]
})
txt_output_df = pd.read_csv(generate_txtToCSV_noFlip)
assert expected_df.columns.tolist() == txt_output_df.columns.tolist()
assert list(expected_df["llat"]) == list(txt_output_df["llat"])
assert list(expected_df["llon"]) == list(txt_output_df["llon"])
assert list(expected_df["rlat"]) == list(txt_output_df["rlat"])
assert list(expected_df["rlon"]) == list(txt_output_df["rlon"])


@pytest.fixture(scope="function")
def generate_txtToCSV_emptyRight(tmpdir):
temp_text_file = tmpdir.join("pytest.txt")
with open(str(temp_text_file), "w") as text_file:
text_file.write("llat,llon,rlat,rlon\n")
text_file.write("30.037581,-92.868569,,\n")
text_file.write("30.137581,-92.868569,,\n")
text_file.write("30.237581,-92.868569,,\n")
centerline_width.txt_to_csv(txt_input=str(temp_text_file),
flip_direction=False)

return temp_text_file


def test_txtToCSV_emptyRightBankCSV(generate_txtToCSV_emptyRight):
with pytest.raises(
ValueError,
match=re.escape(
"CRITICAL ERROR, right bank data is empty (or NaN)")):
centerline_width.CenterlineWidth(
csv_data=str(generate_txtToCSV_emptyRight))


@pytest.fixture(scope="function")
def generate_txtToCSV_emptyLeft(tmpdir):
temp_text_file = tmpdir.join("pytest.txt")
with open(str(temp_text_file), "w") as text_file:
text_file.write("llat,llon,rlat,rlon\n")
text_file.write(",,30.037441,-92.867476\n")
text_file.write(",,30.037441,-92.867476\n")
text_file.write(",,30.037441,-92.867476\n")
centerline_width.txt_to_csv(txt_input=str(temp_text_file),
flip_direction=False)

return temp_text_file


def test_txtToCSV_emptyLeftBankCSV(generate_txtToCSV_emptyLeft):
with pytest.raises(
ValueError,
match=re.escape(
"CRITICAL ERROR, left bank data is empty (or NaN)")):
centerline_width.CenterlineWidth(
csv_data=str(generate_txtToCSV_emptyLeft))
Loading

0 comments on commit 564a31a

Please sign in to comment.