Skip to content

Commit 4612484

Browse files
committed
feat(dev): better logic and tests;
- Added automated tests. - Changed the logic to be better and more clear.
1 parent 5c07ba4 commit 4612484

File tree

3 files changed

+93
-24
lines changed

3 files changed

+93
-24
lines changed

ckanext/xloader/plugin.py

-16
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,6 @@ def notify(self, entity, operation):
8282
or not isinstance(entity, Resource):
8383
return
8484

85-
# If the resource requires validation, stop here if validation
86-
# has not been performed or did not succeed. The Validation
87-
# extension will call resource_patch and this method should
88-
# be called again. However, url_changed will not be in the entity
89-
# once Validation does the patch.
90-
if utils.awaiting_validation() and \
91-
toolkit.asbool(toolkit.config.get('ckanext.xloader.requires_validation')):
92-
93-
if entity.__dict__.get('extras', {}).get('validation_status', None) != 'success':
94-
log.debug("Skipping xloading resource %s because the "
95-
"resource did not pass validation yet.", entity.id)
96-
return
97-
98-
elif not getattr(entity, 'url_changed', False):
99-
return
100-
10185
context = {
10286
"ignore_auth": True,
10387
}

ckanext/xloader/tests/test_plugin.py

+82
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,88 @@ def test_submit_when_url_changes(self, monkeypatch):
5858

5959
assert func.called
6060

61+
@pytest.mark.ckan_config("ckanext.xloader.validation.requires_successful_report", True)
62+
def test_require_validation(self, monkeypatch):
63+
func = mock.Mock()
64+
monkeypatch.setitem(_actions, "xloader_submit", func)
65+
66+
mock_resource_validation_show = mock.Mock()
67+
monkeypatch.setitem(_actions, "resource_validation_show", mock_resource_validation_show)
68+
69+
dataset = factories.Dataset()
70+
71+
resource = helpers.call_action(
72+
"resource_create",
73+
{},
74+
package_id=dataset["id"],
75+
url="http://example.com/file.csv",
76+
format="CSV",
77+
validation_status='failure',
78+
)
79+
80+
assert not func.called # because of the validation_status not being `success`
81+
func.called = None # reset
82+
83+
helpers.call_action(
84+
"resource_update",
85+
{},
86+
id=resource["id"],
87+
package_id=dataset["id"],
88+
url="http://example.com/file2.csv",
89+
format="CSV",
90+
validation_status='success',
91+
)
92+
93+
assert func.called # because of the validation_status is `success`
94+
95+
@pytest.mark.ckan_config("ckanext.xloader.validation.requires_successful_report", True)
96+
@pytest.mark.ckan_config("ckanext.xloader.validation.enforce_schema", False)
97+
def test_enforce_validation_schema(self, monkeypatch):
98+
func = mock.Mock()
99+
monkeypatch.setitem(_actions, "xloader_submit", func)
100+
101+
mock_resource_validation_show = mock.Mock()
102+
monkeypatch.setitem(_actions, "resource_validation_show", mock_resource_validation_show)
103+
104+
dataset = factories.Dataset()
105+
106+
resource = helpers.call_action(
107+
"resource_create",
108+
{},
109+
package_id=dataset["id"],
110+
url="http://example.com/file.csv",
111+
schema='',
112+
validation_status='',
113+
)
114+
115+
assert func.called # because of the schema being empty
116+
func.called = None # reset
117+
118+
helpers.call_action(
119+
"resource_update",
120+
{},
121+
id=resource["id"],
122+
package_id=dataset["id"],
123+
url="http://example.com/file2.csv",
124+
schema='https://example.com/schema.json',
125+
validation_status='failure',
126+
)
127+
128+
assert not func.called # because of the validation_status not being `success` and there is a schema
129+
func.called = None # reset
130+
131+
helpers.call_action(
132+
"resource_update",
133+
{},
134+
package_id=dataset["id"],
135+
id=resource["id"],
136+
url="http://example.com/file3.csv",
137+
schema='https://example.com/schema.json',
138+
validation_status='success',
139+
)
140+
141+
assert func.called # because of the validation_status is `success` and there is a schema
142+
61143
def _pending_task(self, resource_id):
62144
return {
63145
"entity_id": resource_id,

ckanext/xloader/utils.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,17 @@ def awaiting_validation(res_dict):
7676
log.warning('ckanext.xloader.validation.requires_successful_report requires the ckanext-validation plugin to be activated.')
7777
return False
7878

79-
if p.toolkit.asbool(config.get('ckanext.xloader.validation.enforce_schema', True)):
80-
# validation.enforce_schema is turned on, so we will always look for `validation_status`
81-
if res_dict.get('validation_status', None) != 'success':
82-
return True
83-
84-
# validation.enforce_schema is turned off, so if the Resource
85-
# does not have a Validation Schema, we will treat it like
86-
# it does not require Validation.
79+
if p.toolkit.asbool(config.get('ckanext.xloader.validation.enforce_schema', True)) \
80+
and res_dict.get('validation_status', None) != 'success':
81+
# validation.enforce_schema is turned on, and there is no successful report.
82+
return True
83+
84+
elif res_dict.get('schema', None) and res_dict.get('validation_status', None) != 'success':
85+
# validation.enforce_schema is turned off, and there is a Validation Schema and no successful report.
86+
return True
87+
88+
# at this point, we can assume that the Resource is not waiting for Validation.
89+
# or that the Resource does not have a Validation Schema and we are not enforcing schemas.
8790
return False
8891

8992

0 commit comments

Comments
 (0)