1
- import datetime
2
1
import io
3
2
import logging
4
- import os
5
3
import typing
6
- import unittest
7
4
import uuid
8
5
from pathlib import Path
9
6
10
7
import boto3
11
- import hca
8
+ import iso8601
12
9
13
10
from loader import base_loader
14
11
from loader .base_loader import FileURLError
27
24
class TestLoader (AbstractLoaderTest ):
28
25
"""unit tests for standard loader"""
29
26
27
+ s3 = boto3 .resource ('s3' )
28
+
30
29
@classmethod
31
30
def setUpClass (cls ):
32
31
super ().setUpClass ()
@@ -35,7 +34,6 @@ def setUpClass(cls):
35
34
cls .metadata_uploader = base_loader .MetadataFileUploader (cls .dss_uploader )
36
35
37
36
# create test bucket and upload test files
38
- cls .s3 = boto3 .resource ('s3' )
39
37
cls .bucket_name = f'loader-test-bucket-{ uuid .uuid4 ()} '
40
38
cls .s3 .create_bucket (Bucket = cls .bucket_name , CreateBucketConfiguration = {
41
39
'LocationConstraint' : 'us-west-2' })
@@ -273,14 +271,41 @@ def test_many_bundles_dict_concurrently(self):
273
271
"""Same as above but with even more bundles"""
274
272
self ._test_loading_bundles_dict ([self ._make_minimal_bundle (parsed = False ) for _ in range (20 )], concurrently = True )
275
273
274
+ def _get_cloud_object_size (self , cloud_urls : typing .List [str ]) -> int :
275
+ s3_uri = list (filter (lambda url : url .startswith ("s3://" ), cloud_urls ))[0 ]
276
+ bucket , key = s3_uri [5 :].split ("/" , 1 )
277
+ obj = self .s3 .Object (bucket_name = bucket , key = key ) # noqa
278
+ return obj .content_length
279
+
280
+ def _verify_file_reference (self , data_object : ParsedDataFile , file_json : dict ) -> None :
281
+ file_ref_json = self .dss_client .get_file (uuid = file_json ['uuid' ], version = file_json ['version' ], replica = 'aws' )
282
+ self .assertTrue (len (data_object .cloud_urls ) >= 1 )
283
+ for url in data_object .cloud_urls :
284
+ self .assertIn (url , file_ref_json ['url' ])
285
+ self .assertEqual (file_ref_json ['size' ], self ._get_cloud_object_size (data_object .cloud_urls ))
286
+ if data_object .file_guid :
287
+ self .assertIn (data_object .file_guid , file_ref_json ['aliases' ])
288
+
276
289
def _test_bundles_in_dss (self , bundles : typing .List [ParsedBundle ]):
277
290
"""Searches the DSS for the bundles and checks that all the files are there"""
291
+
292
+ def versions_equal (version1 : str , version2 : str ) -> bool :
293
+ """ Check if two ISO 8601 compliant timestamps are equal regardless of specific string format. """
294
+ return iso8601 .parse_date (version1 ) == iso8601 .parse_date (version2 )
295
+
278
296
for bundle in bundles :
279
297
bundle_json = self .dss_client .get_bundle (uuid = bundle .bundle_uuid , replica = 'aws' )['bundle' ] # type: ignore
280
298
self .assertEqual (bundle_json ['uuid' ], bundle .bundle_uuid )
281
299
loaded_file_uuids = {file_json ['uuid' ] for file_json in bundle_json ['files' ]}
282
300
for data_object in bundle .data_files :
283
301
self .assertTrue (data_object .file_uuid in loaded_file_uuids )
302
+ for data_object in bundle .data_files :
303
+ file_json = list (filter (lambda file_json :
304
+ (file_json ['uuid' ] == data_object .file_uuid and
305
+ versions_equal (file_json ['version' ], data_object .file_version )),
306
+ bundle_json ['files' ]))[0 ]
307
+ assert "dss-type=fileref" in file_json ['content-type' ]
308
+ self ._verify_file_reference (data_object , file_json )
284
309
285
310
@ignore_resource_warnings
286
311
def test_minimal_bundle_in_dss (self ):
0 commit comments