From 407db19106994752bdfc363d7a04db29344a31fe Mon Sep 17 00:00:00 2001 From: Jesse Vickery Date: Wed, 31 Jan 2024 18:23:10 +0000 Subject: [PATCH] feat(tests): added new test; - Added new test for should remove unsupported resources from datastore. --- ckanext/xloader/tests/test_plugin.py | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/ckanext/xloader/tests/test_plugin.py b/ckanext/xloader/tests/test_plugin.py index 05b83b5b..8988e750 100644 --- a/ckanext/xloader/tests/test_plugin.py +++ b/ckanext/xloader/tests/test_plugin.py @@ -9,6 +9,21 @@ from six import text_type as str from ckan.tests import helpers, factories from ckan.logic import _actions +from ckanext.xloader.plugin import _should_remove_unsupported_resource_from_datastore + + +@pytest.fixture +def mock_toolkit_config(request): + with mock.patch('ckan.plugins.toolkit.config.get') as mock_get: + mock_get.return_value = request.param + yield mock_get + + +@pytest.fixture +def mock_xloader_formats(request): + with mock.patch('ckanext.xloader.utils.XLoaderFormats.is_it_an_xloader_format') as mock_is_xloader_format: + mock_is_xloader_format.return_value = request.param + yield mock_is_xloader_format @pytest.mark.usefixtures("clean_db", "with_plugins") @@ -58,6 +73,51 @@ def test_submit_when_url_changes(self, monkeypatch): assert func.called + @pytest.mark.parametrize("toolkit_config_value, xloader_formats_value, url_type, datastore_active, expected_result", + [(True, True, 'upload', True, True), # Test1 + (True, False, 'upload', True, False), # Test2 + (False, True, 'upload', True, False), # Test3 + (False, False, 'upload', True, False), # Test4 + (True, True, 'custom_type', True, False), # Test5 + (True, True, 'upload', False, False), # Test6 + (True, True, '', True, True), # Test7 + (True, True, None, True, True), # Test8 + ]) + def test_should_remove_unsupported_resource_from_datastore( + mock_toolkit_config, mock_xloader_formats, toolkit_config_value, + xloader_formats_value, url_type, datastore_active, expected_result): + + # Test1: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='upload', datastore_active=True, expected_result=True + # Should pass as it is an Xloader format and supported url type and datastore active. + # Test2: clean_datastore_tables=True, is_it_an_xloader_format=False, url_type='upload', datastore_active=True, expected_result=False + # Should fail as it is not a supported Xloader format. + # Test3: clean_datastore_tables=False, is_it_an_xloader_format=True, url_type='upload', datastore_active=True, expected_result=False + # Should fail as the config option is turned off. + # Test4: clean_datastore_tables=False, is_it_an_xloader_format=False, url_type='upload', datastore_active=True, expected_result=False + # Should fail as the config option is turned off and the Xloader format is not supported. + # Test5: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='custom_type', datastore_active=True, expected_result=False + # Should fail as the url_type is not supported. + # Test6: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='upload', datastore_active=False, expected_result=False + # Should fail as datastore is inactive. + # Test7: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='', datastore_active=True, expected_result=True + # Should pass as it is an Xloader format and supported url type and datastore active. + # Test8: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type=None, datastore_active=True, expected_result=True + # Should pass as it is an Xloader format and supported url type as falsy and datastore active. + + # Setup mock data + res_dict = { + 'format': 'some_format', + 'url_type': url_type, + 'datastore_active': True, + 'extras': {'datastore_active': True} + } + + # Call the function + result = _should_remove_unsupported_resource_from_datastore(res_dict) + + # Assert the result based on the logic paths covered + assert result == expected_result + def _pending_task(self, resource_id): return { "entity_id": resource_id,