Skip to content

Commit 61ef7c0

Browse files
author
Hans-Chrstian
committed
test(fairdatapoint): fix unit tests for FairDataPointCivityHarvester setup methods
1 parent 0f1fc2c commit 61ef7c0

File tree

4 files changed

+145
-36
lines changed

4 files changed

+145
-36
lines changed

ckanext/fairdatapoint/harvesters/fair_data_point_civity_harvester.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,6 @@
2121

2222

2323
class FairDataPointCivityHarvester(CivityHarvester):
24-
25-
def _get_harvest_catalog_setting(self, harvest_config_dict):
26-
if HARVEST_CATALOG in harvest_config_dict:
27-
log.debug("Using harvest_catalogs from harvest_config_dict")
28-
harvest_catalog_setting = toolkit.asbool(
29-
harvest_config_dict[HARVEST_CATALOG]
30-
)
31-
else:
32-
log.debug("Using harvest_catalogs from global CKAN config")
33-
harvest_catalog_setting = toolkit.asbool(
34-
toolkit.config.get(HARVEST_CATALOG_CONFIG, False)
35-
)
36-
log.debug("Harvesting catalogs is set to %s", harvest_catalog_setting)
37-
return harvest_catalog_setting
38-
3924
def setup_record_provider(self, harvest_url, harvest_config_dict):
4025
# Harvest catalog config can be set on global CKAN level, but can be overriden by harvest config
4126
harvest_catalogs = self._get_harvest_catalog_setting(harvest_config_dict)
@@ -52,9 +37,25 @@ def setup_record_to_package_converter(self, harvest_url, harvest_config_dict):
5237
else:
5338
raise Exception("[{0}] not found in harvester config JSON".format(PROFILE))
5439

