def test_build_file_registry_duplicate_paths(self): """InVEST test that file registry recognizes duplicate paths.""" from natcap.invest import utils with self.assertRaises(ValueError): utils.build_file_registry([({ 'a': 'a.tif' }, ''), ({ 'b': 'a.tif' }, '')], '')
def test_build_file_registry_invalid_value(self): """Utils: test build_file_registry with invalid path type.""" from natcap.invest import utils base_dict = {'foo': 'bar', 'baz': None} with self.assertRaises(ValueError): _ = utils.build_file_registry([(base_dict, 'somepath')], '')
def test_build_file_registry_duppath(self): """Utils: test build_file_registry ValueError on duplicate paths.""" from natcap.invest import utils base_dict = {'foo': 'bar', 'jab': 'bar'} with self.assertRaises(ValueError): _ = utils.build_file_registry([(base_dict, 'newpath')], '')
def test_build_file_registry(self): """Utils: test build_file_registry on simple case.""" from natcap.invest import utils base_dict = {'foo': 'bar', 'baz': '/bart/bam.txt'} file_registry = utils.build_file_registry([(base_dict, '')], '') self.assertEqual(FileRegistryUtilsTests._norm_dict(base_dict), FileRegistryUtilsTests._norm_dict(file_registry))
def test_build_file_registry_list_suffix(self): """Utils: test build_file_registry on list of files w/ suffix.""" from natcap.invest import utils base_dict = {'foo': ['bar', '/bart/bam.txt']} file_registry = utils.build_file_registry([(base_dict, '')], '_suff') expected_dict = {'foo': ['bar_suff', '/bart/bam_suff.txt']} self.assertEqual(FileRegistryUtilsTests._norm_dict(expected_dict), FileRegistryUtilsTests._norm_dict(file_registry))
def test_build_file_registry_dupkeys(self): """Utils: test build_file_registry ValueError on duplicate keys.""" from natcap.invest import utils base_dict1 = { 'foo': 'bar', } base_dict2 = { 'foo': 'bar2', } with self.assertRaises(ValueError): _ = utils.build_file_registry([(base_dict1, ''), (base_dict2, '')], '')
def test_build_file_registry_path(self): """Utils: test build_file_registry on path.""" from natcap.invest import utils base_dict = {'foo': 'bar', 'baz': '/bart/bam.txt', 'jab': 'jim'} file_registry = utils.build_file_registry([(base_dict, 'newpath')], '') expected_dict = { 'foo': 'newpath/bar', 'jab': 'newpath/jim', 'baz': '/bart/bam.txt', } self.assertEqual(FileRegistryUtilsTests._norm_dict(expected_dict), FileRegistryUtilsTests._norm_dict(file_registry))
def test_build_file_registry(self): """InVEST test a complicated file registry creation.""" from natcap.invest import utils dict_a = { 'a': 'aggregated_results.shp', 'b': 'P.tif', '': 'CN.tif', 'l_avail_path': ''} dict_b = { 'apple': '.shp', 'bear': 'tif', 'cat': 'CN.tif'} dict_c = {} result = utils.build_file_registry( [(dict_a, ''), (dict_b, 'foo'), (dict_c, 'garbage')], '') expected_dict = { 'a': 'aggregated_results.shp', 'b': 'P.tif', '': 'CN.tif', 'l_avail_path': '', 'apple': os.path.join('foo', '.shp'), 'bear': os.path.join('foo', 'tif'), 'cat': os.path.join('foo', 'CN.tif'), } unexpected_paths = [] for key, result_path in expected_dict.items(): expected_path = os.path.normpath(result[key]) if os.path.normpath(result_path) != expected_path: unexpected_paths.append( (key, expected_path, os.path.normpath(result_path))) extra_keys = set(result.keys()).difference(set(expected_dict.keys())) if len(unexpected_paths) > 0 or len(extra_keys) > 0: raise AssertionError( "Unexpected paths or keys: %s %s" % ( str(unexpected_paths), str(extra_keys)))