def test_get_datastack_info_logfile_iui_style(self): """Datastack: test get datastack info logfile iui style.""" from natcap.invest import datastack logfile_path = os.path.join(self.workspace, 'logfile.txt') with open(logfile_path, 'w') as logfile: logfile.write( textwrap.dedent(""" Arguments: suffix foo some_int 1 some_float 2.33 workspace_dir some_workspace_dir some other logging here. """)) expected_args = { 'suffix': 'foo', 'some_int': 1, 'some_float': 2.33, 'workspace_dir': 'some_workspace_dir', } stack_type, stack_info = datastack.get_datastack_info(logfile_path) self.assertEqual(stack_type, 'logfile') self.assertEqual( stack_info, datastack.ParameterSet(expected_args, datastack.UNKNOWN, datastack.UNKNOWN))
def test_extract_parameters_from_logfile(self): """Datastacks: Verify we can read args from a logfile.""" from natcap.invest import datastack logfile_path = os.path.join(self.workspace, 'logfile') with open(logfile_path, 'w') as logfile: logfile.write( textwrap.dedent(""" 07/20/2017 16:37:48 natcap.invest.ui.model INFO Arguments for InVEST some_model some_version: suffix foo some_int 1 some_float 2.33 workspace_dir some_workspace_dir 07/20/2017 16:37:48 natcap.invest.ui.model INFO post args. """)) params = datastack.extract_parameters_from_logfile(logfile_path) expected_params = datastack.ParameterSet( { 'suffix': 'foo', 'some_int': 1, 'some_float': 2.33, 'workspace_dir': 'some_workspace_dir' }, 'some_model', 'some_version') self.assertEqual(params, expected_params)
def test_get_datatack_info_parameter_set(self): import natcap.invest from natcap.invest import datastack params = { 'a': 1, 'b': u'hello there', 'c': 'plain bytestring', 'd': '', } json_path = os.path.join(self.workspace, 'archive.invs.json') datastack.build_parameter_set(params, 'sample_model', json_path) stack_type, stack_info = datastack.get_datastack_info(json_path) self.assertEqual(stack_type, 'json') self.assertEqual(stack_info, datastack.ParameterSet( params, 'sample_model', natcap.invest.__version__))
def test_get_datastack_info_logfile_new_style(self): import natcap.invest from natcap.invest import datastack args = { 'a': 1, 'b': 2.7, 'c': [1, 2, 3.55], 'd': 'hello, world!', 'e': False, } logfile_path = os.path.join(self.workspace, 'logfile.txt') with open(logfile_path, 'w') as logfile: logfile.write(datastack.format_args_dict(args, 'some_modelname')) stack_type, stack_info = datastack.get_datastack_info(logfile_path) self.assertEqual(stack_type, 'logfile') self.assertEqual(stack_info, datastack.ParameterSet( args, 'some_modelname', natcap.invest.__version__))
def test_get_datastack_info_archive(self): """Datastacks: verify we can get info from an archive.""" import natcap.invest from natcap.invest import datastack params = { 'a': 1, 'b': u'hello there', 'c': 'plain bytestring', 'd': '', } archive_path = os.path.join(self.workspace, 'archive.invs.tar.gz') datastack.build_datastack_archive(params, 'sample_model', archive_path) stack_type, stack_info = datastack.get_datastack_info(archive_path) self.assertEqual(stack_type, 'archive') self.assertEqual(stack_info, datastack.ParameterSet( params, 'sample_model', natcap.invest.__version__))
def test_get_datastack_info_parameter_set(self): """Datastack: test get datastack info parameter set.""" import natcap.invest from natcap.invest import datastack params = { 'a': 1, 'b': 'hello there', 'c': 'plain bytestring', 'd': '', } test_module_name = 'test_datastack_modules.simple_parameters' json_path = os.path.join(self.workspace, 'archive.invs.json') datastack.build_parameter_set(params, test_module_name, json_path) stack_type, stack_info = datastack.get_datastack_info(json_path) self.assertEqual(stack_type, 'json') self.assertEqual( stack_info, datastack.ParameterSet(params, test_module_name, natcap.invest.__version__))