Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "GET ../files" #65

Merged
merged 5 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 48 additions & 96 deletions tests/ga4gh/trs/endpoints/test_register_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@
import mongomock
from pymongo.errors import DuplicateKeyError
import pytest
import requests

from tests.mock_data import (
ENDPOINT_CONFIG,
ENDPOINT_CONFIG_CHARSET_LITERAL,
ENDPOINT_CONFIG_ONE_ID,
ENDPOINT_CONFIG_TOOL_CLASS_VALIDATION,
MOCK_TEST_FILE,
MOCK_OTHER_FILE,
MOCK_CONTAINER_FILE,
MOCK_DESCRIPTOR_FILE,
MOCK_FILES_TOOL_FILE_MISSING,
MOCK_FILE_TYPE_MISSING,
MOCK_FILES_CHECKSUM_MISSING,
MOCK_FILES_CONTENT_URL_MISSING,
MOCK_ID,
Expand All @@ -43,6 +40,11 @@
)


def _raise(exception) -> None:
"""General purpose exception raiser."""
raise exception


class TestRegisterTool:
"""Tests for `RegisterTool` class."""

Expand Down Expand Up @@ -331,42 +333,26 @@ def test_process_metadata(self):
tool.process_metadata()
assert isinstance(tool.id_charset, str)

def test_process_metadata_empty_file_object(self):
"""Test for processing metadata with one empty file object."""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG),
endpoints=ENDPOINT_CONFIG_CHARSET_LITERAL,
)

data = deepcopy(MOCK_VERSION_NO_ID)
data['files'][0] = {}
with app.app_context():
tool = RegisterToolVersion(data=data, id=MOCK_ID)
tool.process_metadata()
assert isinstance(tool.id_charset, str)

def test_process_file_type_register_invalid_descriptor_type(self):
"""Test for processing metadata with one invalid `descriptors` type."""
def test_process_files_invalid_descriptor_type(self):
"""Test for processing files with an invalid descriptor type."""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG),
endpoints=ENDPOINT_CONFIG_CHARSET_LITERAL,
)

data = deepcopy(MOCK_VERSION_NO_ID)
mock_files = deepcopy(MOCK_DESCRIPTOR_FILE)
mock_files["type"] = "invalid"
mock_file = deepcopy(MOCK_DESCRIPTOR_FILE)
mock_file["type"] = "invalid"
with app.app_context():
with pytest.raises(BadRequest):
tool = RegisterToolVersion(data=data, id=MOCK_ID)
tool.process_file_type_register(
file_data=mock_files
)
tool.data['files'] = [mock_file]
tool.process_files()

