Skip to content

Commit cef20a9

Browse files
committed
feat(tests): added new test;
- Added new test for should remove unsupported resources from datastore.
1 parent e454e90 commit cef20a9

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

ckanext/xloader/plugin.py

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def notify(self, entity, operation):
9696
toolkit.enqueue_job(fn=_remove_unsupported_resource_from_datastore, args=[entity.id])
9797

9898
if not getattr(entity, 'url_changed', False):
99+
# do not submit to xloader if the url has not changed.
99100
return
100101

101102
self._submit_to_xloader(resource_dict)

ckanext/xloader/tests/test_plugin.py

+60
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
from six import text_type as str
1010
from ckan.tests import helpers, factories
1111
from ckan.logic import _actions
12+
from ckanext.xloader.plugin import _should_remove_unsupported_resource_from_datastore
13+
14+
15+
@pytest.fixture
16+
def mock_toolkit_config(request):
17+
with mock.patch('ckan.plugins.toolkit.config.get') as mock_get:
18+
mock_get.return_value = request.param
19+
yield mock_get
20+
21+
22+
@pytest.fixture
23+
def mock_xloader_formats(request):
24+
with mock.patch('ckanext.xloader.utils.XLoaderFormats.is_it_an_xloader_format') as mock_is_xloader_format:
25+
mock_is_xloader_format.return_value = request.param
26+
yield mock_is_xloader_format
1227

1328

1429
@pytest.mark.usefixtures("clean_db", "with_plugins")
@@ -58,6 +73,51 @@ def test_submit_when_url_changes(self, monkeypatch):
5873

5974
assert func.called
6075

76+
@pytest.mark.parametrize("toolkit_config_value, xloader_formats_value, url_type, datastore_active, expected_result",
77+
[(True, True, 'upload', True, True), # Test1
78+
(True, False, 'upload', True, False), # Test2
79+
(False, True, 'upload', True, False), # Test3
80+
(False, False, 'upload', True, False), # Test4
81+
(True, True, 'custom_type', True, False), # Test5
82+
(True, True, 'upload', False, False), # Test6
83+
(True, True, '', True, True), # Test7
84+
(True, True, None, True, True), # Test8
85+
])
86+
def test_should_remove_unsupported_resource_from_datastore(
87+
mock_toolkit_config, mock_xloader_formats, toolkit_config_value,
88+
xloader_formats_value, url_type, datastore_active, expected_result):
89+
90+
# Test1: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='upload', datastore_active=True, expected_result=True
91+
# Should pass as it is an Xloader format and supported url type and datastore active.
92+
# Test2: clean_datastore_tables=True, is_it_an_xloader_format=False, url_type='upload', datastore_active=True, expected_result=False
93+
# Should fail as it is not a supported Xloader format.
94+
# Test3: clean_datastore_tables=False, is_it_an_xloader_format=True, url_type='upload', datastore_active=True, expected_result=False
95+
# Should fail as the config option is turned off.
96+
# Test4: clean_datastore_tables=False, is_it_an_xloader_format=False, url_type='upload', datastore_active=True, expected_result=False
97+
# Should fail as the config option is turned off and the Xloader format is not supported.
98+
# Test5: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='custom_type', datastore_active=True, expected_result=False
99+
# Should fail as the url_type is not supported.
100+
# Test6: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='upload', datastore_active=False, expected_result=False
101+
# Should fail as datastore is inactive.
102+
# Test7: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='', datastore_active=True, expected_result=True
103+
# Should pass as it is an Xloader format and supported url type and datastore active.
104+
# Test8: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type=None, datastore_active=True, expected_result=True
105+
# Should pass as it is an Xloader format and supported url type as falsy and datastore active.
106+
107+
# Setup mock data
108+
res_dict = {
109+
'format': 'some_format',
110+
'url_type': url_type,
111+
'datastore_active': True,
112+
'extras': {'datastore_active': True}
113+
}
114+
115+
# Call the function
116+
result = _should_remove_unsupported_resource_from_datastore(res_dict)
117+
118+
# Assert the result based on the logic paths covered
119+
assert result == expected_result
120+
61121
def _pending_task(self, resource_id):
62122
return {
63123
"entity_id": resource_id,

0 commit comments

Comments
 (0)