def test_identifier_required_no_value(): schema = IdentifierSchema(allowed_schemes=dummy_allowed_schemes) with pytest.raises(ValidationError) as e: schema.load({}) errors = e.value.normalized_messages() assert errors == {'identifier': ['Missing data for required field.']}
def test_autoschema_not_recognized(): invalid_identifier = { "identifier": "0000-0000-0000-00000000", } schema = IdentifierSchema(allowed_schemes=["orcid"]) with pytest.raises(ValidationError) as excinfo: schema.load(invalid_identifier)
def test_only_unknown_identifier_should_fail(): only_unknown_identifier = { "identifier": "foobar", } schema = IdentifierSchema() with pytest.raises(ValidationError): schema.load(only_unknown_identifier)
def test_detected_and_allowed_scheme_second_detected(): # 8 allowed_schemes = {"isni": {"label": "ISNI", "validator": idutils.is_isni}} schema = IdentifierSchema(allowed_schemes=allowed_schemes) valid_isni = {"identifier": "0000-0001-6759-6273"} data = schema.load(valid_isni) valid_isni["scheme"] = "isni" assert data == valid_isni
def test_detected_and_allowed_scheme_valid_value(): # 5 allowed_schemes = {"doi": {"label": "DOI", "validator": idutils.is_doi}} schema = IdentifierSchema(allowed_schemes=allowed_schemes) valid_doi = {"identifier": "10.12345/foo.bar"} data = schema.load(valid_doi) valid_doi["scheme"] = "doi" assert data == valid_doi
def test_identifier_required_empty_value(): schema = IdentifierSchema(allowed_schemes=dummy_allowed_schemes) empty_identifier = {"identifier": "", "scheme": "dummy"} with pytest.raises(ValidationError) as e: schema.load(empty_identifier) errors = e.value.normalized_messages() assert errors == {'identifier': 'Missing data for required field.'}
def test_valid_scheme_identifier_other_forbidden_should_pass(): valid_scheme_identifier = { "identifier": "0000-0001-6759-6273", "scheme": "orcid", } schema = IdentifierSchema(forbidden_schemes=["isbn"]) data = schema.load(valid_scheme_identifier) assert valid_scheme_identifier == data == schema.dump(data)
def test_valid_scheme_identifier_allowed_should_pass(): valid_scheme_identifier = { "identifier": "0000-0001-6759-6273", "scheme": "orcid", } schema = IdentifierSchema(allowed_schemes=["orcid"]) data = schema.load(valid_scheme_identifier) assert valid_scheme_identifier == data == schema.dump(data)
def test_valid_scheme_identifier_forbidden_should_fail(): valid_scheme_identifier = { "identifier": "0000-0001-6759-6273", "scheme": "orcid", } schema = IdentifierSchema(forbidden_schemes=["orcid"]) with pytest.raises(ValidationError): schema.load(valid_scheme_identifier)
def test_identifier_not_provided(): invalid_no_identifier = { "scheme": "orcid" } schema = IdentifierSchema(allowed_schemes=["orcid"]) with pytest.raises(ValidationError) as excinfo: schema.load(invalid_no_identifier)
def test_given_and_allowed_scheme_invalid_value(): # 1 allowed_schemes = {"doi": {"label": "DOI", "validator": idutils.is_doi}} schema = IdentifierSchema(allowed_schemes=allowed_schemes) invalid_doi = {"scheme": "doi", "identifier": "12345"} with pytest.raises(ValidationError) as e: schema.load(invalid_doi) errors = e.value.normalized_messages() assert errors == {'identifier': ['Invalid DOI identifier.']}
def test_autoschema_not_allowed(): invalid_identifier = { "identifier": "0000-0001-6759-6273", "scheme": "orcid" } schema = IdentifierSchema(allowed_schemes=["ror"]) with pytest.raises(ValidationError): schema.load(invalid_identifier)
def test_invalid_identifier(): invalid_blank_identifier = { "identifier": "inv", "scheme": "orcid" } schema = IdentifierSchema(allowed_schemes=["orcid"]) with pytest.raises(ValidationError) as excinfo: schema.load(invalid_blank_identifier)
def test_invalid_scheme_or_format(): invalid_identifier = { "identifier": "0000-0000-0000-00000000", "scheme": "provided-scheme" } schema = IdentifierSchema(allowed_schemes=["provided-scheme"]) with pytest.raises(ValidationError) as excinfo: loaded = schema.load(invalid_identifier)
def test_scheme_and_identifier_no_match_should_pass(): wrong_scheme = {"identifier": "0000-0001-6759-6273", "scheme": "isbn"} schema = IdentifierSchema() data = schema.load(wrong_scheme) correct_scheme = {"scheme": "orcid"} correct_scheme.update(wrong_scheme) assert correct_scheme == data == schema.dump(data)
def test_not_given_not_detected_scheme_for_identifier(): # 10 allowed_schemes = {"isni": {"label": "ISNI", "validator": idutils.is_isni}} schema = IdentifierSchema(allowed_schemes=allowed_schemes) invalid_no_scheme = {"identifier": "00:11:22:33"} with pytest.raises(ValidationError) as e: schema.load(invalid_no_scheme) errors = e.value.normalized_messages() assert errors == {'scheme': ['Invalid scheme for identifier 00:11:22:33.']}
def test_only_identifier_should_pass(): only_identifier = { "identifier": "0000-0001-6759-6273", } schema = IdentifierSchema() data = schema.load(only_identifier) with_scheme = {"scheme": "orcid"} with_scheme.update(only_identifier) assert with_scheme == data == schema.dump(data)
def test_scheme_and_identifier_match_should_pass(): valid_scheme_identifier = { "identifier": "0000-0001-6759-6273", "scheme": "orcid", } schema = IdentifierSchema() data = schema.load(valid_scheme_identifier) # NOTE: Since the schemas return the dict itself, the loaded object # is the same than the input and dumped objects (dicts) assert valid_scheme_identifier == data == schema.dump(data)
def validate_pids(self, value): """Validates the keys of the pids are supported providers.""" for scheme, pid_attrs in value.items(): # The required flag applies to the identifier value # It won't fail for empty allowing the components to reserve one id_schema = IdentifierSchema( fail_on_unknown=False, identifier_required=True) id_schema.load({ "scheme": scheme, "identifier": pid_attrs.get("identifier") })
def test_identifier_auto_scheme(): valid_identifier = { "identifier": "0000-0001-6759-6273", } schema = IdentifierSchema(allowed_schemes=["orcid"]) loaded = schema.load(valid_identifier) # NOTE: Since the schemas return the dict itself, the loaded object # is the same than the input and dumped objects (dicts) valid_identifier["scheme"] == "orcid" assert valid_identifier == loaded == schema.dump(loaded)
def test_full_identifier(): valid_full = { "identifier": "0000-0001-6759-6273", "scheme": "orcid" } schema = IdentifierSchema(allowed_schemes=["orcid", "ror"]) loaded = schema.load(valid_full) # NOTE: Since the schemas return the dict itself, the loaded object # is the same than the input and dumped objects (dicts) assert valid_full == loaded == schema.dump(loaded)
def test_autoschema_allow_all_provided(): valid_identifier = { "identifier": "0000-0001-6759-6273", "scheme": "isni" } schema = IdentifierSchema(allow_all=True) loaded = schema.load(valid_identifier) # NOTE: Since the schemas return the dict itself, the loaded object # is the same than the input and dumped objects (dicts) assert valid_identifier == loaded == schema.dump(loaded)
def test_identifier_not_required_only_scheme(): schema = IdentifierSchema( allowed_schemes=dummy_allowed_schemes, identifier_required=False, ) only_scheme = {"scheme": "dummy"} with pytest.raises(ValidationError) as e: schema.load(only_scheme) errors = e.value.normalized_messages() assert errors == {'identifier': 'Missing data for required field.'}
def test_invalid_scheme_detected_identifier_allowed_should_pass(): invalid_scheme_identifier = { "identifier": "0000-0001-6759-6273", "scheme": "bar", } schema = IdentifierSchema(allowed_schemes=["isbn", "orcid"]) data = schema.load(invalid_scheme_identifier) with_scheme = {"scheme": "orcid"} with_scheme.update(invalid_scheme_identifier) assert with_scheme == data == schema.dump(data)
def test_given_custom_and_allowed_scheme_valid_value(): # 4 allowed_schemes = { "other": {"label": "Other", "validator": validate_other} } schema = IdentifierSchema(allowed_schemes=allowed_schemes) valid_other = { "scheme": "other", "identifier": "12345" } data = schema.load(valid_other) assert data == valid_other
def test_detected_and_allowed_scheme_respect_detection_order(): # 8 allowed_schemes = { "orcid": {"label": "ORCID", "validator": idutils.is_orcid}, "isni": {"label": "ISNI", "validator": idutils.is_isni} } schema = IdentifierSchema(allowed_schemes=allowed_schemes) valid_orcid = { "identifier": "0000-0001-6759-6273" } data = schema.load(valid_orcid) valid_orcid["scheme"] = "orcid" assert data == valid_orcid
def test_given_and_not_allowed_scheme_valid_value(): # 2 allowed_schemes = { "other": {"label": "Other", "validator": validate_other} } schema = IdentifierSchema(allowed_schemes=allowed_schemes) valid_doi = { "scheme": "doi", "identifier": "10.12345/foo.bar" } with pytest.raises(ValidationError) as e: schema.load(valid_doi) errors = e.value.normalized_messages() assert errors == {'scheme': 'Invalid scheme.'}
def test_given_custom_and_allowed_scheme_invalid_value(): # 4 allowed_schemes = { "other": { "label": "Other", "validator": validate_other } } schema = IdentifierSchema(allowed_schemes=allowed_schemes) invalid_other = {"scheme": "other", "identifier": "12345abc"} with pytest.raises(ValidationError) as e: schema.load(invalid_other) errors = e.value.normalized_messages() assert errors == {'identifier': ['Invalid Other identifier.']}
def test_detected_and_not_allowed_scheme_valid_value(): # 6 allowed_schemes = { "other": {"label": "Other", "validator": validate_other} } schema = IdentifierSchema(allowed_schemes=allowed_schemes) valid_doi = { "identifier": "10.12345/foo.bar" } with pytest.raises(ValidationError) as e: schema.load(valid_doi) errors = e.value.normalized_messages() assert errors == { 'scheme': 'Missing data for required field.' }
def validate_pids(self, value): """Validates the keys of the pids are supported providers.""" error_messages = [] for scheme, pid_attrs in value.items(): # The required flag applies to the identifier value # It won't fail for empty allowing the components to reserve one id_schema = IdentifierSchema(fail_on_unknown=True, identifier_required=True) try: id_schema.load({ "scheme": scheme, "identifier": pid_attrs.get("identifier") }) except ValidationError: # cannot raise in case more than one pid presents errors error_messages.append(_(f"Invalid value for scheme {scheme}")) if error_messages: raise ValidationError(message=error_messages)