Skip to content

Commit

Permalink
add more tests, make code more module/flexible as tests are written
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeejay committed May 6, 2024
1 parent d775326 commit 470ef1b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/GOSTurban/country_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
34 changes: 24 additions & 10 deletions tests/test_UrbanRaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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"
34 changes: 34 additions & 0 deletions tests/test_country_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 470ef1b

Please sign in to comment.