Skip to content

Commit

Permalink
add tests, need to patch/scope mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeejay committed May 21, 2024
1 parent 61fe477 commit b367e61
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/GOSTurban/urban_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(
self, pop_layer, admin_layer, urban_layer="", hd_urban_layer="", temp_folder=""
):
"""
Summarize population into urban and rural based on GOST_Urban.UrbanRaster.calculateUrban
Summarize population into urban and rural based on GOSTurban.UrbanRaster.calculateUrban
Parameters
----------
Expand Down
59 changes: 56 additions & 3 deletions tests/test_urban_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest # noqa: F401
from GOSTurban import urban_helper
import os
import shutil
import numpy as np
from unittest import mock
import geopandas as gpd
Expand Down Expand Up @@ -35,7 +36,6 @@ def mocked_zonal_stats(self, inr, minVal=0):
return np.ones((1, 4))

@mock.patch("rasterio.open", mocked_rasterio_open)
@mock.patch("GOSTrocks.rasterMisc.zonalStats", mocked_zonal_stats)
def test_01(self, tmp_path):
# make the admin layer gpd
df = pd.DataFrame(
Expand Down Expand Up @@ -63,8 +63,61 @@ def test_01(self, tmp_path):
assert cv is False
assert isinstance(sp.check_vals, dict)

# run the calculate method
# zs = sp.calculate_zonal()
@mock.patch("rasterio.open", mocked_rasterio_open)
def test_02(self, tmp_path):
# make the admin layer gpd
df = pd.DataFrame(
{
"idx": ["a", "b", "c"],
"geometry": [
Polygon([(0, 0), (1, 0), (1, 1)]),
Polygon([(0, 0), (1, 0), (1, 1)]),
Polygon([(0, 0), (1, 0), (1, 1)]),
],
}
)
admin_layer = gpd.GeoDataFrame(df, geometry=df.geometry, crs="EPSG:3857")
# try to initialize the class
sp = urban_helper.summarize_population("fake_dir/pop_path.tif", admin_layer)
assert sp.temp_folder == "fake_dir"

def test_03(self, tmp_path):
# make the admin layer gpd
df = pd.DataFrame(
{
"idx": ["a", "b", "c"],
"geometry": [
Polygon([(0, 0), (1, 0), (1, 1)]),
Polygon([(0, 0), (1, 0), (1, 1)]),
Polygon([(0, 0), (1, 0), (1, 1)]),
],
}
)
admin_layer = gpd.GeoDataFrame(df, geometry=df.geometry, crs="EPSG:3857")
# copy tutorial data over into the tmp folder
shutil.copyfile(
os.path.join(".", "data", "tutorial_data", "wp_2020_1k_AOI.tif"),
os.path.join(tmp_path, "pop_path.tif"),
)
shutil.copyfile(
os.path.join(".", "data", "tutorial_data", "wp_2020_1k_AOI.tif"),
os.path.join(tmp_path, "pop_path_urban.tif"),
)
shutil.copyfile(
os.path.join(".", "data", "tutorial_data", "wp_2020_1k_AOI.tif"),
os.path.join(tmp_path, "pop_path_urban_hd.tif"),
)

# try to initialize the class
sp = urban_helper.summarize_population(
os.path.join(tmp_path, "pop_path.tif"), admin_layer, temp_folder=tmp_path
)

# calculate zonal
zdf = sp.calculate_zonal()
# assertions
assert isinstance(zdf, pd.DataFrame)
assert zdf.shape[1] == 12


class TestUrbanCountry:
Expand Down

0 comments on commit b367e61

Please sign in to comment.