def test_process_file_type_register_multiple_primarydescriptor(self):
"""Test for processing metadata with more than one `descriptors` having
`tool_file.file_type` as `PRIMARY_DESCRIPTOR`.
def test_process_files_multiple_primarydescriptor(self):
"""Test for processing files with more than one descriptor being
annotated as primary descriptor.
"""
app = Flask(__name__)
app.config['FOCA'] = Config(
Expand All @@ -378,80 +364,73 @@ def test_process_file_type_register_multiple_primarydescriptor(self):
with app.app_context():
with pytest.raises(BadRequest):
tool = RegisterToolVersion(data=data, id=MOCK_ID)
tool.data['files'] = data['files']
tool.primary_descriptor_flags['CWL'] = True
tool.process_file_type_register(
file_data=MOCK_DESCRIPTOR_FILE
)
tool.process_files()

def test_process_file_type_register_invalid_container_type(self):
"""Test for processing metadata with one invalid `containers` type."""
def test_process_files_invalid_container_type(self):
"""Test for processing files with an invalid container type."""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG),
endpoints=ENDPOINT_CONFIG_CHARSET_LITERAL,
)

data = deepcopy(MOCK_VERSION_NO_ID)
mock_files = deepcopy(MOCK_CONTAINER_FILE)
mock_files["type"] = "invalid"
mock_file = deepcopy(MOCK_CONTAINER_FILE)
mock_file["type"] = "invalid"
with app.app_context():
with pytest.raises(BadRequest):
tool = RegisterToolVersion(data=data, id=MOCK_ID)
tool.process_file_type_register(
file_data=mock_files
)
tool.data['files'] = [mock_file]
tool.process_files()

def test_process_file_type_register_invalid_test_type(self):
"""Test for processing metadata with one invalid `tests` type."""
def test_process_files_no_content(self, monkeypatch):
"""Test for processing files with no content provided."""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG),
endpoints=ENDPOINT_CONFIG_CHARSET_LITERAL,
)

data = deepcopy(MOCK_VERSION_NO_ID)
mock_files = deepcopy(MOCK_TEST_FILE)
mock_files["type"] = "invalid"
with app.app_context():
with pytest.raises(BadRequest):
tool = RegisterToolVersion(data=data, id=MOCK_ID)
tool.process_file_type_register(
file_data=mock_files
)
class MockResponse:
text = MOCK_ID

def test_process_file_type_register_invalid_other_type(self):
"""Test for processing metadata with one invalid `others` type."""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG),
endpoints=ENDPOINT_CONFIG_CHARSET_LITERAL,
)
def __init__(*args, **kwargs):
pass

monkeypatch.setattr('requests.get', MockResponse)
data = deepcopy(MOCK_VERSION_NO_ID)
mock_files = deepcopy(MOCK_OTHER_FILE)
mock_files["type"] = "invalid"
mock_file = deepcopy(MOCK_CONTAINER_FILE)
del mock_file['file_wrapper']['content']
mock_file['file_wrapper']['url'] = MOCK_ID
with app.app_context():
with pytest.raises(BadRequest):
tool = RegisterToolVersion(data=data, id=MOCK_ID)
tool.process_file_type_register(
file_data=mock_files
)
tool = RegisterToolVersion(data=data, id=MOCK_ID)
tool.data['files'] = [mock_file]
tool.process_files()

def test_process_files_tool_file_not_present(self):
"""Test for processing metadata with invalid file object with
`tool_file` absent.
def test_process_files_no_content_invalid_url(self, monkeypatch):
"""Test for processing files with no content provided and an invalid
URL.
"""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG),
endpoints=ENDPOINT_CONFIG_CHARSET_LITERAL,
)

monkeypatch.setattr(
'requests.get',
lambda *args, **kwargs: _raise(requests.exceptions.ConnectionError)
)
data = deepcopy(MOCK_VERSION_NO_ID)
data['files'] = MOCK_FILES_TOOL_FILE_MISSING
mock_file = deepcopy(MOCK_CONTAINER_FILE)
del mock_file['file_wrapper']['content']
mock_file['file_wrapper']['url'] = MOCK_ID
with app.app_context():
with pytest.raises(BadRequest):
tool = RegisterToolVersion(data=data, id=MOCK_ID)
tool.data['files'] = [mock_file]
tool.process_files()

def test_register_metadata(self):
Expand Down Expand Up @@ -524,7 +503,7 @@ def test_register_metadata_with_id_replace(self):
version.register_metadata()
assert isinstance(version.data, dict)

def test_register_metadata_duplicate_keys_repeated(self):
def test_register_metadata_duplicate_keys(self):
"""Test for creating a version; running out of unique identifiers."""
app = Flask(__name__)
app.config['FOCA'] = Config(
Expand Down Expand Up @@ -572,30 +551,3 @@ def test_register_metadata_tool_na(self):
with pytest.raises(NotFound):
version = RegisterToolVersion(data=data, id=MOCK_ID)
version.register_metadata()

def test_register_metadata_file_type_not_given(self):
"""Test for creating a version when `file_type` not provided in
`tool_file`.
"""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG),
endpoints=ENDPOINT_CONFIG_CHARSET_LITERAL,
)
mock_resp = deepcopy(MOCK_TOOL_VERSION_ID)
mock_resp["id"] = MOCK_ID
app.config['FOCA'].db.dbs['trsStore'].collections['tools'] \
.client = MagicMock()
app.config['FOCA'].db.dbs['trsStore'].collections['files'] \
.client = MagicMock()
app.config['FOCA'].db.dbs['trsStore'].collections['tools'] \
.client.insert_one(mock_resp)
app.config['FOCA'].db.dbs['trsStore'].collections['files'] \
.client.insert_one(mock_resp)

