|
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 ckan.plugins import toolkit |
12 | 13 | from ckanext.xloader.plugin import _should_remove_unsupported_resource_from_datastore
|
13 | 14 |
|
14 | 15 |
|
15 | 16 | @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.params |
19 |
| - yield mock_get |
| 17 | +def toolkit_config_value(request): |
| 18 | + _original_config = toolkit.config.copy() |
| 19 | + toolkit.config['ckanext.xloader.clean_datastore_tables'] = request.param |
| 20 | + try: |
| 21 | + yield |
| 22 | + finally: |
| 23 | + toolkit.config.clear() |
| 24 | + toolkit.config.update(_original_config) |
20 | 25 |
|
21 | 26 |
|
22 | 27 | @pytest.fixture
|
23 | 28 | def mock_xloader_formats(request):
|
24 | 29 | with mock.patch('ckanext.xloader.plugin.XLoaderFormats.is_it_an_xloader_format') as mock_is_xloader_format:
|
25 |
| - mock_is_xloader_format.return_value = request.params |
| 30 | + mock_is_xloader_format.return_value = request.param |
26 | 31 | yield mock_is_xloader_format
|
27 | 32 |
|
28 | 33 |
|
@@ -73,50 +78,44 @@ def test_submit_when_url_changes(self, monkeypatch):
|
73 | 78 |
|
74 | 79 | assert func.called
|
75 | 80 |
|
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 |
| - ]) |
| 81 | + @pytest.mark.usefixtures("toolkit_config_value", "mock_xloader_formats") |
| 82 | + @pytest.mark.parametrize("toolkit_config_value, mock_xloader_formats, url_type, datastore_active, expected_result", |
| 83 | + [(True, False, 'upload', True, True), # Test1 |
| 84 | + (True, True, 'upload', True, False), # Test2 |
| 85 | + (False, False, 'upload', True, False), # Test3 |
| 86 | + (True, False, 'custom_type', True, False), # Test4 |
| 87 | + (True, False, 'upload', False, False), # Test5 |
| 88 | + (True, False, '', True, True), # Test6 |
| 89 | + (True, False, None, True, True), # Test7 |
| 90 | + ], indirect=["toolkit_config_value", "mock_xloader_formats"]) |
86 | 91 | 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): |
| 92 | + toolkit_config_value, mock_xloader_formats, url_type, datastore_active, expected_result): |
89 | 93 |
|
90 | 94 | # 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. |
| 95 | + # Should pass as it is not an XLoader format and supported url type and datastore active. |
92 | 96 | # 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. |
| 97 | + # Should fail as it is a supported XLoader format. |
94 | 98 | # Test3: clean_datastore_tables=False, is_it_an_xloader_format=True, url_type='upload', datastore_active=True, expected_result=False
|
95 | 99 | # 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 |
| 100 | + # Test4: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='custom_type', datastore_active=True, expected_result=False |
99 | 101 | # 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 |
| 102 | + # Test5: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='upload', datastore_active=False, expected_result=False |
101 | 103 | # 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. |
| 104 | + # Test6: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='', datastore_active=True, expected_result=True |
| 105 | + # Should pass as it is not an XLoader format and supported url type and datastore active. |
| 106 | + # Test7: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type=None, datastore_active=True, expected_result=True |
| 107 | + # Should pass as it is not an XLoader format and supported url type as falsy and datastore active. |
106 | 108 |
|
107 | 109 | # Setup mock data
|
108 | 110 | res_dict = {
|
109 | 111 | 'format': 'some_format',
|
110 | 112 | 'url_type': url_type,
|
111 |
| - 'datastore_active': True, |
112 |
| - 'extras': {'datastore_active': True} |
| 113 | + 'datastore_active': datastore_active, |
| 114 | + 'extras': {'datastore_active': datastore_active} |
113 | 115 | }
|
114 | 116 |
|
115 |
| - # Call the function |
116 |
| - result = _should_remove_unsupported_resource_from_datastore(res_dict) |
117 |
| - |
118 | 117 | # Assert the result based on the logic paths covered
|
119 |
| - assert result == expected_result |
| 118 | + assert _should_remove_unsupported_resource_from_datastore(res_dict) == expected_result |
120 | 119 |
|
121 | 120 | def _pending_task(self, resource_id):
|
122 | 121 | return {
|
|
0 commit comments