Skip to content

Commit

Permalink
test: add tests for PaNOSCMappings #260
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 21, 2021
1 parent 5b71f09 commit 985d433
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 15 deletions.
131 changes: 116 additions & 15 deletions test/search_api/conftest.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
from icat.client import Client
from icat.query import Query
import json
from unittest.mock import mock_open, patch

import pytest

from datagateway_api.src.common.config import Config
from datagateway_api.src.search_api.panosc_mappings import PaNOSCMappings
from datagateway_api.src.search_api.query import SearchAPIQuery


@pytest.fixture(scope="package")
def icat_client():
client = Client(
Config.config.search_api.icat_url,
checkCert=Config.config.search_api.icat_check_cert,
)
client.login(
Config.config.test_mechanism, Config.config.test_user_credentials.dict(),
)
return client


@pytest.fixture()
def search_api_query_dataset():
return SearchAPIQuery("Dataset")
Expand All @@ -31,3 +20,115 @@ def search_api_query_document():
@pytest.fixture()
def search_api_query_instrument():
return SearchAPIQuery("Instrument")


@pytest.fixture()
def test_search_api_mappings_data():
return {
"Affiliation": {
"base_icat_entity": "Affiliation",
"id": "id",
"name": "name",
"address": "fullReference",
"city": "",
"country": "",
"members": {"Member": "user.user.investigationUsers"},
},
"Dataset": {
"base_icat_entity": "Dataset",
"pid": "doi",
"title": "name",
"isPublic": "createTime",
"creationDate": "createTime",
"size": "",
"documents": {"Document": "investigation"},
"techniques": {"Technique": "datasetTechniques.technique"},
"instrument": {"Instrument": "datasetInstruments.instrument"},
"files": {"File": "datafiles"},
"parameters": {"Parameter": "parameters"},
"samples": {"Sample": "sample"},
},
"Document": {
"base_icat_entity": "Investigation",
"pid": "doi",
"isPublic": "releaseDate",
"type": "type.name",
"title": "name",
"summary": "summary",
"doi": "doi",
"startDate": "startDate",
"endDate": "endDate",
"releaseDate": "releaseDate",
"license": "",
"keywords": "keywords.name",
"datasets": {"Dataset": "datasets"},
"members": {"Member": "investigationUsers"},
"parameters": {"Parameter": "parameters"},
},
"File": {
"base_icat_entity": "Datafile",
"id": "id",
"name": "name",
"path": "location",
"size": "fileSize",
"dataset": {"Dataset": "dataset"},
},
"Instrument": {
"base_icat_entity": "Instrument",
"pid": "id",
"name": "name",
"facility": "facility.name",
"datasets": {"Dataset": "datasetInstruments.dataset"},
},
"Member": {
"base_icat_entity": "InvestigationUser",
"id": "id",
"role": "role",
"document": {"Document": "investigation"},
"person": {"Person": "user"},
"affiliation": {"Affiliation": "user.dataPublicationUsers.affiliations"},
},
"Parameter": {
"base_icat_entity": "InvestigationParameter",
"id": "id",
"name": "name",
"value": ["numericValue", "stringValue", "dateTimeValue"],
"unit": "type.units",
"dataset": {
"Dataset": "investigation.investigationInstruments.instrument."
"datasetInstruments.dataset",
},
"document": {"Document": "investigation"},
},
"Person": {
"base_icat_entity": "User",
"id": "id",
"fullName": "fullName",
"orcid": "orcidId",
"researcherId": "",
"firstName": "givenName",
"lastName": "familyName",
"members": {"Member": "investigationUsers"},
},
"Sample": {
"base_icat_entity": "Sample",
"name": "name",
"pid": "pid",
"description": "parameters.type.description",
"datasets": {"Dataset": "datasets"},
},
"Technique": {
"base_icat_entity": "Technique",
"pid": "pid",
"name": "name",
"datasets": {"Dataset": "datasetTechniques.dataset"},
},
}


@pytest.fixture()
def test_panosc_mappings(test_search_api_mappings_data):
with patch(
"builtins.open", mock_open(read_data=json.dumps(test_search_api_mappings_data)),
):
return PaNOSCMappings("test/path")
38 changes: 38 additions & 0 deletions test/search_api/test_panosc_mappings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest

from datagateway_api.src.common.exceptions import SearchAPIError
from datagateway_api.src.search_api.panosc_mappings import PaNOSCMappings


class TestPaNOSCMappings:
def test_valid_load_mappings(self, test_panosc_mappings):
test_mappings = PaNOSCMappings()
assert test_mappings.mappings == test_panosc_mappings.mappings

@pytest.mark.parametrize(
"test_panosc_entity_name, test_panosc_related_field_name, expected_entity_name",
[
pytest.param("Dataset", "files", "File", id="Dataset.files"),
pytest.param(
"Document", "parameters", "Parameter", id="Document.parameters",
),
pytest.param("Person", "members", "Member", id="Person.members"),
],
)
def test_valid_get_panosc_related_entity_name(
self,
test_panosc_mappings,
test_panosc_entity_name,
test_panosc_related_field_name,
expected_entity_name,
):
test_related_name = test_panosc_mappings.get_panosc_related_entity_name(
test_panosc_entity_name, test_panosc_related_field_name,
)
assert test_related_name == expected_entity_name

def test_invalid_get_panosc_related_entity_name(self, test_panosc_mappings):
with pytest.raises(SearchAPIError):
test_panosc_mappings.get_panosc_related_entity_name(
"UnknownField", "unknownField",
)

0 comments on commit 985d433

Please sign in to comment.