Skip to content
This repository was archived by the owner on Nov 15, 2019. It is now read-only.

Commit 38c26f6

Browse files
author
Michael Baumann
committed
Add basic test for load-by-reference JSON document content
1 parent 87fd4e0 commit 38c26f6

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def read(fname):
3333
'dcplib >= 1.3.2, < 2',
3434
'google-cloud-storage >= 1.9.0, < 2',
3535
'hca == 4.1.4',
36+
'iso8601 == 0.1.12',
3637
'requests >= 2.18.4, < 3'],
3738
license='Apache License 2.0',
3839
include_package_data=True,

tests/abstract_loader_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
class AbstractLoaderTest(unittest.TestCase):
1919

20+
dss_client: DSSClient
21+
2022
@classmethod
2123
def setUpClass(cls):
2224
super().setUpClass()

tests/test_standard_loader.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import datetime
21
import io
32
import logging
4-
import os
53
import typing
6-
import unittest
74
import uuid
85
from pathlib import Path
96

107
import boto3
11-
import hca
8+
import iso8601
129

1310
from loader import base_loader
1411
from loader.base_loader import FileURLError
@@ -27,6 +24,8 @@
2724
class TestLoader(AbstractLoaderTest):
2825
"""unit tests for standard loader"""
2926

27+
s3 = boto3.resource('s3')
28+
3029
@classmethod
3130
def setUpClass(cls):
3231
super().setUpClass()
@@ -35,7 +34,6 @@ def setUpClass(cls):
3534
cls.metadata_uploader = base_loader.MetadataFileUploader(cls.dss_uploader)
3635

3736
# create test bucket and upload test files
38-
cls.s3 = boto3.resource('s3')
3937
cls.bucket_name = f'loader-test-bucket-{uuid.uuid4()}'
4038
cls.s3.create_bucket(Bucket=cls.bucket_name, CreateBucketConfiguration={
4139
'LocationConstraint': 'us-west-2'})
@@ -273,14 +271,41 @@ def test_many_bundles_dict_concurrently(self):
273271
"""Same as above but with even more bundles"""
274272
self._test_loading_bundles_dict([self._make_minimal_bundle(parsed=False) for _ in range(20)], concurrently=True)
275273

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+
276289
def _test_bundles_in_dss(self, bundles: typing.List[ParsedBundle]):
277290
"""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+
278296
for bundle in bundles:
279297
bundle_json = self.dss_client.get_bundle(uuid=bundle.bundle_uuid, replica='aws')['bundle'] # type: ignore
280298
self.assertEqual(bundle_json['uuid'], bundle.bundle_uuid)
281299
loaded_file_uuids = {file_json['uuid'] for file_json in bundle_json['files']}
282300
for data_object in bundle.data_files:
283301
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)
284309

285310
@ignore_resource_warnings
286311
def test_minimal_bundle_in_dss(self):

0 commit comments

Comments
 (0)