From 470ef1b9c3d0933dc74c825d595c559a4ba9b39c Mon Sep 17 00:00:00 2001 From: elbeejay Date: Mon, 6 May 2024 18:59:20 -0400 Subject: [PATCH] add more tests, make code more module/flexible as tests are written --- src/GOSTurban/country_helper.py | 4 +++- tests/test_UrbanRaster.py | 34 +++++++++++++++++++++++---------- tests/test_country_helper.py | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/GOSTurban/country_helper.py b/src/GOSTurban/country_helper.py index 47e8a19..637de81 100755 --- a/src/GOSTurban/country_helper.py +++ b/src/GOSTurban/country_helper.py @@ -174,8 +174,10 @@ def summarize_ntl(self, ntl_files=[]): if len(ntl_files) == 0: ntl_files = ntl.aws_search_ntl() for ntl_file in ntl_files: - name = ntl_file.split("/")[-1].split("_")[2][:8] + name = ntl_file # init a name try: + _, _fname = os.path.split(ntl_file) + name = _fname.split("_")[2][:8] inR = rasterio.open(ntl_file) # tPrint("Processing %s" % name) urban_res_file = os.path.join(viirs_folder, f"URBAN_{name}.csv") diff --git a/tests/test_UrbanRaster.py b/tests/test_UrbanRaster.py index 2a23fe9..60ef84d 100644 --- a/tests/test_UrbanRaster.py +++ b/tests/test_UrbanRaster.py @@ -4,6 +4,7 @@ from GOSTurban import UrbanRaster from unittest.mock import MagicMock from unittest import mock +from unittest.mock import patch import numpy as np @@ -66,13 +67,26 @@ def test_init_value_error(self): with pytest.raises(ValueError): UrbanRaster.urbanGriddedPop(5) - # @mock.patch("rasterio.open", mocked_rasterio_open) - # def test_burn_value(self): - # """Testing the private burn value function.""" - # # make the object - # ugp = UrbanRaster.urbanGriddedPop('str') - # with patch("shapely.geometry") as mock_shape: - # mock_shape = MagicMock() - # final_raster, allFeatures = ugp._burnValue( - # np.ones((5, 5), dtype=bool), 1, np.zeros((5, 5)), [], 'a', 'pop', mock_shape - # ) + @mock.patch("rasterio.open", mocked_rasterio_open) + def test_burn_value(self): + """Testing the private burn value function.""" + # make the object + ugp = UrbanRaster.urbanGriddedPop("str") + with patch("shapely.geometry") as mock_shape: + mock_shape = {"type": "Point", "coordinates": [0, 1]} + final_raster, allFeatures = ugp._burnValue( + np.ones((5, 5), dtype=bool), + 1, + np.zeros((5, 5)), + [], + "a", + "pop", + mock_shape, + ) + # assertions + assert final_raster.shape == (5, 5) + assert final_raster[0, 0] == 0.0 + assert isinstance(allFeatures, list) + assert len(allFeatures) == 1 + assert len(allFeatures[0]) == 4 + assert allFeatures[0][0] == "a" diff --git a/tests/test_country_helper.py b/tests/test_country_helper.py index 8701954..0e52e06 100644 --- a/tests/test_country_helper.py +++ b/tests/test_country_helper.py @@ -61,6 +61,40 @@ def test_summarize_ntl(self, tmp_path): # assert that the mocked function was called ntl.aws_search_ntl.assert_called_once() + def test_summarize_ntl_list_err(self, tmp_path, capfd): + """Test the summarize_ntl method with an ntl list, erroring.""" + # make a tmp location for output + out_folder = tmp_path / "output" + # make the class + ch = country_helper.urban_country( + iso3="USA", sel_country="placeholder", cur_folder=out_folder, inP=[1, 2, 3] + ) + # mock some of the methods called + ntl.aws_search_ntl = MagicMock() + # try calling the method + ch.summarize_ntl(ntl_files=["invalid01", "invalid02"]) + # captured output + captured = capfd.readouterr() + assert captured.out.split("\t")[1][0] == "*" + assert captured.out.split("\t")[2][0] == "*" + + def test_summarize_ntl_list(self, tmp_path, capfd): + """Test the summarize_ntl method with an ntl list, erroring.""" + # make a tmp location for output + out_folder = tmp_path / "output" + # make the class + ch = country_helper.urban_country( + iso3="USA", sel_country="placeholder", cur_folder=out_folder, inP=[1, 2, 3] + ) + # mock some of the methods called + ntl.aws_search_ntl = MagicMock() + rasterio.open = MagicMock() + # try calling the method + ch.summarize_ntl(ntl_files=["a_b_c"]) + # captured output + captured = capfd.readouterr() + assert len(captured.out.split("\t")) > 1 + def test_summarize_ghsl(self, tmp_path): """Test the summarize_ghsl method.""" # make a tmp location for output