Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't process extra resources on resource_create #74

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions ckanext/validation/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def get_helpers(self):

# IResourceController

resources_to_validate = {}
packages_to_skip = {}

def _process_schema_fields(self, data_dict):
u'''
Normalize the different ways of providing the `schema` field
Expand Down Expand Up @@ -143,10 +146,12 @@ def _process_schema_fields(self, data_dict):
return data_dict

def before_create(self, context, data_dict):
return self._process_schema_fields(data_dict)

resources_to_validate = {}
packages_to_skip = {}
is_dataset = self._data_dict_is_dataset(data_dict)
if is_dataset:
return self._process_schema_fields(data_dict)
else:
context["_resource_create_call"] = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CKAN 2.10 will restrict the allowed context keys...


def after_create(self, context, data_dict):

Expand Down Expand Up @@ -259,6 +264,14 @@ def after_update(self, context, data_dict):
# in both cases, we don't need to validate every resource.
return

if context.pop("_resource_create_call", False):
new_resource = data_dict["resources"][-1]
if new_resource:
# This is part of a resource_create call, we only need to validate
# the new resource being created
self._handle_validation_for_resource(context, new_resource)
return

for resource in data_dict.get(u'resources', []):
if resource[u'id'] in self.resources_to_validate:
# This is part of a resource_update call, it will be
Expand Down
53 changes: 53 additions & 0 deletions ckanext/validation/tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,59 @@ def test_resource_validation_resets_existing_validation_object(
assert validation.report is None
assert validation.error is None

@mock.patch("ckanext.validation.logic.enqueue_job")
def test_resource_validation_only_called_on_resource_created(
self, mock_enqueue_job
):

resource1 = {"format": "CSV", "url": "https://some.url"}

dataset = factories.Dataset(resources=[resource1])

assert mock_enqueue_job.call_count == 1
assert mock_enqueue_job.call_args[0][1][0]["id"] == dataset["resources"][0]["id"]

mock_enqueue_job.reset_mock()

resource2 = call_action(
"resource_create",
package_id=dataset["id"],
name="resource_2",
format="CSV",
url="https://some.url"
)

assert mock_enqueue_job.call_count == 1
assert mock_enqueue_job.call_args[0][1][0]["id"] == resource2["id"]

@mock.patch("ckanext.validation.logic.enqueue_job")
def test_resource_validation_only_called_on_resource_updated(
self, mock_enqueue_job
):

resource1 = {"name": "resource_1", "format": "CSV", "url": "https://some.url"}
resource2 = {"name": "resource_2", "format": "CSV", "url": "https://some.url"}

dataset = factories.Dataset(resources=[resource1, resource2])

assert mock_enqueue_job.call_count == 2

mock_enqueue_job.reset_mock()

resource_1_id = [r["id"] for r in dataset["resources"] if r["name"] == "resource_1"][0]

call_action(
"resource_update",
id=resource_1_id,
name="resource_1",
format="CSV",
url="https://some.updated.url",
description="updated"
)

assert mock_enqueue_job.call_count == 1
assert mock_enqueue_job.call_args[0][1][0]["id"] == resource_1_id


@pytest.mark.usefixtures("clean_db", "validation_setup", "with_plugins")
class TestResourceValidationShow(object):
Expand Down