Skip to content

Commit

Permalink
Implement _enforce_file_extension() further across the package; updat…
Browse files Browse the repository at this point in the history
…e testing to accept new behaviours (warnings instead of errors)
  • Loading branch information
CBROWN-ONS committed Oct 12, 2023
1 parent 580f75f commit ac9de77
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/transport_performance/osm/osm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
_check_list,
_check_parent_dir_exists,
_is_expected_filetype,
_enforce_file_extension,
)


Expand Down Expand Up @@ -53,9 +54,7 @@ def filter_osm(
"""
# defence
_is_expected_filetype(pbf_pth, param_nm="pbf_pth", exp_ext=".pbf")
_is_expected_filetype(
out_pth, param_nm="out_pth", exp_ext=".pbf", check_existing=False
)
_enforce_file_extension(out_pth, ".pbf", ".pbf", "out_pth")
for nm, val in {
"tag_filter": tag_filter,
"install_osmosis": install_osmosis,
Expand Down
8 changes: 6 additions & 2 deletions src/transport_performance/utils/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
_check_parent_dir_exists,
_is_expected_filetype,
_type_defence,
_enforce_file_extension,
)


Expand Down Expand Up @@ -120,8 +121,11 @@ def merge_raster_files(
_type_defence(output_filename, "output_filename", str)
merged_dir = os.path.join(output_dir, output_filename)
_check_parent_dir_exists(merged_dir, "merged_dir", create=True)
_is_expected_filetype(
merged_dir, "merged_dir", check_existing=False, exp_ext=".tif"
# _is_expected_filetype(
# merged_dir, "merged_dir", check_existing=False, exp_ext=".tif"
# )
merged_dir = _enforce_file_extension(
merged_dir, ".tif", ".tif", "merged_dir"
)

xds_merged.rio.to_raster(merged_dir)
Expand Down
6 changes: 3 additions & 3 deletions tests/osm/test_osm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class TestFilterOsm(object):
"""Testing filter_osm()."""

def test_filter_osm_defence(self):
def test_filter_osm_defence(self, tmp_path):
"""Defensive behaviour for filter_osm."""
with pytest.raises(
FileNotFoundError,
Expand All @@ -34,8 +34,7 @@ def test_filter_osm_defence(self):
with pytest.raises(
TypeError,
match=re.escape(
"`pth` expected (<class 'str'>, <class 'pathlib.Path'>). Got <"
"class 'bool'>"
"`path` expected path-like, found <class 'bool'>."
),
):
# out_pth is not a path_like
Expand Down Expand Up @@ -70,6 +69,7 @@ def test_filter_osm_defence(self):
):
# type problems with bbox
filter_osm(bbox=[0, 1.1, 0.1, 1.2])
# TODO: Add better testing for OSM on MacOS if possible

@patch("builtins.print")
def test_filter_osm_defence_missing_osmosis(
Expand Down
28 changes: 20 additions & 8 deletions tests/utils/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"""

import os
import re
import pytest
import pathlib
import numpy as np
import rasterio as rio
import xarray as xr
Expand Down Expand Up @@ -290,11 +292,11 @@ def test_merge_raster_files(self, merge_xarrs_fpath: str) -> None:
"",
"test.txt",
None,
pytest.raises(
ValueError,
match=(
"`merged_dir` expected file extension .tif. "
"Found .txt"
pytest.warns(
UserWarning,
match=re.escape(
"Format .txt provided. Expected ['tif'] for path given"
" to 'merged_dir'. Path defaulted to .tif"
),
),
),
Expand Down Expand Up @@ -335,6 +337,7 @@ def test_merge_raster_files_on_fail(
output_filename,
subset_regex,
expected: Type[RaisesContext],
tmp_path: pathlib.Path,
) -> None:
"""Test `merge_raster_files` when raises occur.
Expand All @@ -350,6 +353,8 @@ def test_merge_raster_files_on_fail(
Regex str to select subset of input files within `input_dir`
expected : Type[RaisesContext]
exception to test with
tmp_path : pathlib.Path
path to temporary pytest directory.
Notes
-----
Expand All @@ -358,6 +363,8 @@ def test_merge_raster_files_on_fail(
"""
with expected:
if isinstance(output_dir, (str, pathlib.Path)):
output_dir = os.path.join(tmp_path, output_dir)
merge_raster_files(
input_dir=input_dir,
output_dir=output_dir,
Expand Down Expand Up @@ -420,9 +427,9 @@ def test_sum_resample_file(self, resample_xarr_fpath: str) -> None:
),
# test input_filepath that does not exist
(
"test.tif",
None,
None,
"test/test/test/test.tif",
"tester.tif",
5,
pytest.raises(FileNotFoundError, match="not found on file."),
),
# test input_filepath that does not have correct file extension
Expand Down Expand Up @@ -465,6 +472,7 @@ def test_sum_resample_on_fail(
output_filepath,
resample_factor,
expected: Type[RaisesContext],
tmp_path: pathlib.Path,
) -> None:
"""Test sum_resample_file in failing cases.
Expand All @@ -478,6 +486,8 @@ def test_sum_resample_on_fail(
resample factor to pass to `sum_resample_file`
expected : Type[RaisesContext]
exception to test with
tmp_path : pathlib.Path
path to temporary pytest directory.
Notes
-----
Expand All @@ -486,6 +496,8 @@ def test_sum_resample_on_fail(
"""
with expected:
if isinstance(output_filepath, (str, pathlib.Path)):
output_filepath = os.path.join(tmp_path, output_filepath)
sum_resample_file(
input_filepath=input_filepath,
output_filepath=output_filepath,
Expand Down

0 comments on commit ac9de77

Please sign in to comment.