Skip to content

Commit

Permalink
test(fairdatapoint): fix unit tests for FairDataPointCivityHarvester …
Browse files Browse the repository at this point in the history
…setup methods
  • Loading branch information
Hans-Chrstian committed Aug 12, 2024
1 parent 0f1fc2c commit 61ef7c0
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,6 @@


class FairDataPointCivityHarvester(CivityHarvester):

def _get_harvest_catalog_setting(self, harvest_config_dict):
if HARVEST_CATALOG in harvest_config_dict:
log.debug("Using harvest_catalogs from harvest_config_dict")
harvest_catalog_setting = toolkit.asbool(
harvest_config_dict[HARVEST_CATALOG]
)
else:
log.debug("Using harvest_catalogs from global CKAN config")
harvest_catalog_setting = toolkit.asbool(
toolkit.config.get(HARVEST_CATALOG_CONFIG, False)
)
log.debug("Harvesting catalogs is set to %s", harvest_catalog_setting)
return harvest_catalog_setting

def setup_record_provider(self, harvest_url, harvest_config_dict):
# Harvest catalog config can be set on global CKAN level, but can be overriden by harvest config
harvest_catalogs = self._get_harvest_catalog_setting(harvest_config_dict)
Expand All @@ -52,9 +37,25 @@ def setup_record_to_package_converter(self, harvest_url, harvest_config_dict):
else:
raise Exception("[{0}] not found in harvester config JSON".format(PROFILE))

def info(self):
@staticmethod
def info():
return {
"name": "fair_data_point_harvester",
"title": "FAIR data point harvester",
"description": "Harvester for end points implementing the FAIR data point protocol",
}

@staticmethod
def _get_harvest_catalog_setting(harvest_config_dict):
if HARVEST_CATALOG in harvest_config_dict:
log.debug("Using harvest_catalogs from harvest_config_dict")
harvest_catalog_setting = toolkit.asbool(
harvest_config_dict[HARVEST_CATALOG]
)
else:
log.debug("Using harvest_catalogs from global CKAN config")
harvest_catalog_setting = toolkit.asbool(
toolkit.config.get(HARVEST_CATALOG_CONFIG, False)
)
log.debug("Harvesting catalogs is set to %s", harvest_catalog_setting)
return harvest_catalog_setting
10 changes: 10 additions & 0 deletions ckanext/fairdatapoint/tests/test_data/process_catalogs.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-FileCopyrightText: 2024 Stichting Health-RI
#
# SPDX-License-Identifier: AGPL-3.0-only

@prefix ldp: <http://www.w3.org/ns/ldp#> .
@prefix dcat: <http://www.w3.org/ns/dcat#> .
<http://example.com> ldp:contains <http://example.com/catalog1> .
<http://example.com/catalog1> a dcat:Catalog .
<http://example.com/catalog1> dcat:dataset <http://example.com/dataset1> .
<http://example.com/dataset1> a dcat:Dataset .
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# SPDX-FileCopyrightText: 2024 Stichting Health-RI
#
# SPDX-License-Identifier: AGPL-3.0-only

import unittest
import ckanext.fairdatapoint.plugin as plugin
from unittest.mock import patch, MagicMock
from ckanext.fairdatapoint.harvesters import FairDataPointCivityHarvester
from ckanext.fairdatapoint.harvesters import fair_data_point_civity_harvester


class TestFairDataPointCivityHarvester(unittest.TestCase):

def setUp(self):
plugin.toolkit = MagicMock()

def test_get_harvest_catalog_setting_from_dict(self):
harvester = FairDataPointCivityHarvester()
harvest_config_dict = {fair_data_point_civity_harvester.HARVEST_CATALOG: 'true'}
result = harvester._get_harvest_catalog_setting(harvest_config_dict)
self.assertTrue(result)

@patch('ckan.plugins.toolkit.config')
def test_get_harvest_catalog_setting_from_global_config(self, mock_config):
mock_config.get.return_value = 'false'
harvester = FairDataPointCivityHarvester()

harvest_config_dict = {}
result = harvester._get_harvest_catalog_setting(harvest_config_dict)

self.assertFalse(result)
mock_config.get.assert_called_once_with(fair_data_point_civity_harvester.HARVEST_CATALOG_CONFIG, False)

@patch('ckanext.fairdatapoint.harvesters.domain.fair_data_point_record_provider.FairDataPointRecordProvider'
'.__init__')
def test_setup_record_provider(self, mock_record_provider):
mock_record_provider.return_value = None
harvester = FairDataPointCivityHarvester()
harvester._get_harvest_catalog_setting = MagicMock(return_value=True)
harvest_url = 'http://example.com'
harvest_config_dict = {fair_data_point_civity_harvester.HARVEST_CATALOG: 'true'}
harvester.setup_record_provider(harvest_url, harvest_config_dict)
harvester._get_harvest_catalog_setting.assert_called_once_with(harvest_config_dict)
mock_record_provider.assert_called_once_with(harvest_url, True)

@patch(
'ckanext.fairdatapoint.harvesters.domain.fair_data_point_record_to_package_converter'
'.FairDataPointRecordToPackageConverter'
'.__init__')
def test_setup_record_to_package_converter_with_profile(self, mock_converter):
mock_converter.return_value = None
harvester = FairDataPointCivityHarvester()
harvest_url = 'http://example.com'
harvest_config_dict = {fair_data_point_civity_harvester.PROFILE: 'test_profile'}
harvester.setup_record_to_package_converter(harvest_url, harvest_config_dict)
mock_converter.assert_called_once_with('test_profile')

def test_setup_record_to_package_converter_raises_exception(self):
# Instantiate the harvester
harvester = FairDataPointCivityHarvester()

