Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve semver validation error messsage #845

Merged
merged 2 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion optimade/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def __modify_schema__(cls, field_schema):
@classmethod
def validate(cls, v: str):
if not cls.regex.match(v):
raise ValueError(f"Unable to validate version {v} as a semver.")
raise ValueError(
f"Unable to validate the version string {v!r} as a semantic version (expected <major>.<minor>.<patch>)."
"See https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string for more information."
)

return v

Expand Down
5 changes: 4 additions & 1 deletion tests/models/test_baseinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def test_available_api_versions():
]
bad_versions = [
{"url": "https://example.com/v0", "version": "v0.1.9"},
{"url": "https://example.com/v0", "version": "0.1"},
{"url": "https://example.com/v1", "version": "1.0"},
{"url": "https://example.com/v1.0.2", "version": "v1.0.2"},
{"url": "https://example.com/optimade/v1.2", "version": "v1.2.3"},
{"url": "https://example.com/v1.0.0", "version": "1.asdfaf.0-rc55"},
Expand Down Expand Up @@ -48,7 +50,8 @@ def test_available_api_versions():
with pytest.raises(ValueError) as exc:
AvailableApiVersion(**data)
assert (
f"Unable to validate version {data['version']} as a semver" in exc.exconly()
f"Unable to validate the version string {data['version']!r} as a semantic version (expected <major>.<minor>.<patch>)"
in exc.exconly()
), f"SemVer validator not triggered as it should.\nException message: {exc.exconly()}.\nInputs: {data}"

for data in bad_combos:
Expand Down