Skip to content

Commit a8494ad

Browse files
committed
tests updated and extended for json metadata validation
1 parent 0f95c42 commit a8494ad

2 files changed

Lines changed: 84 additions & 33 deletions

File tree

tests/test_services.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ def test_queue_metadata(flask_app, crate_json: dict, profile: str, webhook: str,
197197
"{}",
198198
422, "Required parameter crate_json is empty"
199199
),
200-
]
200+
],
201+
ids=["missing_crate_json","invalid_json","empty_json"]
201202
)
202203
def test_queue_metadata_json_errors(flask_app, crate_json: str, status_code: int, response_error: str):
203204
response, status = queue_ro_crate_metadata_validation_task(crate_json)

tests/test_validation_tasks.py

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from unittest import mock
22
import pytest
3+
import json
34

45
from app.tasks.validation_tasks import (
56
process_validation_task_by_id,
67
perform_ro_crate_validation,
8+
perform_metadata_validation,
79
return_ro_crate_validation,
810
process_validation_task_by_metadata,
911
check_ro_crate_exists,
@@ -227,34 +229,28 @@ def test_process_validation_failure(
227229
# Test function: process_validation_task_by_metadata
228230

229231
@pytest.mark.parametrize(
230-
"crate_json, profile_name, webhook_url, mock_path, validation_json, validation_value, os_path_exists",
232+
"crate_json, profile_name, webhook_url, validation_json, validation_value",
231233
[
232234
(
233235
'{"@context": "https://w3id.org/ro/crate/1.1/context", "@graph": []}',
234-
"test-profile", "https://example.com/webhook", "/tmp/crate",
235-
'{"status": "valid"}', False, True
236+
"test-profile", "https://example.com/webhook",
237+
'{"status": "valid"}', False
236238
),
237239
(
238240
'{"@context": "https://w3id.org/ro/crate/1.1/context", "@graph": []}',
239-
"test-profile", "https://example.com/webhook", "/tmp/crate",
240-
'{"status": "invalid"}', True, True
241+
"test-profile", "https://example.com/webhook",
242+
'{"status": "invalid"}', True
241243
)
242244
],
243245
ids=["success_no_issues", "success_with_issues"]
244246
)
245-
@mock.patch("app.tasks.validation_tasks.shutil.rmtree")
246-
@mock.patch("app.tasks.validation_tasks.os.path.exists")
247247
@mock.patch("app.tasks.validation_tasks.send_webhook_notification")
248-
@mock.patch("app.tasks.validation_tasks.perform_ro_crate_validation")
249-
@mock.patch("app.tasks.validation_tasks.build_metadata_only_rocrate")
248+
@mock.patch("app.tasks.validation_tasks.perform_metadata_validation")
250249
def test_metadata_validation(
251-
mock_build, mock_validate, mock_webhook, mock_exists, mock_rmtree,
252-
crate_json: str, profile_name: str, webhook_url: str, mock_path: str,
253-
validation_json: str, validation_value: bool, os_path_exists: bool
250+
mock_validate, mock_webhook,
251+
crate_json: str, profile_name: str, webhook_url: str,
252+
validation_json: str, validation_value: bool,
254253
):
255-
mock_exists.return_value = os_path_exists
256-
mock_build.return_value = mock_path
257-
258254
mock_result = mock.Mock()
259255
mock_result.has_issues.return_value = validation_value
260256
mock_result.to_json.return_value = validation_json
@@ -263,39 +259,33 @@ def test_metadata_validation(
263259
result = process_validation_task_by_metadata(crate_json, profile_name, webhook_url)
264260

265261
assert result == validation_json
266-
mock_build.assert_called_once_with(crate_json)
267262
mock_validate.assert_called_once()
268263
mock_webhook.assert_called_once_with(webhook_url, validation_json)
269-
mock_rmtree.assert_called_once_with(mock_path)
270264

271265

272266
@pytest.mark.parametrize(
273-
"crate_json, profile_name, webhook_url, mock_path, validation_message, os_path_exists",
267+
"crate_json, profile_name, webhook_url, validation_message",
274268
[
275269
(
276270
'{"@context": "https://w3id.org/ro/crate/1.1/context", "@graph": []}',
277-
"test-profile", "https://example.com/webhook", "/tmp/crate",
278-
"Validation error", True
271+
"test-profile", "https://example.com/webhook",
272+
"Validation error"
279273
),
280274
(
281275
'{"@context": "https://w3id.org/ro/crate/1.1/context", "@graph": []}',
282-
"test-profile", None, "/tmp/crate",
283-
"Validation error", True
276+
"test-profile", None,
277+
"Validation error"
284278
)
285279
],
286280
ids=["validation_fails", "validation_fails_no_webhook"]
287281
)
288-
@mock.patch("app.tasks.validation_tasks.shutil.rmtree")
289-
@mock.patch("app.tasks.validation_tasks.os.path.exists", return_value=True)
290282
@mock.patch("app.tasks.validation_tasks.send_webhook_notification")
291-
@mock.patch("app.tasks.validation_tasks.perform_ro_crate_validation")
292-
@mock.patch("app.tasks.validation_tasks.build_metadata_only_rocrate")
283+
@mock.patch("app.tasks.validation_tasks.perform_metadata_validation")
293284
def test_validation_fails_and_sends_error_notification_to_webhook(
294-
mock_build, mock_validate, mock_webhook, mock_exists, mock_rmtree,
295-
crate_json: str, profile_name: str, webhook_url: str, mock_path: str,
296-
validation_message: str, os_path_exists: bool
285+
mock_validate, mock_webhook,
286+
crate_json: str, profile_name: str, webhook_url: str,
287+
validation_message: str
297288
):
298-
mock_build.return_value = mock_path
299289

300290
mock_validate.return_value = validation_message
301291

@@ -313,8 +303,6 @@ def test_validation_fails_and_sends_error_notification_to_webhook(
313303
# Make sure webhook not sent
314304
mock_webhook.assert_not_called()
315305

316-
mock_rmtree.assert_called_once_with(mock_path)
317-
318306

319307
# Test function: perform_ro_crate_validation
320308

@@ -378,6 +366,68 @@ def test_validation_settings_error(mock_validation_settings, mock_validate):
378366
mock_validate.assert_not_called()
379367

380368

369+
# Test function: perform_metadata_validation
370+
371+
@pytest.mark.parametrize(
372+
"crate_json, profile_name, skip_checks",
373+
[
374+
('{"id":"dummy json"}', "ro_profile", ["check1", "check2"]),
375+
('{"id":"dummy json"}', None, None)
376+
],
377+
ids=["success_with_all_args", "success_with_only_crate"]
378+
)
379+
@mock.patch("app.tasks.validation_tasks.services.validate")
380+
@mock.patch("app.tasks.validation_tasks.services.ValidationSettings")
381+
def test_metadata_validation_success_with_all_args(
382+
mock_validation_settings, mock_validate,
383+
crate_json: str, profile_name: str, skip_checks: list
384+
):
385+
mock_result = mock.Mock()
386+
mock_validate.return_value = mock_result
387+
388+
result = perform_metadata_validation(crate_json, profile_name, skip_checks)
389+
390+
# Assert that result was returned
391+
assert result == mock_result
392+
393+
# Validate proper construction of ValidationSettings
394+
mock_validation_settings.assert_called_once()
395+
args, kwargs = mock_validation_settings.call_args
396+
assert kwargs["metadata_dict"] == json.loads(crate_json)
397+
if profile_name is not None:
398+
assert kwargs["profile_identifier"] == profile_name
399+
else:
400+
assert "profile_identifier" not in kwargs
401+
if skip_checks is not None:
402+
assert kwargs["skip_checks"] == skip_checks
403+
else:
404+
assert "skip_checks" not in kwargs
405+
406+
mock_validate.assert_called_once_with(mock_validation_settings.return_value)
407+
408+
409+
@mock.patch("app.tasks.validation_tasks.services.validate", side_effect=RuntimeError("Validation error"))
410+
@mock.patch("app.tasks.validation_tasks.services.ValidationSettings")
411+
def test_metadata_validation_raises_exception_and_returns_string(mock_validation_settings, mock_validate):
412+
crate_json = '{"id":"test metadata"}'
413+
result = perform_metadata_validation(crate_json, "profile", skip_checks_list=None)
414+
415+
assert isinstance(result, str)
416+
assert "Validation error" in result
417+
mock_validate.assert_called_once()
418+
419+
420+
@mock.patch("app.tasks.validation_tasks.services.validate")
421+
@mock.patch("app.tasks.validation_tasks.services.ValidationSettings", side_effect=ValueError("Bad config"))
422+
def test_metadata_validation_settings_error(mock_validation_settings, mock_validate):
423+
crate_json = '{"id":"test metadata"}'
424+
result = perform_metadata_validation(crate_json, None)
425+
426+
assert isinstance(result, str)
427+
assert "Bad config" in result
428+
mock_validate.assert_not_called()
429+
430+
381431
# Test function: return_ro_crate_validation
382432

383433
@mock.patch("app.tasks.validation_tasks.get_validation_status_from_minio")

0 commit comments

Comments
 (0)