66import unittest
77
88from climada .util .files_handler import to_list , get_file_names , download_file
9- from climada .util .constants import DATA_DIR
9+ from climada .util .constants import DATA_DIR , GLB_CENTROIDS_MAT , ENT_TEMPLATE_XLS
1010
1111class TestDownloadUrl (unittest .TestCase ):
1212 """Test download_file function """
@@ -53,16 +53,17 @@ def test_list_wrong_length_fail(self):
5353 self .assertIn ("Provide one or 3 values." , cm .output [0 ])
5454
5555class TestGetFileNames (unittest .TestCase ):
56- """Test get_file_names function"""
56+ """ Test get_file_names function. Only works with actually existing
57+ files and directories. """
5758 def test_one_file_copy (self ):
5859 """If input is one file name, return a list with this file name"""
59- file_name = "test.mat"
60+ file_name = GLB_CENTROIDS_MAT
6061 out = get_file_names (file_name )
6162 self .assertEqual ([file_name ], out )
6263
6364 def test_several_file_copy (self ):
6465 """If input is a list with several file names, return the same list"""
65- file_name = ["test1.mat" , "test2.mat" , "test3.mat" , "test4.mat" ]
66+ file_name = [GLB_CENTROIDS_MAT , ENT_TEMPLATE_XLS ]
6667 out = get_file_names (file_name )
6768 self .assertEqual (file_name , out )
6869
@@ -79,6 +80,19 @@ def test_folder_contents(self):
7980 for file in out :
8081 self .assertNotEqual ('' , os .path .splitext (file )[1 ])
8182
83+ def test_globbing (self ):
84+ """ If input is a glob pattern, return a list of matching visible
85+ files; omit folders.
86+ """
87+ file_name = os .path .join (DATA_DIR , 'demo/' )
88+ out = get_file_names (file_name + '*' )
89+
90+ tmp_files = os .listdir (file_name )
91+ tmp_files = [file_name + f for f in tmp_files ]
92+ tmp_files = [f for f in tmp_files if not os .path .isdir (f )
93+ and not f .startswith ('.' )]
94+ self .assertEqual (tmp_files , out )
95+
8296# Execute Tests
8397TESTS = unittest .TestLoader ().loadTestsFromTestCase (TestToStrList )
8498TESTS .addTests (unittest .TestLoader ().loadTestsFromTestCase (TestGetFileNames ))
0 commit comments