Skip to content

Commit 8308dd6

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

File tree

2 files changed

+92
-5
lines changed

2 files changed

+92
-5
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,19 @@ def awaiting_validation(res_dict):
7575
log.warning('ckanext.xloader.validation.requires_successful_report requires the ckanext-validation plugin to be activated.')
7676
return False
7777

78-
if p.toolkit.asbool(p.toolkit.config.get('ckanext.xloader.validation.enforce_schema', True)):
79-
# validation.enforce_schema is turned on, so we will always look for `validation_status`
78+
if p.toolkit.asbool(config.get('ckanext.xloader.validation.enforce_schema', True)):
79+
# validation.enforce_schema is turned on, explicitly check for the `validation_status`
8080
if res_dict.get('validation_status', None) != 'success':
8181
return True
82+
else:
83+
return False
84+
85+
elif res_dict.get('schema', None) and res_dict.get('validation_status', None) != 'success':
86+
# validation.enforce_schema is turned off, and there is a Validation Schema and no successful report.
87+
return True
8288

83-
# validation.enforce_schema is turned off, so if the Resource
84-
# does not have a Validation Schema, we will treat it like
85-
# it does not require Validation.
89+
# at this point, we can assume that the Resource is not waiting for Validation.
90+
# or that the Resource does not have a Validation Schema and we are not enforcing schemas.
8691
return False
8792

8893

0 commit comments

Comments
 (0)