55-
def info(self):
40+
@staticmethod
41+
def info():
5642
return {
5743
"name": "fair_data_point_harvester",
5844
"title": "FAIR data point harvester",
5945
"description": "Harvester for end points implementing the FAIR data point protocol",
6046
}
47+
48+
@staticmethod
49+
def _get_harvest_catalog_setting(harvest_config_dict):
50+
if HARVEST_CATALOG in harvest_config_dict:
51+
log.debug("Using harvest_catalogs from harvest_config_dict")
52+
harvest_catalog_setting = toolkit.asbool(
53+
harvest_config_dict[HARVEST_CATALOG]
54+
)
55+
else:
56+
log.debug("Using harvest_catalogs from global CKAN config")
57+
harvest_catalog_setting = toolkit.asbool(
58+
toolkit.config.get(HARVEST_CATALOG_CONFIG, False)
59+
)
60+
log.debug("Harvesting catalogs is set to %s", harvest_catalog_setting)
61+
return harvest_catalog_setting
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-FileCopyrightText: 2024 Stichting Health-RI
2+
#
3+
# SPDX-License-Identifier: AGPL-3.0-only
4+
5+
@prefix ldp: <http://www.w3.org/ns/ldp#> .
6+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
7+
<http://example.com> ldp:contains <http://example.com/catalog1> .
8+
<http://example.com/catalog1> a dcat:Catalog .
9+
<http://example.com/catalog1> dcat:dataset <http://example.com/dataset1> .
10+
<http://example.com/dataset1> a dcat:Dataset .
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# SPDX-FileCopyrightText: 2024 Stichting Health-RI
2+
#
3+
# SPDX-License-Identifier: AGPL-3.0-only
4+
5+
import unittest
6+
import ckanext.fairdatapoint.plugin as plugin
7+
from unittest.mock import patch, MagicMock
8+
from ckanext.fairdatapoint.harvesters import FairDataPointCivityHarvester
9+
from ckanext.fairdatapoint.harvesters import fair_data_point_civity_harvester
10+
11+
12+
class TestFairDataPointCivityHarvester(unittest.TestCase):
13+
14+
def setUp(self):
15+
plugin.toolkit = MagicMock()
16+
17+
def test_get_harvest_catalog_setting_from_dict(self):
18+
harvester = FairDataPointCivityHarvester()
19+
harvest_config_dict = {fair_data_point_civity_harvester.HARVEST_CATALOG: 'true'}
20+
result = harvester._get_harvest_catalog_setting(harvest_config_dict)
21+
self.assertTrue(result)
22+
23+
@patch('ckan.plugins.toolkit.config')
24+
def test_get_harvest_catalog_setting_from_global_config(self, mock_config):
25+
mock_config.get.return_value = 'false'
26+
harvester = FairDataPointCivityHarvester()
27+
28+
harvest_config_dict = {}
29+
result = harvester._get_harvest_catalog_setting(harvest_config_dict)
30+
31+
self.assertFalse(result)
32+
mock_config.get.assert_called_once_with(fair_data_point_civity_harvester.HARVEST_CATALOG_CONFIG, False)
33+
34+
@patch('ckanext.fairdatapoint.harvesters.domain.fair_data_point_record_provider.FairDataPointRecordProvider'
35+
'.__init__')
36+
def test_setup_record_provider(self, mock_record_provider):
37+
mock_record_provider.return_value = None
38+
harvester = FairDataPointCivityHarvester()
39+
harvester._get_harvest_catalog_setting = MagicMock(return_value=True)
40+
harvest_url = 'http://example.com'
41+
harvest_config_dict = {fair_data_point_civity_harvester.HARVEST_CATALOG: 'true'}
42+
harvester.setup_record_provider(harvest_url, harvest_config_dict)
43+
harvester._get_harvest_catalog_setting.assert_called_once_with(harvest_config_dict)
44+
mock_record_provider.assert_called_once_with(harvest_url, True)
45+
46+
@patch(
47+
'ckanext.fairdatapoint.harvesters.domain.fair_data_point_record_to_package_converter'
48+
'.FairDataPointRecordToPackageConverter'
49+
'.__init__')
50+
def test_setup_record_to_package_converter_with_profile(self, mock_converter):
51+
mock_converter.return_value = None
52+
harvester = FairDataPointCivityHarvester()
53+
harvest_url = 'http://example.com'
54+
harvest_config_dict = {fair_data_point_civity_harvester.PROFILE: 'test_profile'}
55+
harvester.setup_record_to_package_converter(harvest_url, harvest_config_dict)
56+
mock_converter.assert_called_once_with('test_profile')
57+
58+
def test_setup_record_to_package_converter_raises_exception(self):
59+
# Instantiate the harvester
60+
harvester = FairDataPointCivityHarvester()
61+
62+
# Test data without PROFILE in the dictionary
63+
harvest_url = 'http://example.com'
64+
harvest_config_dict = {} # No PROFILE key
65+
66+
# Verify that an exception is raised when PROFILE is missing
67+
with self.assertRaises(Exception) as context:
68+
harvester.setup_record_to_package_converter(harvest_url, harvest_config_dict)
69+
70+
# Check the exception message
71+
self.assertEqual(
72+
str(context.exception),
73+
"[profile] not found in harvester config JSON"
74+
)

ckanext/fairdatapoint/tests/test_record_provider.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
TEST_DATA_DIRECTORY = Path(Path(__file__).parent.resolve(), "test_data")
1313

1414
TEST_CAT_IDS_DICT = {
15-
"catalog=https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d":
16-
URIRef("https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d"),
17-
"catalog=https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d;"
18-
"dataset=https://fair.healthinformationportal.eu/dataset/898ca4b8-197b-4d40-bc81-d9cd88197670":
19-
URIRef("https://fair.healthinformationportal.eu/dataset/898ca4b8-197b-4d40-bc81-d9cd88197670"),
20-
"catalog=https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4":
21-
URIRef("https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4"),
22-
"catalog=https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4;"
23-
"dataset=https://fair.healthinformationportal.eu/dataset/32bd0246-b731-480a-b5f4-a2f60ccaebc9":
24-
URIRef("https://fair.healthinformationportal.eu/dataset/32bd0246-b731-480a-b5f4-a2f60ccaebc9"),
25-
"catalog=https://fair.healthinformationportal.eu/catalog/17412bc2-daf1-491e-94fb-6680f7a67b1e":
26-
URIRef("https://fair.healthinformationportal.eu/catalog/17412bc2-daf1-491e-94fb-6680f7a67b1e")
27-
}
15+
"catalog=https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d":
16+
URIRef("https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d"),
17+
"catalog=https://fair.healthinformationportal.eu/catalog/1c75c2c9-d2cc-44cb-aaa8-cf8c11515c8d;"
18+
"dataset=https://fair.healthinformationportal.eu/dataset/898ca4b8-197b-4d40-bc81-d9cd88197670":
19+
URIRef("https://fair.healthinformationportal.eu/dataset/898ca4b8-197b-4d40-bc81-d9cd88197670"),
20+
"catalog=https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4":
21+
URIRef("https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4"),
22+
"catalog=https://fair.healthinformationportal.eu/catalog/14225c50-00b0-4fba-8300-a677ab0c86f4;"
23+
"dataset=https://fair.healthinformationportal.eu/dataset/32bd0246-b731-480a-b5f4-a2f60ccaebc9":
24+
URIRef("https://fair.healthinformationportal.eu/dataset/32bd0246-b731-480a-b5f4-a2f60ccaebc9"),
25+
"catalog=https://fair.healthinformationportal.eu/catalog/17412bc2-daf1-491e-94fb-6680f7a67b1e":
26+
URIRef("https://fair.healthinformationportal.eu/catalog/17412bc2-daf1-491e-94fb-6680f7a67b1e")
27+
}
2828