# Test data without PROFILE in the dictionary
harvest_url = 'http://example.com'
harvest_config_dict = {} # No PROFILE key

# Verify that an exception is raised when PROFILE is missing
with self.assertRaises(Exception) as context:
harvester.setup_record_to_package_converter(harvest_url, harvest_config_dict)

# Check the exception message
self.assertEqual(
str(context.exception),
"[profile] not found in harvester config JSON"
)
64 changes: 44 additions & 20 deletions ckanext/fairdatapoint/tests/test_record_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
TEST_DATA_DIRECTORY = Path(Path(__file__).parent.resolve(), "test_data")

TEST_CAT_IDS_DICT = {
"catalog=https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d":
URIRef("https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d"),
"catalog=https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d;"
"dataset=https://fair.healthinformationportal.eu/dataset/898ca4b8-197b-4d40-bc81-d9cd88197670":
URIRef("https://fair.healthinformationportal.eu/dataset/898ca4b8-197b-4d40-bc81-d9cd88197670"),
"catalog=https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4":
URIRef("https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4"),
"catalog=https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4;"
"dataset=https://fair.healthinformationportal.eu/dataset/32bd0246-b731-480a-b5f4-a2f60ccaebc9":
URIRef("https://fair.healthinformationportal.eu/dataset/32bd0246-b731-480a-b5f4-a2f60ccaebc9"),
"catalog=https://fair.healthinformationportal.eu/catalog/17412bc2-daf1-491e-94fb-6680f7a67b1e":
URIRef("https://fair.healthinformationportal.eu/catalog/17412bc2-daf1-491e-94fb-6680f7a67b1e")
}
"catalog=https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d":
URIRef("https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d"),
"catalog=https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d;"
"dataset=https://fair.healthinformationportal.eu/dataset/898ca4b8-197b-4d40-bc81-d9cd88197670":
URIRef("https://fair.healthinformationportal.eu/dataset/898ca4b8-197b-4d40-bc81-d9cd88197670"),
"catalog=https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4":
URIRef("https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4"),
"catalog=https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4;"
"dataset=https://fair.healthinformationportal.eu/dataset/32bd0246-b731-480a-b5f4-a2f60ccaebc9":
URIRef("https://fair.healthinformationportal.eu/dataset/32bd0246-b731-480a-b5f4-a2f60ccaebc9"),
"catalog=https://fair.healthinformationportal.eu/catalog/17412bc2-daf1-491e-94fb-6680f7a67b1e":
URIRef("https://fair.healthinformationportal.eu/catalog/17412bc2-daf1-491e-94fb-6680f7a67b1e")
}


def get_graph_by_id(*args, **kwargs):
Expand All @@ -39,12 +39,12 @@ class TestRecordProvider:
fdp_record_provider = FairDataPointRecordProvider("http://test_end_point.com")

@pytest.mark.parametrize("fdp_response_file,expected",
[
(Path(TEST_DATA_DIRECTORY, "root_fdp_response.ttl"),
TEST_CAT_IDS_DICT.keys()),
(Path(TEST_DATA_DIRECTORY, "root_fdp_response_no_catalogs.ttl"),
dict().keys())
])
[
(Path(TEST_DATA_DIRECTORY, "root_fdp_response.ttl"),
TEST_CAT_IDS_DICT.keys()),
(Path(TEST_DATA_DIRECTORY, "root_fdp_response_no_catalogs.ttl"),
dict().keys())
])
def test_get_record_ids(self, mocker, fdp_response_file, expected):
fdp_get_graph = mocker.MagicMock(name="get_data")
mocker.patch("ckanext.fairdatapoint.harvesters.domain.fair_data_point.FairDataPoint.get_graph",
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_get_record_by_id_distr(self, mocker):
def test_orcid_call(self, mocker):
"""if orcid url in contact point - add vcard full name"""
with requests_mock.Mocker() as mock:
mock.get("https://orcid.org/0000-0002-4348-707X/public-record.json",
mock.get("https://orcid.org/0000-0002-4348-707X/public-record.json",
json={"displayName": "N.K. De Vries"})
fdp_get_graph = mocker.MagicMock(name="get_data")
mocker.patch("ckanext.fairdatapoint.harvesters.domain.fair_data_point.FairDataPoint.get_graph",
Expand All @@ -105,3 +105,27 @@ def test_orcid_call(self, mocker):
expected = Graph().parse(Path(TEST_DATA_DIRECTORY, "Project_27866022694497978_out.ttl")).serialize()
assert mock.called
assert actual == expected

@pytest.mark.parametrize("harvest_catalogs, expected_results", [
(False, {
'catalog=http://example.com/catalog1;dataset=http://example.com/dataset1': URIRef(
'http://example.com/dataset1')
}),
(True, {
'catalog=http://example.com/catalog1;dataset=http://example.com/dataset1': URIRef(
'http://example.com/dataset1'),
'catalog=http://example.com/catalog1': URIRef('http://example.com/catalog1')
})
])
def test_process_catalogs(self, mocker, harvest_catalogs, expected_results):
fdp_get_graph = mocker.MagicMock(name="get_data")
mocker.patch("ckanext.fairdatapoint.harvesters.domain.fair_data_point.FairDataPoint.get_graph",
new=fdp_get_graph)
fdp_get_graph.return_value = Graph().parse(Path(TEST_DATA_DIRECTORY, "process_catalogs.ttl"))

self.fdp_record_provider.harvest_catalogs = harvest_catalogs
actual_result = self.fdp_record_provider._process_catalog("http://example.com/catalog1")

# Assertions
for key, value in expected_results.items():
assert actual_result[key] == value

0 comments on commit 61ef7c0

Please sign in to comment.