-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This contains all the stuff required for the preps view including link to specimen and testing. This also includes a rewrite of the key importer tests as they took ages to run and weren't precise enough to actually be testing what we needed to test.
- Loading branch information
Showing
8 changed files
with
1,060 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from dataimporter.emu.views.utils import ( | ||
NO_PUBLISH, | ||
DISALLOWED_STATUSES, | ||
DEPARTMENT_COLLECTION_CODES, | ||
INVALID_STATUS, | ||
INVALID_DEPARTMENT, | ||
INVALID_TYPE, | ||
is_web_published, | ||
is_valid_guid, | ||
INVALID_GUID, | ||
) | ||
from dataimporter.emu.views.utils import emu_date | ||
from dataimporter.model import SourceRecord | ||
from dataimporter.view import View, FilterResult, SUCCESS_RESULT | ||
|
||
INVALID_SUBDEPARTMENT = FilterResult(False, "Invalid subdepartment") | ||
|
||
|
||
class PreparationView(View): | ||
""" | ||
View for preparation records. | ||
This view populates the preparation resource on the Data Portal. | ||
""" | ||
|
||
def is_member(self, record: SourceRecord) -> FilterResult: | ||
""" | ||
Filters the given record, determining whether it should be included in the | ||
preparation resource or not. | ||
:param record: the record to filter | ||
:return: a FilterResult object | ||
""" | ||
if record.get_first_value("ColRecordType", default="").lower() != "preparation": | ||
return INVALID_TYPE | ||
|
||
if not is_web_published(record): | ||
return NO_PUBLISH | ||
|
||
if not is_valid_guid(record): | ||
return INVALID_GUID | ||
|
||
if record.get_first_value("SecRecordStatus") in DISALLOWED_STATUSES: | ||
return INVALID_STATUS | ||
|
||
if record.get_first_value("ColDepartment") not in DEPARTMENT_COLLECTION_CODES: | ||
return INVALID_DEPARTMENT | ||
|
||
if record.get_first_value("ColSubDepartment") != "Molecular Collections": | ||
return INVALID_SUBDEPARTMENT | ||
|
||
return SUCCESS_RESULT | ||
|
||
def make_data(self, record: SourceRecord) -> dict: | ||
""" | ||
Converts the record's raw data to a dict which will be the data presented on the | ||
Data Portal. | ||
:param record: the record to project | ||
:return: a dict containing the data for this record that should be displayed on | ||
the Data Portal | ||
""" | ||
# cache these for perf | ||
get_all = record.get_all_values | ||
get_first = record.get_first_value | ||
|
||
return { | ||
"_id": record.id, | ||
"created": emu_date( | ||
get_first("AdmDateInserted"), get_first("AdmTimeInserted") | ||
), | ||
"modified": emu_date( | ||
get_first("AdmDateModified"), get_first("AdmTimeModified") | ||
), | ||
"project": get_all("NhmSecProjectName"), | ||
"preparationNumber": get_first("EntPreNumber"), | ||
"preparationType": get_first("EntPrePreparationKind"), | ||
"mediumType": get_first("EntPreStorageMedium"), | ||
"preparationProcess": get_first("EntPrePreparationMethod"), | ||
"preparationContents": get_first("EntPreContents"), | ||
"preparationDate": get_first("EntPreDate"), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from contextlib import closing | ||
from pathlib import Path | ||
from typing import List, Tuple | ||
|
||
import pytest | ||
|
||
from dataimporter.dbs import DataDB | ||
from dataimporter.emu.views.utils import ( | ||
NO_PUBLISH, | ||
INVALID_TYPE, | ||
INVALID_GUID, | ||
INVALID_STATUS, | ||
INVALID_DEPARTMENT, | ||
) | ||
from dataimporter.model import SourceRecord | ||
from dataimporter.view import FilterResult, SUCCESS_RESULT | ||
from dataimporter.emu.views.preparation import PreparationView, INVALID_SUBDEPARTMENT | ||
from tests.helpers.samples.preparation import ( | ||
SAMPLE_PREPARATION_DATA, | ||
SAMPLE_PREPARATION_ID, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def prep_view(tmp_path: Path) -> PreparationView: | ||
with closing( | ||
PreparationView(tmp_path / "prep_view", DataDB(tmp_path / "prep_data")) | ||
) as view: | ||
yield view | ||
|
||
|
||
is_member_scenarios: List[Tuple[dict, FilterResult]] = [ | ||
({"ColRecordType": "Specimen"}, INVALID_TYPE), | ||
({"AdmPublishWebNoPasswordFlag": "n"}, NO_PUBLISH), | ||
({"AdmGUIDPreferredValue": "not a valid guid!"}, INVALID_GUID), | ||
({"SecRecordStatus": "INVALID"}, INVALID_STATUS), | ||
({"ColDepartment": "DDI"}, INVALID_DEPARTMENT), | ||
({"ColSubDepartment": "Informatics"}, INVALID_SUBDEPARTMENT), | ||
({}, SUCCESS_RESULT), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("overrides, result", is_member_scenarios) | ||
def test_is_member(overrides: dict, result: FilterResult, prep_view: PreparationView): | ||
data = {**SAMPLE_PREPARATION_DATA, **overrides} | ||
record = SourceRecord(SAMPLE_PREPARATION_ID, data, "test") | ||
assert prep_view.is_member(record) == result | ||
|
||
|
||
def test_transform_deleted(prep_view: PreparationView): | ||
record = SourceRecord(SAMPLE_PREPARATION_ID, {}, "test") | ||
assert record.is_deleted | ||
|
||
data = prep_view.transform(record) | ||
assert data == {} | ||
|
||
|
||
def test_make_data(prep_view: PreparationView): | ||
record = SourceRecord(SAMPLE_PREPARATION_ID, SAMPLE_PREPARATION_DATA, "test") | ||
|
||
data = prep_view.make_data(record) | ||
assert data == { | ||
"_id": record.id, | ||
"created": "2022-09-12T17:07:51+00:00", | ||
"modified": "2022-09-12T17:21:14+00:00", | ||
"project": "Darwin Tree of Life", | ||
"preparationNumber": "C9K02TWP_B2", | ||
"preparationType": "DNA Extract", | ||
"mediumType": None, | ||
"preparationProcess": None, | ||
"preparationContents": "**OTHER_SOMATIC_ANIMAL_TISSUE**", | ||
"preparationDate": "2022-05-09", | ||
} |
Oops, something went wrong.