2929

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

4141
@pytest.mark.parametrize("fdp_response_file,expected",
42-
[
43-
(Path(TEST_DATA_DIRECTORY, "root_fdp_response.ttl"),
44-
TEST_CAT_IDS_DICT.keys()),
45-
(Path(TEST_DATA_DIRECTORY, "root_fdp_response_no_catalogs.ttl"),
46-
dict().keys())
47-
])
42+
[
43+
(Path(TEST_DATA_DIRECTORY, "root_fdp_response.ttl"),
44+
TEST_CAT_IDS_DICT.keys()),
45+
(Path(TEST_DATA_DIRECTORY, "root_fdp_response_no_catalogs.ttl"),
46+
dict().keys())
47+
])
4848
def test_get_record_ids(self, mocker, fdp_response_file, expected):
4949
fdp_get_graph = mocker.MagicMock(name="get_data")
5050
mocker.patch("ckanext.fairdatapoint.harvesters.domain.fair_data_point.FairDataPoint.get_graph",
@@ -92,7 +92,7 @@ def test_get_record_by_id_distr(self, mocker):
9292
def test_orcid_call(self, mocker):
9393
"""if orcid url in contact point - add vcard full name"""
9494
with requests_mock.Mocker() as mock:
95-
mock.get("https://orcid.org/0000-0002-4348-707X/public-record.json",
95+
mock.get("https://orcid.org/0000-0002-4348-707X/public-record.json",
9696
json={"displayName": "N.K. De Vries"})
9797
fdp_get_graph = mocker.MagicMock(name="get_data")
9898
mocker.patch("ckanext.fairdatapoint.harvesters.domain.fair_data_point.FairDataPoint.get_graph",
@@ -105,3 +105,27 @@ def test_orcid_call(self, mocker):
105105
expected = Graph().parse(Path(TEST_DATA_DIRECTORY, "Project_27866022694497978_out.ttl")).serialize()
106106
assert mock.called
107107
assert actual == expected
108+
109+
@pytest.mark.parametrize("harvest_catalogs, expected_results", [
110+
(False, {
111+
'catalog=http://example.com/catalog1;dataset=http://example.com/dataset1': URIRef(
112+
'http://example.com/dataset1')
113+
}),
114+
(True, {
115+
'catalog=http://example.com/catalog1;dataset=http://example.com/dataset1': URIRef(
116+
'http://example.com/dataset1'),
117+
'catalog=http://example.com/catalog1': URIRef('http://example.com/catalog1')
118+
})
119+
])
120+
def test_process_catalogs(self, mocker, harvest_catalogs, expected_results):
121+
fdp_get_graph = mocker.MagicMock(name="get_data")
122+
mocker.patch("ckanext.fairdatapoint.harvesters.domain.fair_data_point.FairDataPoint.get_graph",
123+
new=fdp_get_graph)
124+
fdp_get_graph.return_value = Graph().parse(Path(TEST_DATA_DIRECTORY, "process_catalogs.ttl"))
125+
126+
self.fdp_record_provider.harvest_catalogs = harvest_catalogs
127+
actual_result = self.fdp_record_provider._process_catalog("http://example.com/catalog1")
128+
129+
# Assertions
130+
for key, value in expected_results.items():
131+
assert actual_result[key] == value

0 commit comments

Comments
 (0)