added tests for CV validate function. #BITBUCKET-3912.
This commit is contained in:
parent
89e8b8637b
commit
747af5e48e
|
@ -286,6 +286,11 @@ class CarbonValidationTests(unittest.TestCase):
|
|||
def setUp(self):
|
||||
"""Create a temporary workspace."""
|
||||
self.workspace_dir = tempfile.mkdtemp()
|
||||
self.base_required_keys = [
|
||||
'workspace_dir',
|
||||
'lulc_cur_path',
|
||||
'carbon_pools_path',
|
||||
]
|
||||
|
||||
def tearDown(self):
|
||||
"""Remove the temporary workspace after a test."""
|
||||
|
@ -298,60 +303,50 @@ class CarbonValidationTests(unittest.TestCase):
|
|||
|
||||
validation_errors = carbon.validate({}) # empty args dict.
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set([
|
||||
'workspace_dir',
|
||||
'lulc_cur_path',
|
||||
'carbon_pools_path',
|
||||
])
|
||||
expected_missing_keys = set(self.base_required_keys)
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
||||
def test_missing_keys_sequestration(self):
|
||||
"""Carbon Validate: assert missing calc_sequestration keys."""
|
||||
from natcap.invest import carbon
|
||||
from natcap.invest import validation
|
||||
|
||||
args = {'calc_sequestration': True}
|
||||
validation_errors = carbon.validate(args)
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set([
|
||||
'workspace_dir',
|
||||
'lulc_cur_path',
|
||||
'carbon_pools_path',
|
||||
'lulc_cur_year',
|
||||
'lulc_fut_year',
|
||||
'lulc_fut_path',
|
||||
])
|
||||
expected_missing_keys = set(
|
||||
self.base_required_keys +
|
||||
['lulc_cur_year',
|
||||
'lulc_fut_year',
|
||||
'lulc_fut_path'])
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
||||
def test_missing_keys_redd(self):
|
||||
"""Carbon Validate: assert missing do_redd keys."""
|
||||
from natcap.invest import carbon
|
||||
from natcap.invest import validation
|
||||
|
||||
args = {'do_redd': True}
|
||||
validation_errors = carbon.validate(args)
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set([
|
||||
'workspace_dir',
|
||||
'lulc_cur_path',
|
||||
'carbon_pools_path',
|
||||
'calc_sequestration',
|
||||
'lulc_redd_path',
|
||||
])
|
||||
expected_missing_keys = set(
|
||||
self.base_required_keys +
|
||||
['calc_sequestration',
|
||||
'lulc_redd_path'])
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
||||
def test_missing_keys_valuation(self):
|
||||
"""Carbon Validate: assert missing do_valuation keys."""
|
||||
from natcap.invest import carbon
|
||||
from natcap.invest import validation
|
||||
|
||||
args = {'do_valuation': True}
|
||||
validation_errors = carbon.validate(args)
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set([
|
||||
'workspace_dir',
|
||||
'lulc_cur_path',
|
||||
'carbon_pools_path',
|
||||
'calc_sequestration',
|
||||
'price_per_metric_ton_of_c',
|
||||
'discount_rate',
|
||||
'rate_change',
|
||||
])
|
||||
expected_missing_keys = set(
|
||||
self.base_required_keys +
|
||||
['calc_sequestration',
|
||||
'price_per_metric_ton_of_c',
|
||||
'discount_rate',
|
||||
'rate_change'])
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
|
|
@ -603,26 +603,6 @@ class CoastalVulnerabilityTests(unittest.TestCase):
|
|||
# It's all nodata going in, so should be all same nodata out.
|
||||
numpy.testing.assert_array_equal(array, pos_array)
|
||||
|
||||
def test_exception_from_validate_polyline(self):
|
||||
"""CV: raise ValueError on incorrect geometry type during validation.
|
||||
|
||||
shelf_contour_vector_path must be a line geometry, here it's a polygon.
|
||||
"""
|
||||
gpkg_driver = ogr.GetDriverByName("GPKG")
|
||||
shelf_poly_path = os.path.join(self.workspace_dir, 'shelf_poly.gpkg')
|
||||
vector = gpkg_driver.CreateDataSource(shelf_poly_path)
|
||||
srs = osr.SpatialReference()
|
||||
srs.ImportFromEPSG(4326)
|
||||
vector.CreateLayer('layer', srs, ogr.wkbPolygon)
|
||||
vector = None
|
||||
args = CoastalVulnerabilityTests.generate_base_args(self.workspace_dir)
|
||||
args['shelf_contour_vector_path'] = shelf_poly_path
|
||||
with self.assertRaises(ValueError):
|
||||
err_list = coastal_vulnerability.validate(args)
|
||||
for keys, err_strings in err_list:
|
||||
if 'Must be a polyline vector' in err_strings:
|
||||
raise ValueError(err_list)
|
||||
|
||||
def test_shore_points_on_single_polygon(self):
|
||||
"""CV: test shore point creation with single polygon landmass."""
|
||||
args = CoastalVulnerabilityTests.generate_base_args(self.workspace_dir)
|
||||
|
@ -754,3 +734,109 @@ def assert_pickled_arrays_almost_equal(
|
|||
actual_values, expected_values, decimal=2)
|
||||
numpy.testing.assert_array_equal(actual_fids, expected_fids)
|
||||
|
||||
class CoastalVulnerabilityValidationTests(unittest.TestCase):
|
||||
"""Tests for the CV Model ARGS_SPEC and validation."""
|
||||
|
||||
def setUp(self):
|
||||
"""Create a temporary workspace."""
|
||||
self.workspace_dir = tempfile.mkdtemp()
|
||||
self.base_required_keys = [
|
||||
'workspace_dir',
|
||||
'aoi_vector_path',
|
||||
'model_resolution',
|
||||
'landmass_vector_path',
|
||||
'wwiii_vector_path',
|
||||
'max_fetch_distance',
|
||||
'shelf_contour_vector_path',
|
||||
'dem_path',
|
||||
'dem_averaging_radius',
|
||||
'habitat_table_path',
|
||||
]
|
||||
|
||||
def tearDown(self):
|
||||
"""Remove the temporary workspace after a test."""
|
||||
shutil.rmtree(self.workspace_dir)
|
||||
|
||||
def test_missing_keys(self):
|
||||
"""CV Validate: assert missing required keys."""
|
||||
from natcap.invest import coastal_vulnerability
|
||||
from natcap.invest import validation
|
||||
|
||||
validation_errors = coastal_vulnerability.validate({}) # empty args dict.
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set(
|
||||
self.base_required_keys)
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
||||
def test_missing_keys_geomorphology(self):
|
||||
"""CV Validate: assert missing geomorphology keys."""
|
||||
from natcap.invest import coastal_vulnerability
|
||||
from natcap.invest import validation
|
||||
|
||||
args = {'geomorphology_vector_path': 'foo.shp'}
|
||||
validation_errors = coastal_vulnerability.validate(args)
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set(
|
||||
self.base_required_keys +
|
||||
['geomorphology_fill_value',
|
||||
'geomorphology_vector_path'])
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
||||
def test_missing_keys_population(self):
|
||||
"""CV Validate: assert missing population keys."""
|
||||
from natcap.invest import coastal_vulnerability
|
||||
from natcap.invest import validation
|
||||
|
||||
args = {'population_raster_path': 'foo.tif'}
|
||||
validation_errors = coastal_vulnerability.validate(args)
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set(
|
||||
self.base_required_keys +
|
||||
['population_raster_path',
|
||||
'population_radius'])
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
||||
def test_missing_keys_sealevelrise(self):
|
||||
"""CV Validate: assert missing sealevelrise keys."""
|
||||
from natcap.invest import coastal_vulnerability
|
||||
from natcap.invest import validation
|
||||
|
||||
args = {'slr_vector_path': 'foo.shp'}
|
||||
validation_errors = coastal_vulnerability.validate(args)
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set(
|
||||
self.base_required_keys +
|
||||
['slr_vector_path',
|
||||
'slr_field'])
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
||||
def test_message_about_incorrect_geometry(self):
|
||||
"""CV Validate: test catching incorrect shelf contour geometry type.
|
||||
|
||||
shelf_contour_vector_path must be a line geometry, here it's a polygon.
|
||||
"""
|
||||
gpkg_driver = ogr.GetDriverByName("GPKG")
|
||||
shelf_poly_path = os.path.join(self.workspace_dir, 'shelf_poly.gpkg')
|
||||
vector = gpkg_driver.CreateDataSource(shelf_poly_path)
|
||||
srs = osr.SpatialReference()
|
||||
srs.ImportFromEPSG(4326)
|
||||
vector.CreateLayer('layer', srs, ogr.wkbPolygon)
|
||||
vector = None
|
||||
with self.assertRaises(ValueError):
|
||||
err_list = coastal_vulnerability.validate(
|
||||
{'shelf_contour_vector_path': shelf_poly_path})
|
||||
for keys, err_strings in err_list:
|
||||
if 'Must be a polyline vector' in err_strings:
|
||||
raise ValueError(err_list)
|
||||
|
||||
def test_missing_sealevelrise_field(self):
|
||||
"""CV Validate: test catching SLR field not present in vector."""
|
||||
slr_vector_path = os.path.join(
|
||||
INPUT_DATA, 'sea_level_rise.gpkg')
|
||||
with self.assertRaises(ValueError):
|
||||
err_list = coastal_vulnerability.validate(
|
||||
{'slr_vector_path': slr_vector_path,
|
||||
'slr_field': 'foo'})
|
||||
for keys, err_strings in err_list:
|
||||
if 'Value must be one of:' in err_strings:
|
||||
raise ValueError(err_strings)
|
||||
|
|
Loading…
Reference in New Issue