Skip to content

Commit 2f972ea

Browse files
added tests
1 parent f756f47 commit 2f972ea

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

climada/util/test/test_coordinates.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,62 @@ def test_mask_raster_with_geometry(self):
22942294
)
22952295

22962296

2297+
class TestBoundsFromUserInput(unittest.TestCase):
2298+
"""Unit tests for the bounds_from_user_input function."""
2299+
2300+
def test_boundsNESW_from_global(self):
2301+
"""Test for 'global' area selection."""
2302+
result = u_coord.boundsNESW_from_global()
2303+
expected = [90, 180, -90, -180]
2304+
np.testing.assert_almost_equal(result, expected)
2305+
2306+
def test_boundsNESW_from_country_codes(self):
2307+
"""Test for a list of ISO country codes."""
2308+
result = u_coord.boundsNESW_from_country_codes(
2309+
["ITA"], rel_margin=0.2
2310+
) # Testing with Italy (ITA)
2311+
# Real expected bounds for Italy (calculated or manually known)
2312+
expected = [
2313+
49.404409157600064,
2314+
20.900365510000075,
2315+
33.170049669400036,
2316+
4.219788779000066,
2317+
] # Italy's bounding box
2318+
2319+
np.testing.assert_array_almost_equal(result, expected, decimal=4)
2320+
2321+
def test_bounding_box(self):
2322+
"""Test for bounding box input with margin applied."""
2323+
[north, east, south, west] = [50, -100, 30, -120]
2324+
result = u_coord.boundsNESW_from_NESW(
2325+
north=north, south=south, west=west, east=east, rel_margin=0.1
2326+
)
2327+
expected = [
2328+
50 + 2,
2329+
-100 + 2,
2330+
30 - 2,
2331+
-120 - 2,
2332+
] # Apply margin calculation
2333+
np.testing.assert_array_almost_equal(result, expected)
2334+
2335+
def test_invalid_input_string(self):
2336+
"""Test for invalid string input."""
2337+
with self.assertRaises(Exception):
2338+
u_coord.boundsNESW_from_country_codes("DEU")
2339+
2340+
def test_empty_input(self):
2341+
"""Test for empty input."""
2342+
with self.assertRaises(Exception):
2343+
u_coord.boundsNESW_from_country_codes([])
2344+
2345+
def test_invalid_coordinate_input(self):
2346+
"""Test for str in coordinates input input."""
2347+
with self.assertRaises(ValueError):
2348+
u_coord.boundsNESW_from_NESW(north=40, south=50, east=30, west=10)
2349+
with self.assertRaises(TypeError):
2350+
u_coord.boundsNESW_from_NESW(north=40, south="20", east=30, west=10)
2351+
2352+
22972353
# Execute Tests
22982354
if __name__ == "__main__":
22992355
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestFunc)
@@ -2302,4 +2358,5 @@ def test_mask_raster_with_geometry(self):
23022358
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestRasterMeta))
23032359
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestRasterIO))
23042360
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestDistance))
2361+
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestBoundsFromUserInput))
23052362
unittest.TextTestRunner(verbosity=2).run(TESTS)

0 commit comments

Comments
 (0)