Skip to content

Commit

Permalink
temp text files, test convertColumnsToCSV
Browse files Browse the repository at this point in the history
  • Loading branch information
cyschneck committed Mar 30, 2024
1 parent e5ff799 commit f6237fa
Showing 1 changed file with 66 additions and 13 deletions.
79 changes: 66 additions & 13 deletions centerline_width/pytests/test_verifyPreprocessing.py
@@ -1,34 +1,87 @@
# 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
import pandas as pd

# Internal centerline-width reference to access functions, global variables, and error handling
import centerline_width


def test_preprocessing_emptyRightBankCSV():
@pytest.fixture(scope="function")
def generate_txt_convertColumnsToCSV_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")
centerline_width.convertColumnsToCSV(text_file=str(temp_text_file),
flipBankDirection=False)

return temp_text_file


def test_txt_convertColumnsToCSV(generate_txt_convertColumnsToCSV_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_txt_convertColumnsToCSV_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_txt_convertColumnsToCSV_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.convertColumnsToCSV(text_file=str(temp_text_file),
flipBankDirection=False)

return temp_text_file


def test_preprocessing_emptyRightBankCSV(
generate_txt_convertColumnsToCSV_emptyRight):
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)
centerline_width.riverCenterline(
csv_data=str(generate_txt_convertColumnsToCSV_emptyRight))


@pytest.fixture(scope="function")
def generate_txt_convertColumnsToCSV_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.convertColumnsToCSV(text_file=str(temp_text_file),
flipBankDirection=False)

return temp_text_file


def test_preprocessing_emptyLeftBankCSV():
def test_preprocessing_emptyLeftBankCSV(
generate_txt_convertColumnsToCSV_emptyLeft):
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)
centerline_width.riverCenterline(
csv_data=str(generate_txt_convertColumnsToCSV_emptyLeft))

0 comments on commit f6237fa

Please sign in to comment.