From ac9de779f5df66ee51194a0f1b8761ba19fe0756 Mon Sep 17 00:00:00 2001 From: Browning Date: Thu, 12 Oct 2023 14:33:05 +0100 Subject: [PATCH] Implement _enforce_file_extension() further across the package; update testing to accept new behaviours (warnings instead of errors) --- src/transport_performance/osm/osm_utils.py | 5 ++-- src/transport_performance/utils/raster.py | 8 +++++-- tests/osm/test_osm_utils.py | 6 ++--- tests/utils/test_raster.py | 28 +++++++++++++++------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/transport_performance/osm/osm_utils.py b/src/transport_performance/osm/osm_utils.py index bd444847..a07f3ab7 100644 --- a/src/transport_performance/osm/osm_utils.py +++ b/src/transport_performance/osm/osm_utils.py @@ -9,6 +9,7 @@ _check_list, _check_parent_dir_exists, _is_expected_filetype, + _enforce_file_extension, ) @@ -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, diff --git a/src/transport_performance/utils/raster.py b/src/transport_performance/utils/raster.py index a55a02e0..cec9983e 100644 --- a/src/transport_performance/utils/raster.py +++ b/src/transport_performance/utils/raster.py @@ -20,6 +20,7 @@ _check_parent_dir_exists, _is_expected_filetype, _type_defence, + _enforce_file_extension, ) @@ -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) diff --git a/tests/osm/test_osm_utils.py b/tests/osm/test_osm_utils.py index 2fbd4ec2..26112559 100644 --- a/tests/osm/test_osm_utils.py +++ b/tests/osm/test_osm_utils.py @@ -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, @@ -34,8 +34,7 @@ def test_filter_osm_defence(self): with pytest.raises( TypeError, match=re.escape( - "`pth` expected (, ). Got <" - "class 'bool'>" + "`path` expected path-like, found ." ), ): # out_pth is not a path_like @@ -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( diff --git a/tests/utils/test_raster.py b/tests/utils/test_raster.py index 08420c0a..8f5444ec 100644 --- a/tests/utils/test_raster.py +++ b/tests/utils/test_raster.py @@ -9,7 +9,9 @@ """ import os +import re import pytest +import pathlib import numpy as np import rasterio as rio import xarray as xr @@ -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" ), ), ), @@ -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. @@ -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 ----- @@ -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, @@ -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 @@ -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. @@ -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 ----- @@ -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,