def test_missing_keys(self): """Carbon Validate: assert missing required keys.""" from natcap.invest import carbon from natcap.invest import validation validation_errors = carbon.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_translate_formatted_string(self): """Translation: test that f-string can be translated.""" set_locale(TEST_LANG) importlib.reload(validation) importlib.reload(carbon) args = {'n_workers': 'not a number'} validation_messages = carbon.validate(args) self.assertIn( TEST_MESSAGES[not_a_number_msg].format(value=args['n_workers']), str(validation_messages))
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(self.base_required_keys + ['calc_sequestration', 'lulc_redd_path']) self.assertEqual(invalid_keys, expected_missing_keys)
def test_translate_formatted_string(self): with patch('natcap.invest.LOCALE_DIR', self.test_locale_dir): install_language('en') importlib.reload(validation) importlib.reload(carbon) args = {'n_workers': 'not a number'} validation_messages = carbon.validate(args) self.assertIn( TEST_MESSAGES[not_a_number_msg].format(value=args['n_workers']), str(validation_messages))
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(self.base_required_keys + [ 'calc_sequestration', 'price_per_metric_ton_of_c', 'discount_rate', 'rate_change' ]) self.assertEqual(invalid_keys, expected_missing_keys)
def test_get_invest_validate(self): """UI server: get_invest_validate endpoint.""" from natcap.invest import carbon test_client = ui_server.app.test_client() args = {'workspace_dir': 'foo'} payload = { 'model_module': carbon.ARGS_SPEC['pyname'], 'args': json.dumps(args) } response = test_client.post('/validate', json=payload) results = json.loads(response.get_data(as_text=True)) expected = carbon.validate(args) # These differ only because a tuple was transformed to a list during # the json (de)serializing, so do the same with expected data self.assertEqual(results, json.loads(json.dumps(expected)))