Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/api/test_checks.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import pytest | |||||
from swh.deposit.api.checks import check_metadata | from swh.deposit.api.checks import check_metadata | ||||
def test_api_checks_check_metadata_ok(swh_checks_deposit): | @pytest.mark.parametrize( | ||||
actual_check, detail = check_metadata( | "metadata_ok", | ||||
[ | |||||
{ | { | ||||
"url": "something", | "url": "something", | ||||
"external_identifier": "something-else", | "external_identifier": "something-else", | ||||
"name": "foo", | "name": "foo", | ||||
"author": "someone", | "author": "someone", | ||||
} | }, | ||||
) | |||||
assert actual_check is True | |||||
assert detail is None | |||||
def test_api_checks_check_metadata_ok2(swh_checks_deposit): | |||||
actual_check, detail = check_metadata( | |||||
{ | { | ||||
"url": "something", | "url": "some url", | ||||
"external_identifier": "something-else", | "external_identifier": "some id", | ||||
"title": "bar", | "title": "bar", | ||||
"author": "someone", | "author": "no one", | ||||
} | }, | ||||
vlorentz: what is the difference in handling between these two? | |||||
ardumontAuthorUnsubmitted Done Inline Actionseither name or title needs to be there. ardumont: either name or title needs to be there.
The first one provides the name, the other the title. | |||||
vlorentzUnsubmitted Not Done Inline Actionsoh right, I missed that name != title vlorentz: oh right, I missed that name != title | |||||
], | |||||
) | ) | ||||
def test_api_checks_check_metadata_ok(metadata_ok, swh_checks_deposit): | |||||
actual_check, detail = check_metadata(metadata_ok) | |||||
assert actual_check is True | assert actual_check is True | ||||
assert detail is None | assert detail is None | ||||
def test_api_checks_check_metadata_ko(swh_checks_deposit): | @pytest.mark.parametrize( | ||||
"""Missing optional field should be caught | "metadata_ko,expected_summary", | ||||
[ | |||||
""" | ( | ||||
actual_check, error_detail = check_metadata( | |||||
{ | { | ||||
"url": "something", | "url": "something", | ||||
"external_identifier": "something-else", | "external_identifier": "something-else", | ||||
"author": "someone", | "author": "someone", | ||||
} | }, | ||||
) | |||||
expected_error = { | |||||
"metadata": [ | |||||
{ | { | ||||
"summary": "Mandatory alternate fields are missing", | "summary": "Mandatory alternate fields are missing", | ||||
"fields": ["name or title"], | "fields": ["name or title"], | ||||
} | }, | ||||
] | ), | ||||
} | ( | ||||
assert actual_check is False | |||||
assert error_detail == expected_error | |||||
def test_api_checks_check_metadata_ko2(swh_checks_deposit): | |||||
"""Missing mandatory fields should be caught | |||||
""" | |||||
actual_check, error_detail = check_metadata( | |||||
{ | { | ||||
"url": "something", | "url": "something", | ||||
"external_identifier": "something-else", | "external_identifier": "something-else", | ||||
"title": "foobar", | "title": "foobar", | ||||
} | }, | ||||
{"summary": "Mandatory fields are missing", "fields": ["author"],}, | |||||
), | |||||
], | |||||
vlorentzUnsubmitted Not Done Inline Actionsthat's a little hard to read, with the metadata and errors mixed together :/ vlorentz: that's a little hard to read, with the metadata and errors mixed together :/ | |||||
ardumontAuthorUnsubmitted Done Inline ActionsHow to do better though? i'm planning on changing the current checks and adding plenty of tests whose body is the same does not excite me much. ardumont: How to do better though?
i'm planning on changing the current checks and adding plenty of… | |||||
ardumontAuthorUnsubmitted Done Inline Actionsto clarify... ... "and this" does not excite me much to copy/paste those body instructions all around. ardumont: to clarify...
... "and this" does not excite me much to copy/paste those body instructions all… | |||||
vlorentzUnsubmitted Not Done Inline ActionsI don't know. :/ vlorentz: I don't know. :/ | |||||
) | ) | ||||
def test_api_checks_check_metadata_ko( | |||||
metadata_ko, expected_summary, swh_checks_deposit | |||||
): | |||||
"""Missing optional field should be caught | |||||
expected_error = { | """ | ||||
"metadata": [{"summary": "Mandatory fields are missing", "fields": ["author"],}] | actual_check, error_detail = check_metadata(metadata_ko) | ||||
} | |||||
assert actual_check is False | assert actual_check is False | ||||
assert error_detail == expected_error | assert error_detail == {"metadata": [expected_summary]} |
what is the difference in handling between these two?