|
9 | 9 | from six import text_type as str
|
10 | 10 | from ckan.tests import helpers, factories
|
11 | 11 | 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 |
12 | 27 |
|
13 | 28 |
|
14 | 29 | @pytest.mark.usefixtures("clean_db", "with_plugins")
|
@@ -58,6 +73,51 @@ def test_submit_when_url_changes(self, monkeypatch):
|
58 | 73 |
|
59 | 74 | assert func.called
|
60 | 75 |
|
| 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 | + |
61 | 121 | def _pending_task(self, resource_id):
|
62 | 122 | return {
|
63 | 123 | "entity_id": resource_id,
|
|
0 commit comments