|
| 1 | +""" |
| 2 | + CKAN Fair Data Point extension Test Suite |
| 3 | + Copyright (C) 2024, Stichting Health-RI |
| 4 | +
|
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU Affero General Public License as |
| 7 | + published by the Free Software Foundation, either version 3 of the |
| 8 | + License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU Affero General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Affero General Public License |
| 16 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +""" |
| 18 | + |
| 19 | +import pytest |
| 20 | +from datetime import datetime |
| 21 | +from dateutil.tz import tzutc, tzoffset |
| 22 | +from pathlib import Path |
| 23 | +from rdflib import Graph, URIRef |
| 24 | +from ckanext.fairdatapoint.profiles import validate_tags, convert_datetime_string |
| 25 | +from ckanext.fairdatapoint.harvesters.domain.fair_data_point_record_to_package_converter import ( |
| 26 | + FairDataPointRecordToPackageConverter) |
| 27 | + |
| 28 | +TEST_DATA_DIRECTORY = Path(Path(__file__).parent.resolve(), "test_data") |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("input_tags,expected_tags", [ |
| 32 | + ([{"name": "CNS/Brain"}], [{"name": "CNS Brain"}]), |
| 33 | + ([{"name": "COVID-19"}, {"name": "3`-DNA"}], [{"name": "COVID-19"}, {"name": "3 -DNA"}]), |
| 34 | + ([{"name": "something-1.1"}, {"name": "breast cancer"}], [{"name": "something-1.1"}, {"name": "breast cancer"}]), |
| 35 | + ([], []) |
| 36 | +]) |
| 37 | +def test_validate_tags(input_tags, expected_tags): |
| 38 | + actual_tags = validate_tags(input_tags) |
| 39 | + assert actual_tags == expected_tags |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.ckan_config("ckan.plugins", "scheming_datasets") |
| 43 | +@pytest.mark.usefixtures("with_plugins") |
| 44 | +def test_parse_dataset(): |
| 45 | + """Dataset with keywords which should be modified""" |
| 46 | + fdp_record_to_package = FairDataPointRecordToPackageConverter(profile="fairdatapoint_dcat_ap") |
| 47 | + data = Graph().parse(Path(TEST_DATA_DIRECTORY, "dataset_cbioportal.ttl")).serialize() |
| 48 | + actual = fdp_record_to_package.record_to_package( |
| 49 | + guid="catalog=https://health-ri.sandbox.semlab-leiden.nl/catalog/5c85cb9f-be4a-406c-ab0a-287fa787caa0;" |
| 50 | + "dataset=https://health-ri.sandbox.semlab-leiden.nl/dataset/d9956191-1aff-4181-ac8b-16b829135ed5", |
| 51 | + record=data) |
| 52 | + expected = { |
| 53 | + 'extras': [ |
| 54 | + {'key': 'uri', |
| 55 | + 'value': 'https://health-ri.sandbox.semlab-leiden.nl/dataset/d9956191-1aff-4181-ac8b-16b829135ed5' |
| 56 | + } |
| 57 | + ], |
| 58 | + 'resources': [{'name': 'Clinical data for [PUBLIC] Low-Grade Gliomas (UCSF, Science 2014)', |
| 59 | + 'description': 'Clinical data for [PUBLIC] Low-Grade Gliomas (UCSF, Science 2014)', |
| 60 | + 'access_url': 'https://cbioportal.health-ri.nl/study/clinicalData?id=lgg_ucsf_2014', |
| 61 | + 'license': 'http://rdflicense.appspot.com/rdflicense/cc-by-nc-nd3.0', |
| 62 | + 'url': 'https://cbioportal.health-ri.nl/study/clinicalData?id=lgg_ucsf_2014', |
| 63 | + 'uri': 'https://health-ri.sandbox.semlab-leiden.nl/distribution/' |
| 64 | + '931ed9c4-ad23-47ff-b121-2eb428e57423', |
| 65 | + 'distribution_ref': 'https://health-ri.sandbox.semlab-leiden.nl/distribution/' |
| 66 | + '931ed9c4-ad23-47ff-b121-2eb428e57423'}, |
| 67 | + {'name': 'Mutations', |
| 68 | + 'description': 'Mutation data from whole exome sequencing of 23 grade II glioma tumor/normal ' |
| 69 | + 'pairs. (MAF)', |
| 70 | + 'access_url': 'https://cbioportal.health-ri.nl/study/summary?id=lgg_ucsf_2014', |
| 71 | + 'license': 'http://rdflicense.appspot.com/rdflicense/cc-by-nc-nd3.0', |
| 72 | + 'url': 'https://cbioportal.health-ri.nl/study/summary?id=lgg_ucsf_2014', |
| 73 | + 'uri': 'https://health-ri.sandbox.semlab-leiden.nl/distribution/' |
| 74 | + 'ad00299f-6efb-42aa-823d-5ff2337f38f7', |
| 75 | + 'distribution_ref': 'https://health-ri.sandbox.semlab-leiden.nl/distribution/' |
| 76 | + 'ad00299f-6efb-42aa-823d-5ff2337f38f7'}], |
| 77 | + 'title': '[PUBLIC] Low-Grade Gliomas (UCSF, Science 2014)', |
| 78 | + 'notes': 'Whole exome sequencing of 23 grade II glioma tumor/normal pairs.', |
| 79 | + 'url': 'https://cbioportal.health-ri.nl/study/summary?id=lgg_ucsf_2014', |
| 80 | + 'tags': [{'name': 'CNS Brain'}, {'name': 'Diffuse Glioma'}, {'name': 'Glioma'}], 'license_id': '', |
| 81 | + 'issued': datetime(2019, 10, 30, 23, 0), |
| 82 | + 'modified': datetime(2019, 10, 30, 23, 0), |
| 83 | + 'identifier': 'lgg_ucsf_2014', 'language': ['http://id.loc.gov/vocabulary/iso639-1/en'], |
| 84 | + 'conforms_to': ['https://health-ri.sandbox.semlab-leiden.nl/profile/2f08228e-1789-40f8-84cd-28e3288c3604'], |
| 85 | + 'publisher_uri': 'https://www.health-ri.nl', |
| 86 | + 'access_rights': 'https://health-ri.sandbox.semlab-leiden.nl/dataset/' |
| 87 | + 'd9956191-1aff-4181-ac8b-16b829135ed5#accessRights', |
| 88 | + 'is_referenced_by': '["https://pubmed.ncbi.nlm.nih.gov/24336570"]'} |
| 89 | + assert actual == expected |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize("input_timestring,expected_output", [ |
| 93 | + ("2023-10-06T10:12:55.614000+00:00", |
| 94 | + datetime(2023, 10, 6, 10, 12, 55, 614000, tzinfo=tzutc())), |
| 95 | + ("2024-02-15 11:16:37+03:00", |
| 96 | + datetime(2024, 2, 15, 11, 16, 37, tzinfo=tzoffset(None, 10800))), |
| 97 | + ("November 9, 1999", datetime(1999, 11, 9, 0, 0, 0)), |
| 98 | + ("2006-09", datetime(2006, 9, 18))]) |
| 99 | +def test_convert_datetime_string(input_timestring, expected_output): |
| 100 | + actual = convert_datetime_string(input_timestring) |
| 101 | + assert actual == expected_output |
0 commit comments