Skip to content

Commit

Permalink
Merge pull request #4 from qld-gov-au/feature/qld-gov-au/remove-unsup…
Browse files Browse the repository at this point in the history
…ported-datastore-tables

replace complex indirect fixtures with simple 'with' blocks
  • Loading branch information
JVickery-TBS authored Feb 2, 2024
2 parents d7236fe + 8b6cab9 commit 60e9f34
Showing 1 changed file with 22 additions and 46 deletions.
68 changes: 22 additions & 46 deletions ckanext/xloader/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,12 @@
except ImportError:
import mock
from six import text_type as str

from ckan.tests import helpers, factories
from ckan.logic import _actions
from ckan.plugins import toolkit
from ckanext.xloader.plugin import _should_remove_unsupported_resource_from_datastore


@pytest.fixture
def toolkit_config_value(request):
_original_config = toolkit.config.copy()
toolkit.config['ckanext.xloader.clean_datastore_tables'] = request.param
try:
yield
finally:
toolkit.config.clear()
toolkit.config.update(_original_config)


@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")
@pytest.mark.ckan_config("ckan.plugins", "datastore xloader")
class TestNotify(object):
Expand Down Expand Up @@ -78,33 +60,24 @@ def test_submit_when_url_changes(self, monkeypatch):

assert func.called

@pytest.mark.usefixtures("toolkit_config_value", "mock_xloader_formats")
@pytest.mark.parametrize("toolkit_config_value, mock_xloader_formats, url_type, datastore_active, expected_result",
[(True, False, 'upload', True, True), # Test1
(True, True, 'upload', True, False), # Test2
(False, False, 'upload', True, False), # Test3
(True, False, 'custom_type', True, False), # Test4
(True, False, 'upload', False, False), # Test5
(True, False, '', True, True), # Test6
(True, False, None, True, True), # Test7
], indirect=["toolkit_config_value", "mock_xloader_formats"])
@pytest.mark.parametrize("toolkit_config_value, mock_xloader_formats, url_type, datastore_active, expected_result", [
# Test1: Should pass as it is an upload with an active datastore entry but an unsupported format
(True, False, 'upload', True, True),
# Test2: Should fail as it is a supported XLoader format.
(True, True, 'upload', True, False),
# Test3: Should fail as the config option is turned off.
(False, False, 'upload', True, False),
# Test4: Should fail as the url_type is not supported.
(True, False, 'custom_type', True, False),
# Test5: Should fail as datastore is inactive.
(True, False, 'upload', False, False),
# Test6: Should pass as it is a recognised resource type with an active datastore entry but an unsupported format
(True, False, '', True, True),
# Test7: Should pass as it is a recognised resource type with an active datastore entry but an unsupported format
(True, False, None, True, True),
])
def test_should_remove_unsupported_resource_from_datastore(
toolkit_config_value, mock_xloader_formats, 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 not 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 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=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.
# Test5: 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.
# Test6: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type='', datastore_active=True, expected_result=True
# Should pass as it is not an XLoader format and supported url type and datastore active.
# Test7: clean_datastore_tables=True, is_it_an_xloader_format=True, url_type=None, datastore_active=True, expected_result=True
# Should pass as it is not an XLoader format and supported url type as falsy and datastore active.
self, toolkit_config_value, mock_xloader_formats, url_type, datastore_active, expected_result):

# Setup mock data
res_dict = {
Expand All @@ -115,7 +88,10 @@ def test_should_remove_unsupported_resource_from_datastore(
}

# Assert the result based on the logic paths covered
assert _should_remove_unsupported_resource_from_datastore(res_dict) == expected_result
with helpers.changed_config('ckanext.xloader.clean_datastore_tables', toolkit_config_value):
with mock.patch('ckanext.xloader.utils.XLoaderFormats.is_it_an_xloader_format') as mock_is_xloader_format:
mock_is_xloader_format.return_value = mock_xloader_formats
assert _should_remove_unsupported_resource_from_datastore(res_dict) == expected_result

def _pending_task(self, resource_id):
return {
Expand Down

0 comments on commit 60e9f34

Please sign in to comment.