def test_info_excel_file(self): """HRA: test excel files read correctly by _get_info_dataframe.""" from natcap.invest.hra import _get_info_dataframe # Make an info CSV file and read it as a dataframe info_csv_path = os.path.join(self.workspace_dir, 'info.csv') _make_info_csv( info_csv_path, workspace_dir=self.workspace_dir, rel_path=True) expected_df = _get_info_dataframe( info_csv_path, self.workspace_dir, self.workspace_dir, self.workspace_dir, '')[0].astype(str) # Since we don't have openpyxl library, use the existing excel file # from TEST_DATA folder, and copy it to self.workspace_dir so # the function won't raise exceptions about vector or raster files # in the table not existing info_excel_path = os.path.join(TEST_DATA, 'info_excel.xlsx') copied_info_excel_path = os.path.join( self.workspace_dir, 'info_excel.xlsx') shutil.copyfile(info_excel_path, copied_info_excel_path) out_df = _get_info_dataframe( copied_info_excel_path, self.workspace_dir, self.workspace_dir, self.workspace_dir, '')[0].astype(str) self.assertTrue( out_df.equals(expected_df), 'The dataframes read from info CSV and excel files are different.')
def test_wrong_layer_type_in_info_csv(self): """HRA: exception raised when layer type is wrong info CSV.""" from natcap.invest.hra import _get_info_dataframe # Test missing columns from info CSV bad_info_table_path = os.path.join(self.workspace_dir, 'bad_criteria.csv') _make_info_csv(bad_info_table_path, workspace_dir=self.workspace_dir, wrong_layer_type=True) with self.assertRaises(ValueError) as cm: _get_info_dataframe(bad_info_table_path, self.workspace_dir, self.workspace_dir, self.workspace_dir, '') expected_message = "is having ['wrong type']" actual_message = str(cm.exception) self.assertTrue(expected_message in actual_message, actual_message)
def test_wrong_buffer_in_info_csv(self): """HRA: exception raised when buffers are not number in info CSV.""" from natcap.invest.hra import _get_info_dataframe # Test missing columns from info CSV bad_info_table_path = os.path.join(self.workspace_dir, 'bad_criteria.csv') _make_info_csv(bad_info_table_path, workspace_dir=self.workspace_dir, wrong_buffer_value=True) with self.assertRaises(ValueError) as cm: _get_info_dataframe(bad_info_table_path, self.workspace_dir, self.workspace_dir, self.workspace_dir, '') expected_message = "should be a number for stressors" actual_message = str(cm.exception) self.assertTrue(expected_message in actual_message, actual_message)
def test_missing_columns_from_info_csv(self): """HRA: exception raised when columns are missing from info CSV.""" from natcap.invest.hra import _get_info_dataframe # Test missing columns from info CSV bad_info_table_path = os.path.join(self.workspace_dir, 'bad_criteria.csv') _make_info_csv(bad_info_table_path, workspace_dir=self.workspace_dir, missing_columns=True) with self.assertRaises(ValueError) as cm: _get_info_dataframe(bad_info_table_path, self.workspace_dir, self.workspace_dir, self.workspace_dir, '') expected_message = "'NAME', 'STRESSOR BUFFER (METERS)'" actual_message = str(cm.exception) self.assertTrue(expected_message in actual_message, actual_message)