data = deepcopy(MOCK_VERSION_NO_ID)
data['files'] = MOCK_FILE_TYPE_MISSING
with app.app_context():
version = RegisterToolVersion(data=data, id=MOCK_ID)
version.register_metadata()
assert isinstance(version.data, dict)
67 changes: 67 additions & 0 deletions tests/ga4gh/trs/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SERVICE_INFO_CONFIG,
MOCK_CONTAINER_FILE,
MOCK_DESCRIPTOR_FILE,
MOCK_OTHER_FILE,
)
from trs_filer.ga4gh.trs.server import (
deleteTool,
Expand All @@ -36,6 +37,7 @@
toolsGet,
toolsIdGet,
toolsIdVersionsGet,
toolsIdVersionsVersionIdTypeFilesGet,
toolsIdVersionsVersionIdContainerfileGet,
toolsIdVersionsVersionIdGet,
toolsIdVersionsVersionIdTypeDescriptorGet,
Expand Down Expand Up @@ -259,6 +261,71 @@ def test_toolsGet_filters():
assert res == ([data], '200', HEADERS_PAGINATION)


def test_toolsIdVersionsVersionIdTypeFilesGet_Descriptor():
"""Test for getting descriptor files associated with a specific tool version
identified by the given tool and version identifiers.
"""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG)
)
mock_resp = deepcopy(MOCK_FILES_DB_ENTRY)
app.config['FOCA'].db.dbs['trsStore'] \
.collections['files'].client = mongomock.MongoClient().db.collection
app.config['FOCA'].db.dbs['trsStore'].collections['files'] \
.client.insert_one(mock_resp)

with app.app_context():
res = toolsIdVersionsVersionIdTypeFilesGet.__wrapped__(
id=MOCK_ID,
version_id=MOCK_ID,
type=MOCK_DESCRIPTOR_FILE['type']
)
assert res == [MOCK_DESCRIPTOR_FILE["tool_file"]]


def test_toolsIdVersionsVersionIdTypeFilesGet_no_tool_file_NotFound():
"""Test for getting tool files associated with a specific tool version
identified by the given tool and version identifiers when no
specification file is available for the tool version.
"""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG)
)
mock_resp = deepcopy(MOCK_FILES_DB_ENTRY)
for version in mock_resp['versions']:
version['files'] = {}
app.config['FOCA'].db.dbs['trsStore'] \
.collections['files'].client = mongomock.MongoClient().db.collection
app.config['FOCA'].db.dbs['trsStore'].collections['files'] \
.client.insert_one(mock_resp)

with app.app_context():
with pytest.raises(NotFound):
toolsIdVersionsVersionIdTypeFilesGet.__wrapped__(
id=MOCK_ID,
version_id=MOCK_ID + MOCK_ID,
type=MOCK_OTHER_FILE['type']
)


def test_toolsIdVersionsVersionIdTypeFilesGet_wrong_type_BadRequest():
"""Test for getting files when an invalid type is specified."""
app = Flask(__name__)
app.config['FOCA'] = Config(
db=MongoConfig(**MONGO_CONFIG)
)

with app.app_context():
with pytest.raises(BadRequest):
toolsIdVersionsVersionIdTypeFilesGet.__wrapped__(
id=MOCK_ID,
version_id=MOCK_ID + MOCK_ID,
type='foo'
)


# GET /tools/{id}/versions/{version_id}/containerfile
def test_toolsIdVersionsVersionIdContainerfileGet():
"""Test for getting container files associated with a specific tool version
Expand Down
8 changes: 4 additions & 4 deletions tests/mock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"file_type": "TEST_FILE",
"path": "path",
},
"type": "JSON"
"type": "CWL",
}
MOCK_DESCRIPTOR_FILE = {
"file_wrapper": {
Expand All @@ -132,7 +132,7 @@
"file_type": "PRIMARY_DESCRIPTOR",
"path": "path",
},
"type": "CWL"
"type": "CWL",
}
MOCK_CONTAINER_FILE = {
"file_wrapper": {
Expand All @@ -149,7 +149,7 @@
"file_type": "CONTAINERFILE",
"path": "path",
},
"type": "Docker"
"type": "Docker",
}
MOCK_OTHER_FILE = {
"file_wrapper": {
Expand All @@ -166,7 +166,7 @@
"file_type": "OTHER",
"path": "path",
},
"type": "OTHER"
"type": "CWL"
}
MOCK_FILES = [
MOCK_TEST_FILE,
Expand Down
Loading