1+ """Test /api/files.""" 
2+ 
13# fmt:off 
4+ # pylint: skip-file 
25
36from  __future__ import  absolute_import , division , print_function 
47
58import  hashlib 
69import  os 
710import  unittest 
11+ from  typing  import  Any , Dict , List , Optional 
812
9- from  tornado .escape  import  json_decode , json_encode 
10- 
11- # local imports 
12- from  rest_tools .client  import  RestClient 
13+ from  rest_tools .client  import  RestClient   # type: ignore[import] 
14+ from  tornado .escape  import  json_encode 
1315
1416from  .test_server  import  TestServerAPI 
1517
1618
17- def  hex (data ):
19+ def  hex (data : Any ) ->  str :
20+     """Get sha512.""" 
1821    if  isinstance (data , str ):
19-         data  =  data .encode (' utf-8' 
22+         data  =  data .encode (" utf-8" 
2023    return  hashlib .sha512 (data ).hexdigest ()
2124
25+ 
2226class  TestFilesAPI (TestServerAPI ):
2327    def  test_10_files (self ):
2428        self .start_server ()
@@ -111,7 +115,7 @@ def test_12_files_keys(self):
111115            "locations" ,
112116            "extra" ,
113117            "supplemental" ,
114-             "meta_modify_date" 
118+             "meta_modify_date" , 
115119        }
116120
117121        # w/ all-keys = False 
@@ -130,7 +134,7 @@ def test_12_files_keys(self):
130134            "locations" ,
131135            "extra" ,
132136            "supplemental" ,
133-             "meta_modify_date" 
137+             "meta_modify_date" , 
134138        }
135139
136140        # w/ all-keys = False & keys 
@@ -143,6 +147,78 @@ def test_12_files_keys(self):
143147        data  =  r .request_seq ("GET" , "/api/files" , args )
144148        assert  set (data ["files" ][0 ].keys ()) ==  {"checksum" , "file_size" }
145149
150+     def  test_13_files_path_like_args (self ):
151+         """Test the path-like base/shortcut arguments. 
152+ 
153+         "logical_name", "directory", "filename", "path", & "path-regex". 
154+         """ 
155+         self .start_server ()  # type: ignore[no-untyped-call] 
156+         token  =  self .get_token ()  # type: ignore[no-untyped-call] 
157+         r  =  RestClient (self .address , token , timeout = 1 , retries = 1 )
158+ 
159+         metadata_objs  =  [
160+             {
161+                 "logical_name" : "/foo/bar/baz/bat.txt" ,
162+                 "checksum" : {"sha512" : hex ("1" )},
163+                 "file_size" : 1 ,
164+                 "locations" : [{"site" : "test" , "path" : "foo/bar/baz/bat.txt" }],
165+             },
166+             {
167+                 "logical_name" : "/foo/bar/ham.txt" ,
168+                 "checksum" : {"sha512" : hex ("2" )},
169+                 "file_size" : 2 ,
170+                 "locations" : [{"site" : "test" , "path" : "/foo/bar/ham.txt" }],
171+             },
172+             {
173+                 "logical_name" : "/green/eggs/and/ham.txt" ,
174+                 "checksum" : {"sha512" : hex ("3" )},
175+                 "file_size" : 3 ,
176+                 "locations" : [{"site" : "test" , "path" : "/green/eggs/and/ham.txt" }],
177+             },
178+             {
179+                 "logical_name" : "/john/paul/george/ringo/ham.txt" ,
180+                 "checksum" : {"sha512" : hex ("4" )},
181+                 "file_size" : 4 ,
182+                 "locations" : [
183+                     {"site" : "test" , "path" : "/john/paul/george/ringo/ham.txt" }
184+                 ],
185+             },
186+         ]
187+         for  meta  in  metadata_objs :
188+             r .request_seq ("POST" , "/api/files" , meta )
189+ 
190+         def  get_paths (args : Optional [Dict [str , str ]] =  None ) ->  List [str ]:
191+             if  not  args :
192+                 args  =  {}
193+             ret  =  r .request_seq ("GET" , "/api/files" , args )
194+             print (ret )
195+             return  [f ["logical_name" ] for  f  in  ret ["files" ]]
196+ 
197+         assert  len (get_paths ()) ==  4 
198+         # logical_name 
199+         assert  len (get_paths ({"logical_name" : "/foo/bar/ham.txt" })) ==  1 
200+         # path 
201+         assert  len (get_paths ({"path" : "/green/eggs/and/ham.txt" })) ==  1 
202+         # directory 
203+         paths  =  get_paths ({"directory" : "/foo/bar" })
204+         assert  set (paths ) ==  {"/foo/bar/ham.txt" , "/foo/bar/baz/bat.txt" }
205+         assert  len (get_paths ({"directory" : "/fo" })) ==  0 
206+         # filename 
207+         paths  =  get_paths ({"filename" : "ham.txt" })
208+         assert  set (paths ) ==  {
209+             "/foo/bar/ham.txt" ,
210+             "/green/eggs/and/ham.txt" ,
211+             "/john/paul/george/ringo/ham.txt" ,
212+         }
213+         assert  len (get_paths ({"filename" : ".txt" })) ==  0 
214+         # directory & filename 
215+         paths  =  get_paths ({"directory" : "/foo" , "filename" : "ham.txt" })
216+         assert  paths  ==  ["/foo/bar/ham.txt" ]
217+         # path-regex 
218+         paths  =  get_paths ({"path-regex" : r".*george/ringo.*" })
219+         assert  paths  ==  ["/john/paul/george/ringo/ham.txt" ]
220+         assert  len (get_paths ({"path-regex" : r".*" })) ==  4 
221+ 
146222    def  test_15_files_auth (self ):
147223        self .start_server (config_override = {'SECRET' :'secret' })
148224        token  =  self .get_token ()
0 commit comments