Skip to content

Commit

Permalink
Merge branch 'master' of github.com:frictionlessdata/ckanext-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Dec 1, 2022
2 parents e61df5e + 370eb09 commit 1073c80
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
18 changes: 15 additions & 3 deletions ckanext/validation/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,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 @@ -151,10 +154,11 @@ 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 not is_dataset:
context["_resource_create_call"] = True
return self._process_schema_fields(data_dict)

def after_create(self, context, data_dict):

Expand Down Expand Up @@ -267,6 +271,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
54 changes: 54 additions & 0 deletions ckanext/validation/tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,60 @@ 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

0 comments on commit 1073c80

Please sign in to comment.