def test_validation(self): """NDR test argument validation.""" from natcap.invest.ndr import ndr # use predefined directory so test can clean up files during teardown args = NDRTests.generate_base_args(self.workspace_dir) # should not raise an exception ndr.validate(args) with self.assertRaises(KeyError) as context: del args['workspace_dir'] ndr.validate(args) self.assertTrue( 'The following keys were expected' in str(context.exception)) args = NDRTests.generate_base_args(self.workspace_dir) args['workspace_dir'] = '' validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) # here the wrong GDAL type happens (vector instead of raster) args = NDRTests.generate_base_args(self.workspace_dir) args['lulc_path'] = args['watersheds_path'] validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) # here the wrong GDAL type happens (raster instead of vector) args = NDRTests.generate_base_args(self.workspace_dir) args['watersheds_path'] = args['lulc_path'] validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) # cover that there's no p and n calculation args = NDRTests.generate_base_args(self.workspace_dir) args['calc_p'] = False args['calc_n'] = False validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) # cover that a file is missing args = NDRTests.generate_base_args(self.workspace_dir) args['lulc_path'] = 'this/path/does/not/exist.tif' validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1)
def test_validation(self): """NDR test argument validation.""" from natcap.invest.ndr import ndr from natcap.invest import validation # use predefined directory so test can clean up files during teardown args = NDRTests.generate_base_args(self.workspace_dir) # should not raise an exception ndr.validate(args) del args['workspace_dir'] validation_errors = ndr.validate(args) self.assertEquals(len(validation_errors), 1) args = NDRTests.generate_base_args(self.workspace_dir) args['workspace_dir'] = '' validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) # here the wrong GDAL type happens (vector instead of raster) args = NDRTests.generate_base_args(self.workspace_dir) args['lulc_path'] = args['watersheds_path'] validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) # here the wrong GDAL type happens (raster instead of vector) args = NDRTests.generate_base_args(self.workspace_dir) args['watersheds_path'] = args['lulc_path'] validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) # cover that there's no p and n calculation args = NDRTests.generate_base_args(self.workspace_dir) args['calc_p'] = False args['calc_n'] = False validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) self.assertTrue('calc_n' in validation_error_list[0][0] and 'calc_p' in validation_error_list[0][0]) # cover that a file is missing args = NDRTests.generate_base_args(self.workspace_dir) args['lulc_path'] = 'this/path/does/not/exist.tif' validation_error_list = ndr.validate(args) # we should have one warning that is an empty value self.assertEqual(len(validation_error_list), 1) # cover that some args are conditionally required when # these args are present and true args = {'calc_p': True, 'calc_n': True} validation_error_list = ndr.validate(args) invalid_args = validation.get_invalid_keys(validation_error_list) expected_missing_args = [ 'biophysical_table_path', 'threshold_flow_accumulation', 'dem_path', 'subsurface_critical_length_n', 'subsurface_critical_length_p', 'runoff_proxy_path', 'lulc_path', 'workspace_dir', 'k_param', 'watersheds_path', 'subsurface_eff_p', 'subsurface_eff_n', ] self.assertEqual(set(invalid_args), set(expected_missing_args))