@@ -771,11 +771,17 @@ def test_plugins_download_http_assets_filename_from_get(
771771
772772 self .assertEqual (path , os .path .join (self .output_dir , "dummy_product" ))
773773 self .assertTrue (os .path .isdir (path ))
774- self .assertTrue (
775- os .path .isfile (
776- os .path .join (self .output_dir , "dummy_product" , "somethingelse" )
777- )
774+ asset_local_path = os .path .join (
775+ self .output_dir , "dummy_product" , "somethingelse"
776+ )
777+ self .assertTrue (os .path .isfile (asset_local_path ))
778+
779+ statements = plugin .check_cache (
780+ self .product .assets ["foo" ], output_dir = self .output_dir
778781 )
782+ self .assertIsNotNone (statements )
783+ self .assertEqual (statements ["href" ], "http://somewhere/something" )
784+ self .assertEqual (statements ["file:local_path" ], asset_local_path )
779785
780786 @mock .patch ("eodag.plugins.download.http.HTTPDownload._get_asset_sizes" )
781787 @mock .patch ("eodag.plugins.download.http.requests.head" , autospec = True )
@@ -1129,7 +1135,8 @@ def test_plugins_download_http_asset_filter(
11291135 """HTTPDownload.download() must create an outputfile"""
11301136
11311137 plugin = self .get_download_plugin (self .product )
1132- self .product .location = self .product .remote_location = "http://somewhere"
1138+ self .product .collection = "FOO"
1139+ self .product .location = self .product .remote_location = "http://foo"
11331140 self .product .properties ["id" ] = "someproduct"
11341141 self .product .assets .clear ()
11351142 self .product .assets .update (
@@ -1147,20 +1154,90 @@ def test_plugins_download_http_asset_filter(
11471154 mock_requests_head .return_value .headers = CaseInsensitiveDict (
11481155 {"Content-Disposition" : "" }
11491156 )
1157+ expected_hashes = {
1158+ "somewhere" : hashlib .md5 (
1159+ "FOO-someproduct-somewhere" .encode ("utf-8" )
1160+ ).hexdigest (),
1161+ "elsewhere" : hashlib .md5 (
1162+ "FOO-someproduct-elsewhere" .encode ("utf-8" )
1163+ ).hexdigest (),
1164+ }
1165+ cache_dir = Path (self .output_dir ) / ".downloaded"
1166+ product_output_dir = Path (self .output_dir ) / "dummy_product"
11501167
1168+ # Call download() with asset specified using pattern -----------------------------------------------------------
11511169 path = plugin .download (self .product , output_dir = self .output_dir , asset = "else.*" )
11521170
1153- self .assertEqual (path , os .path .join (self .output_dir , "dummy_product" ))
1154- self .assertTrue (os .path .isdir (path ))
1171+ # 2 requests.get calls for 'elsewhere' asset:
1172+ # 1. fetch_asset_size / 2. _raw_stream_download_assets
1173+ self .assertEqual (2 , mock_requests_get .call_count )
11551174 self .assertTrue (
1156- os .path .isfile (
1157- os .path .join (self .output_dir , "dummy_product" , "somethingelse" )
1175+ all (
1176+ mock_call .args == ("http://elsewhere/anything" ,)
1177+ for mock_call in mock_requests_get .call_args_list
11581178 )
11591179 )
1160- self .assertEqual (2 , mock_requests_get .call_count )
1161- self .product .location = self .product .remote_location = "http://elsewhere"
1180+ # statements for the asset should be stored in the cache
1181+ self .assertFalse (
1182+ os .path .isfile (cache_dir / f"{ expected_hashes ['somewhere' ]} .json" )
1183+ )
1184+ self .assertTrue (
1185+ os .path .isfile (cache_dir / f"{ expected_hashes ['elsewhere' ]} .json" )
1186+ )
1187+ somewhere_statements = plugin .check_cache (
1188+ self .product .assets ["somewhere" ], output_dir = self .output_dir
1189+ )
1190+ self .assertIsNone (somewhere_statements )
1191+ elsewhere_statements = plugin .check_cache (
1192+ self .product .assets ["elsewhere" ], output_dir = self .output_dir
1193+ )
1194+ self .assertEqual (elsewhere_statements ["href" ], "http://elsewhere/anything" )
1195+ self .assertEqual (
1196+ elsewhere_statements ["file:local_path" ],
1197+ str (product_output_dir / "somethingelse" ),
1198+ )
1199+
1200+ # output file and dir created
1201+ self .assertEqual (path , str (product_output_dir ))
1202+ self .assertTrue (os .path .isdir (path ))
1203+ self .assertTrue (os .path .isfile (product_output_dir / "somethingelse" ))
1204+
1205+ # Another download() call without asset specified: should only download remaining asset ------------------------
1206+ self .product .location = self .product .remote_location = "http://foo"
11621207 plugin .download (self .product , output_dir = self .output_dir )
1163- self .assertEqual (6 , mock_requests_get .call_count )
1208+
1209+ # 2 more requests.get calls for 'somewhere' asset:
1210+ # 1. fetch_asset_size / 2. _raw_stream_download_assets
1211+ self .assertEqual (4 , mock_requests_get .call_count )
1212+ self .assertTrue (
1213+ all (
1214+ mock_call .args == ("http://somewhere/something" ,)
1215+ for mock_call in mock_requests_get .call_args_list [2 :]
1216+ )
1217+ )
1218+ # statements for the asset should be stored in the cache
1219+ self .assertTrue (
1220+ os .path .isfile (cache_dir / f"{ expected_hashes ['somewhere' ]} .json" )
1221+ )
1222+ self .assertTrue (
1223+ os .path .isfile (cache_dir / f"{ expected_hashes ['elsewhere' ]} .json" )
1224+ )
1225+ somewhere_statements = plugin .check_cache (
1226+ self .product .assets ["somewhere" ], output_dir = self .output_dir
1227+ )
1228+ self .assertEqual (somewhere_statements ["href" ], "http://somewhere/something" )
1229+ self .assertEqual (
1230+ somewhere_statements ["file:local_path" ],
1231+ str (product_output_dir / "somethingelse" ),
1232+ )
1233+ elsewhere_statements = plugin .check_cache (
1234+ self .product .assets ["elsewhere" ], output_dir = self .output_dir
1235+ )
1236+ self .assertEqual (elsewhere_statements ["href" ], "http://elsewhere/anything" )
1237+ self .assertEqual (
1238+ elsewhere_statements ["file:local_path" ],
1239+ str (product_output_dir / "somethingelse" ),
1240+ )
11641241
11651242 @mock .patch ("eodag.plugins.download.http.requests.head" , autospec = True )
11661243 @mock .patch ("eodag.plugins.download.http.requests.get" , autospec = True )
0 commit comments