def test_new_file(): """ Test that new file are created correctly """ os.environ['DATASERVICE_API'] = 'http://api.com/' mock = patch('service.requests') req = mock.start() mock_resp = MagicMock() mock_resp.status_code = 201 mock_resp.json.return_value = {'results': {'kf_id': 'GF_00000000'}} req.post.return_value = mock_resp importer = service.FileImporter('http://api.com/', 'abc123') res = importer.new_file(BUCKET, OBJECT, 'd41d8cd98f00b204e9800998ecf8427e', 1024) expected = { 'file_name': '60d33dec-98db-446c-ac64-f4d027588f26.cram', 'file_format': 'cram', 'data_type': 'Aligned Reads', 'controlled_access': True, 'availability': 'Immediate Download', 'is_harmonized': True, 'reference_genome': 'GRCh38', 'hashes': { 'etag': 'd41d8cd98f00b204e9800998ecf8427e' }, 'size': 1024, 'urls': ['s3://{}/{}'.format(BUCKET, OBJECT)], 'acl': [] } req.post.assert_called_with('http://api.com/genomic-files', json=expected) assert req.post.call_count == 1
def test_no_biospecimen(event, obj): """ Test that nothing is done if the biospecimen does not exist """ obj() s3 = boto3.client('s3') mock = patch('service.requests') req = mock.start() mock_resp = MagicMock() mock_resp.status_code = 404 req.get.return_value = mock_resp importer = service.FileImporter('http://api.com/', 'abc123') with pytest.raises(service.ImportException): importer.import_harmonized(event['Records'][0]) req.get.assert_any_call('http://api.com/biospecimens/BS_QV3Z0DZM') mock.stop()
def test_new_file_error(): """ Test that new file with predefined kf_id """ os.environ['DATASERVICE_API'] = 'http://api.com/' mock = patch('service.requests') req = mock.start() mock_resp = MagicMock() mock_resp.status_code = 400 mock_resp.json.return_value = {'results': {'kf_id': 'GF_00000002'}} req.post.return_value = mock_resp importer = service.FileImporter('http://api.com/', 'abc123') with pytest.raises(service.DataServiceException): res = importer.new_file(BUCKET, OBJECT, 'd41d8cd98f00b204e9800998ecf8427e', 1024) assert req.post.call_count == 1
def test_get_gf_id_tag(): """ Test that gf_id is returned from tags correctly """ mock = patch('service.requests') req = mock.start() mock_resp = MagicMock() mock_resp.status_code = 404 mock_resp.json.return_value = {'_status': {'code': 404}} req.post.return_value = mock_resp importer = service.FileImporter('http://api.com/', 'abc123') tags = {'gf_id': 'GF_00000003'} res = importer.get_gf_id_tag(tags) assert res == 'GF_00000003' assert req.get.call_count == 1 mock.stop()
def test_get_gf_id_tag_exists(): """ Test getting gf_id tag if it already exists """ mock = patch('service.requests') req = mock.start() mock_resp = MagicMock() mock_resp.status_code = 200 mock_resp.json.return_value = {'results': {'kf_id': 'GF_00000003'}} req.get.return_value = mock_resp importer = service.FileImporter('http://api.com/', 'abc123') tags = {'gf_id': 'GF_00000003'} with pytest.raises(service.ImportException): res = importer.get_gf_id_tag(tags) assert req.get.call_count == 1 mock.stop()
def test_req_tags(event, obj): """ Test that error is raised for missing tags """ obj() s3 = boto3.client('s3') # Add a gf_id tags = TAGS.copy() tags['TagSet'] = tags['TagSet'][:4] response = s3.put_object_tagging(Bucket=BUCKET, Key=OBJECT, Tagging=tags) mock = patch('service.requests') req = mock.start() mock_resp = MagicMock() mock_resp.status_code = 404 req.get.return_value = mock_resp importer = service.FileImporter('http://api.com/', 'abc123') with pytest.raises(service.ImportException): importer.import_harmonized(event['Records'][0]) mock.stop()
def test_file_formats(filename, file_format, data_type): """ Test that file formats and data types are discovered correctly """ mock = patch('service.requests') req = mock.start() mock_resp = MagicMock() mock_resp.status_code = 201 mock_resp.json.return_value = {'results': {'kf_id': 'GF_00000001'}} req.post.return_value = mock_resp importer = service.FileImporter('http://api.com/', 'abc123') res = importer.new_file(SOURCE_BUCKET, filename, 'd41d8cd98f00b204e9800998ecf8427e', 1024, gf_id='GF_00000001', bs_id='BS_00000000', study_id='SD_00000000') expected = { 'kf_id': 'GF_00000001', 'file_name': filename, 'file_format': file_format, 'acl': ['SD_00000000'], 'data_type': data_type, 'controlled_access': True, 'availability': 'Immediate Download', 'is_harmonized': False, 'biospecimen_id': 'BS_00000000', 'hashes': { 'etag': 'd41d8cd98f00b204e9800998ecf8427e' }, 'size': 1024, 'urls': ['s3://{}/{}'.format(SOURCE_BUCKET, SOURCE_OBJECT)] } assert req.post.call_count == 1 called_with = req.post.call_args_list[0][1]['json'] assert called_with['file_format'] == file_format assert called_with['data_type'] == data_type
def test_new_file_bs_id(): """ Test that new source file registers with a biospecimen """ mock = patch('service.requests') req = mock.start() mock_resp = MagicMock() mock_resp.status_code = 201 mock_resp.json.return_value = {'results': {'kf_id': 'GF_00000001'}} req.post.return_value = mock_resp importer = service.FileImporter('http://api.com/', 'abc123') res = importer.new_file(SOURCE_BUCKET, SOURCE_OBJECT, 'd41d8cd98f00b204e9800998ecf8427e', 1024, gf_id='GF_00000001', bs_id='BS_00000000', study_id='SD_00000000') expected = { 'kf_id': 'GF_00000001', 'file_name': 'bd042b24ae844a57ace28cf70cb3c852.bam.bai', 'file_format': 'bai', 'acl': ['SD_00000000'], 'data_type': 'Aligned Reads Index', 'controlled_access': True, 'availability': 'Immediate Download', 'is_harmonized': False, 'biospecimen_id': 'BS_00000000', 'hashes': { 'etag': 'd41d8cd98f00b204e9800998ecf8427e' }, 'size': 1024, 'urls': ['s3://{}/{}'.format(SOURCE_BUCKET, SOURCE_OBJECT)] } req.post.assert_called_with('http://api.com/genomic-files', json=expected) assert req.post.call_count == 1
def test_get_gf_id_tag_no_tag(): """ Test getting gf_id tag if its not in the tags """ importer = service.FileImporter('http://api.com/', 'abc123') tags = {'fake': 'tag'} res = importer.get_gf_id_tag(tags) assert res == None