Skip to content

Commit cdfb644

Browse files
Joe McGlinchyLeah Wasser
authored andcommitted
added tests for _stack_bands and deprecated stack_raster_tifs. (#241)
* added tests for _stack_bands and deprecated stack_raster_tifs. Also edited _stack_bands in spatial.py since it needed to change ValueError to Exception for except clause to be accessed. Otherwise, the specific error in the test is an AttributeError. * cleaned up imports and changed Exception to AssertionError in spatial.py stack function * Adding warning message to stack_raster_tifs deprecation test
1 parent 5bc49cb commit cdfb644

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

earthpy/spatial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ def _stack_bands(sources, write_raster=False, dest=None):
254254
for src in sources:
255255
src.profile
256256

257-
except ValueError as ve:
258-
raise ValueError("The sources object should be Dataset Reader")
257+
except AttributeError as ae:
258+
raise AttributeError("The sources object should be Dataset Reader")
259259
sys.exit()
260260

261261
else:

earthpy/tests/test__stack_bands.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
""" Tests for the _stack_bands() method """
2+
3+
import numpy as np
4+
import pytest
5+
import earthpy.spatial as es
6+
7+
8+
@pytest.fixture
9+
def b1_b2_arrs():
10+
b1 = np.array([[6, 7, 8, 9, 10], [16, 17, 18, 19, 20]])
11+
b2 = np.array([[1, 2, 3, 4, 5], [14, 12, 13, 14, 17]])
12+
return b1, b2
13+
14+
15+
def test__stack_bands(b1_b2_arrs):
16+
"""_stack_bands input should be of type DatasetReader."""
17+
18+
# Test data
19+
b1, b2 = b1_b2_arrs
20+
21+
# Check ValueError for Dataset Reader
22+
with pytest.raises(
23+
AttributeError, match="The sources object should be Dataset Reader"
24+
):
25+
es._stack_bands([b1, b2])
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
""" Tests for the _stack_bands() method """
2+
3+
import numpy as np
4+
import pytest
5+
import earthpy.spatial as es
6+
7+
8+
@pytest.fixture
9+
def b1_b2_arrs():
10+
b1 = np.array([[6, 7, 8, 9, 10], [16, 17, 18, 19, 20]])
11+
b2 = np.array([[1, 2, 3, 4, 5], [14, 12, 13, 14, 17]])
12+
return b1, b2
13+
14+
15+
def test_stack_raster_tifs(b1_b2_arrs):
16+
"""stack_raster_tifs() should return a Warning."""
17+
18+
# Test data
19+
b1, b2 = b1_b2_arrs
20+
21+
# Check Warning for Deprecation
22+
with pytest.raises(Warning, match="stack_raster_tifs is deprecated"):
23+
es.stack_raster_tifs([b1, b2], out_path="test.tif")

0 commit comments

Comments
 (0)