@pytest.mark.parametrize(
"allowed_pid_types,generate_pid_on_publish,persistent_identifier,error",
[
(["external"], None, None, "has to have a persistent identifier"),
(["external"], None, "external-pid", None),
(["external"], None, "draft:temp-pid", "Cannot assign draft PID."),
(["external"], "URN", None, "catalog does not allow PID generation"),
(["external"], "URN", "external-pid", "using generate_pid_on_publish"),
(["URN"], None, None, "Value is required by the catalog when publishing"),
(["URN"], "URN", None, None),
(["URN"], "DOI", None, "is not a valid choice for catalog"),
(["URN"], "DOI", "external-pid", "using generate_pid_on_publish"),
(["URN", "DOI"], "DOI", None, None),
(["URN"], None, "external-pid", "user-defined PID is not supported by the catalog"),
(["URN", "external"], "URN", None, None),
(["URN", "external"], None, "external-pid", None),
(["URN", "external"], "URN", "external-pid", "using generate_pid_on_publish"),
],
)
def test_dataset_allowed_pid_types(
allowed_pid_types, generate_pid_on_publish, persistent_identifier, error
):
"""The data catalog determines which PID types are supported."""
catalog = factories.DataCatalogFactory(allowed_pid_types=allowed_pid_types)
dataset = factories.DatasetFactory(data_catalog=catalog)
dataset.actors.set([factories.DatasetActorFactory(roles=["creator", "publisher"])])
dataset.generate_pid_on_publish = generate_pid_on_publish
exc = None
with contextlib.ExitStack() as stack:
if error:
exc = stack.enter_context(pytest.raises(ValidationError))
DatasetSerializer.validate_new_pid(dataset=dataset, new_pid=persistent_identifier)
dataset.persistent_identifier = persistent_identifier
dataset.save()
dataset.publish()
if error:
assert error in str(exc.value)