Skip to content

Commit

Permalink
Added tests for _remove_validation_row
Browse files Browse the repository at this point in the history
  • Loading branch information
CBROWN-ONS committed Oct 17, 2023
1 parent 70ef720 commit 449b445
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/transport_performance/gtfs/gtfs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pathlib
from geopandas import GeoDataFrame
import numpy as np
import warnings

from transport_performance.utils.defence import (
_is_expected_filetype,
Expand Down Expand Up @@ -400,18 +401,19 @@ def _remove_validation_row(
_type_defence(message, "message", (str, type(None)))
_type_defence(index, "index", (list, np.ndarray, type(None)))
_check_attribute(gtfs, "validity_df")
# remove row from validation table
if message is None and index is None:
raise ValueError(
"Both 'message' and 'index' are None, therefore no"
"warnings/errors are able to be cleaned."
)
if message is not None and index is not None:
raise UserWarning(
"Both 'index' and 'message' are not None. Warnings/"
"Errors have been cleaned on 'message'"
warnings.warn(
UserWarning(
"Both 'index' and 'message' are not None. Warnings/"
"Errors have been cleaned on 'message'"
)
)

# remove row from validation table
if message is not None:
gtfs.validity_df = gtfs.validity_df[
~gtfs.validity_df.message.str.contains(
Expand Down
48 changes: 48 additions & 0 deletions tests/gtfs/test_gtfs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
filter_gtfs_around_trip,
convert_pandas_to_plotly,
_get_validation_warnings,
_remove_validation_row,
)

# location of GTFS test fixture
Expand Down Expand Up @@ -283,3 +284,50 @@ def test__get_validation_warnings(self):
gtfs, "This is a test!!!", return_type="Dataframe"
)
assert len(no_match) == 0, "No matches expected. Matched found"


class TestRemoveValidationRow(object):
"""Tests for _remove_validation_row."""

def test__remove_validation_row_defence(self):
"""Tests the defences of _remove_validation_row."""
gtfs = GtfsInstance(GTFS_FIX_PTH)
gtfs.is_valid()
# no message or index provided
with pytest.raises(
ValueError, match=r"Both .* and .* are None, .* to be cleaned"
):
_remove_validation_row(gtfs)
# both provided
with pytest.warns(
UserWarning,
match=r"Both .* and .* are not None.* cleaned on 'message'",
):
_remove_validation_row(gtfs, message="test", index=[0, 1])

def test__remove_validation_row_on_pass(self):
"""Tests for _remove_validation_row on pass."""
gtfs = GtfsInstance(GTFS_FIX_PTH)
gtfs.is_valid()
# with message
msg = "Unrecognized column agency_noc"
_remove_validation_row(gtfs, message=msg)
assert len(gtfs.validity_df) == 5, "DF is incorrect size"
found_cols = _get_validation_warnings(
gtfs, message=msg, return_type="dataframe"
)
assert (
len(found_cols) == 0
), "Invalid errors/warnings still in validity_df"
# with index (removing the same error)
gtfs = GtfsInstance(GTFS_FIX_PTH)
gtfs.is_valid()
ind = [0]
_remove_validation_row(gtfs, index=ind)
assert len(gtfs.validity_df) == 5, "DF is incorrect size"
found_cols = _get_validation_warnings(
gtfs, message=msg, return_type="dataframe"
)
assert (
len(found_cols) == 0
), "Invalid errors/warnings still in validity_df"

0 comments on commit 449b445

Please sign in to comment.