tests for globio validate function. #BITBUCKET-3912.
This commit is contained in:
parent
3f9afb73d5
commit
71bf4814d1
|
@ -45,7 +45,7 @@ ARGS_SPEC = {
|
|||
},
|
||||
"lulc_path": {
|
||||
"type": "raster",
|
||||
"required": "~predefined_globio",
|
||||
"required": "not predefined_globio",
|
||||
"about": "used in \"mode (a)\" path to a base landcover map with integer codes",
|
||||
"name": "Land Use/Cover (Raster)"
|
||||
},
|
||||
|
@ -54,7 +54,7 @@ ARGS_SPEC = {
|
|||
"required_fields": ["lucode", "globio_lucode"],
|
||||
},
|
||||
"type": "csv",
|
||||
"required": "~predefined_globio",
|
||||
"required": "not predefined_globio",
|
||||
"about": (
|
||||
"A CSV table containing model information corresponding to "
|
||||
"each of the land use classes in the LULC raster input. It "
|
||||
|
@ -78,13 +78,13 @@ ARGS_SPEC = {
|
|||
},
|
||||
"pasture_path": {
|
||||
"type": "raster",
|
||||
"required": "~predefined_globio",
|
||||
"required": "not predefined_globio",
|
||||
"about": "used in \"mode (a)\" path to pasture raster",
|
||||
"name": "Pasture (Raster)"
|
||||
},
|
||||
"potential_vegetation_path": {
|
||||
"type": "raster",
|
||||
"required": "~predefined_globio",
|
||||
"required": "not predefined_globio",
|
||||
"about": "used in \"mode (a)\" path to potential vegetation raster",
|
||||
"name": "Potential Vegetation (Raster)"
|
||||
},
|
||||
|
@ -93,7 +93,7 @@ ARGS_SPEC = {
|
|||
"expression": "(value >= 0) & (value <= 1)",
|
||||
},
|
||||
"type": "number",
|
||||
"required": "~predefined_globio",
|
||||
"required": "not predefined_globio",
|
||||
"about": "used in \"mode (a)\"",
|
||||
"name": "Pasture Threshold"
|
||||
},
|
||||
|
@ -113,7 +113,7 @@ ARGS_SPEC = {
|
|||
"expression": "(value >= 0) & (value <= 1)",
|
||||
},
|
||||
"type": "number",
|
||||
"required": "~predefined_globio",
|
||||
"required": "not predefined_globio",
|
||||
"about": "used in \"mode (a)\"",
|
||||
"name": "Primary Threshold"
|
||||
},
|
||||
|
|
|
@ -256,30 +256,6 @@ class CarbonTests(unittest.TestCase):
|
|||
os.path.join(args['workspace_dir'], 'npv_redd.tif'), -0.4602106)
|
||||
|
||||
|
||||
# def generate_base_args():
|
||||
# args = {
|
||||
# 'workspace_dir': self.workspace_dir,
|
||||
# 'results_suffix': 'foo',
|
||||
# 'n_workers': -1,
|
||||
# 'lulc_cur_path': 'some_path.tif',
|
||||
# 'calc_sequestration': True,
|
||||
# 'lulc_fut_path': 'some_path.tif',
|
||||
# 'do_redd': True,
|
||||
# 'lulc_redd_path': 'some_path.tif',
|
||||
# 'carbon_pools_path': 'some_path.csv',
|
||||
# 'lulc_cur_year': 2016,
|
||||
# 'lulc_fut_year': 2030,
|
||||
# 'do_valuation': True,
|
||||
# 'price_per_metric_ton_of_c': 43.0,
|
||||
# 'discount_rate': -7.1,
|
||||
# 'rate_change': 2.8,
|
||||
# }
|
||||
# from natcap.invest.carbon import ARGS_SPEC
|
||||
# # assert base args isn't missing any args.
|
||||
# assert set(ARGS_SPEC.keys()).is_subset(set(args.keys()))
|
||||
# return args
|
||||
|
||||
|
||||
class CarbonValidationTests(unittest.TestCase):
|
||||
"""Tests for the Carbon Model ARGS_SPEC and validation."""
|
||||
|
||||
|
|
|
@ -232,3 +232,66 @@ class GLOBIOTests(unittest.TestCase):
|
|||
result_layer = None
|
||||
gdal.Dataset.__swig_destroy__(result_vector)
|
||||
result_vector = None
|
||||
|
||||
class GlobioValidationTests(unittest.TestCase):
|
||||
"""Tests for the GLOBIO Model ARGS_SPEC and validation."""
|
||||
|
||||
def setUp(self):
|
||||
"""Create a temporary workspace."""
|
||||
self.workspace_dir = tempfile.mkdtemp()
|
||||
self.base_required_keys = [
|
||||
'primary_threshold',
|
||||
'pasture_path',
|
||||
'pasture_threshold',
|
||||
'lulc_path',
|
||||
'potential_vegetation_path',
|
||||
'msa_parameters_path',
|
||||
'lulc_to_globio_table_path',
|
||||
'workspace_dir',
|
||||
'intensification_fraction',
|
||||
'infrastructure_dir',
|
||||
]
|
||||
|
||||
def tearDown(self):
|
||||
"""Remove the temporary workspace after a test."""
|
||||
shutil.rmtree(self.workspace_dir)
|
||||
|
||||
def test_missing_keys(self):
|
||||
"""GLOBIO Validate: assert missing required keys."""
|
||||
from natcap.invest import globio
|
||||
from natcap.invest import validation
|
||||
|
||||
validation_errors = globio.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_predefined_globio(self):
|
||||
"""GLOBIO Validate: assert missing required keys with predifined GLOBIO."""
|
||||
from natcap.invest import globio
|
||||
from natcap.invest import validation
|
||||
|
||||
validation_errors = globio.validate({'predefined_globio': True})
|
||||
invalid_keys = validation.get_invalid_keys(validation_errors)
|
||||
expected_missing_keys = set(
|
||||
['workspace_dir',
|
||||
'infrastructure_dir',
|
||||
'intensification_fraction',
|
||||
'msa_parameters_path',
|
||||
'globio_lulc_path'])
|
||||
self.assertEqual(invalid_keys, expected_missing_keys)
|
||||
|
||||
def test_missing_field_in_msa_parameters(self):
|
||||
"""GLOBIO Validate: warning message on invalid fields."""
|
||||
from natcap.invest import globio
|
||||
msa_parameters_path = os.path.join(self.workspace_dir, 'bad_table.csv')
|
||||
with open(msa_parameters_path, 'w') as file:
|
||||
file.write('foo,bar\n')
|
||||
file.write('1,2\n')
|
||||
validation_warnings = globio.validate(
|
||||
{'msa_parameters_path': msa_parameters_path})
|
||||
expected_message = "Fields are missing from this table: ['MEASUREMENT', 'MSA_TYPE', 'MSA_X', 'SE', 'VALUE']"
|
||||
actual_messages = set()
|
||||
for keys, error_strings in validation_warnings:
|
||||
actual_messages.add(error_strings)
|
||||
self.assertTrue(expected_message in actual_messages)
|
Loading…
Reference in New Issue