Repository: Materials-Consortia/optimade-python-tools
Pull request #845 (2021-06-09 08:44:33)

Improve semver validation error messsage
Minor fix arising from some validation of AFLOW yesterday (that "1.0" is not a valid semver, and also that semver may not be well known as an abbreviation).

codecov[bot]: # [Codecov](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia) Report
> Merging [#845](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia) (22f6d55) into [master](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/commit/97c0c892cd1011cc2c9119a3370cc83217e23f1e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia) (97c0c89) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845/graphs/tree.svg?width=650&height=150&src=pr&token=UJAtmqkZZO&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia)](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia)

```diff
@@           Coverage Diff           @@
##           master     #845   +/-   ##
=======================================
  Coverage   92.70%   92.70%           
=======================================
  Files          67       67           
  Lines        3699     3699           
=======================================
  Hits         3429     3429           
  Misses        270      270           
```

| Flag | Coverage Δ | |
|---|---|---|
| project | `92.70% <100.00%> (ø)` | |
| validator | `92.70% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia) | Coverage Δ | |
|---|---|---|
| [optimade/models/utils.py](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia#diff-b3B0aW1hZGUvbW9kZWxzL3V0aWxzLnB5) | `91.56% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia). Last update [f80c842...22f6d55](https://codecov.io/gh/Materials-Consortia/optimade-python-tools/pull/845?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Materials-Consortia).


Patch:
diff --git a/optimade/models/utils.py b/optimade/models/utils.py
index 5a47b795..8887656e 100644
--- a/optimade/models/utils.py
+++ b/optimade/models/utils.py
@@ -145,7 +145,10 @@ class SemanticVersion(str):
     @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
 


Test patch:
diff --git a/tests/models/test_baseinfo.py b/tests/models/test_baseinfo.py
index 70ad3bfd..e7230f29 100644
--- a/tests/models/test_baseinfo.py
+++ b/tests/models/test_baseinfo.py
@@ -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"},
@@ -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: