Skip to content

Commit

Permalink
feat: tests for MultiGtfsInstance::viz_stops().
Browse files Browse the repository at this point in the history
  • Loading branch information
CBROWN-ONS committed Nov 17, 2023
1 parent 49f8b10 commit b1c0f59
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/transport_performance/gtfs/multi_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
_type_defence,
_is_expected_filetype,
_check_parent_dir_exists,
_enforce_file_extension,
)


Expand Down Expand Up @@ -390,8 +391,9 @@ def viz_stops(
# defences
_type_defence(path, "path", (str, pathlib.Path, type(None)))
_type_defence(return_viz, "return_viz", (bool, type(None)))
if not isinstance(path, type(None)):
if path:
_check_parent_dir_exists(path, "path", True)
_enforce_file_extension(path, ".html", ".html", "path")
if not path and not return_viz:
raise ValueError(
"Both 'path' and 'return_viz' parameters are of NoneType."
Expand Down
53 changes: 53 additions & 0 deletions tests/gtfs/test_multi_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
import pandas as pd
import folium

from transport_performance.gtfs.multi_validation import (
MultiGtfsInstance,
Expand Down Expand Up @@ -246,3 +247,55 @@ def test_summarise_routes(self, multi_gtfs_fixture):
# assert summary isn't returned
not_summary = multi_gtfs_fixture.summarise_routes(return_summary=False)
assert isinstance(not_summary, type(None))

@pytest.mark.parametrize(
"path, return_viz, raises, match",
(
[
True,
True,
TypeError,
".*path.*expected.*str.*Path.*None.*Got.*bool.*",
],
[
"test.html",
12,
TypeError,
".*return_viz.*expected.*bool.*None.*Got.*int.*",
],
[
None,
None,
ValueError,
"Both .*path.*return_viz.* parameters are of NoneType.",
],
),
)
def test_viz_stops_defences(
self, multi_gtfs_fixture, path, return_viz, raises, match
):
"""Defensive tests for .viz_stops()."""
with pytest.raises(raises, match=match):
multi_gtfs_fixture.viz_stops(path=path, return_viz=return_viz)

def test_viz_stops(self, multi_gtfs_fixture, tmp_path):
"""General tests for .viz_stops()."""
# saving without returning
save_path = os.path.join(tmp_path, "save_test.html")
returned = multi_gtfs_fixture.viz_stops(
path=save_path, return_viz=False
)
assert os.path.exists(save_path)
assert isinstance(returned, type(None))
# saving with returning
save_path = os.path.join(tmp_path, "save_test2.html")
returned = multi_gtfs_fixture.viz_stops(
path=save_path, return_viz=True
)
assert os.path.exists(save_path)
assert isinstance(returned, folium.Map)
# returning without save
returned = multi_gtfs_fixture.viz_stops(return_viz=True)
assert isinstance(returned, folium.Map)
files = glob.glob(f"{tmp_path}/*.html")
assert len(files) == 2, "More files saved than expected"

0 comments on commit b1c0f59

Please sign in to comment.