Skip to content

Commit

Permalink
Merge pull request #211 from Materials-Consortia/fix_204_correctly_te…
Browse files Browse the repository at this point in the history
…st_availableapiversion

Update tests for available_api_versions
  • Loading branch information
ml-evs committed Mar 5, 2020
2 parents 7ed738d + 52feec9 commit 8d72b8f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
2 changes: 2 additions & 0 deletions openapi/index_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,14 @@
"title": "Url",
"maxLength": 65536,
"minLength": 1,
"pattern": ".+/v[0-1](\\.[0-9]+)*/?$",
"type": "string",
"description": "a string specifying a versioned base URL that MUST adhere to the rules in section Base URL",
"format": "uri"
},
"version": {
"title": "Version",
"pattern": "^[0-9]+(\\.[0-9]+){,2}$",
"type": "string",
"description": "a string containing the full version number of the API served at that versioned base URL. The version number string MUST NOT be prefixed by, e.g., 'v'."
}
Expand Down
2 changes: 2 additions & 0 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -918,12 +918,14 @@
"title": "Url",
"maxLength": 65536,
"minLength": 1,
"pattern": ".+/v[0-1](\\.[0-9]+)*/?$",
"type": "string",
"description": "a string specifying a versioned base URL that MUST adhere to the rules in section Base URL",
"format": "uri"
},
"version": {
"title": "Version",
"pattern": "^[0-9]+(\\.[0-9]+){,2}$",
"type": "string",
"description": "a string containing the full version number of the API served at that versioned base URL. The version number string MUST NOT be prefixed by, e.g., 'v'."
}
Expand Down
19 changes: 4 additions & 15 deletions optimade/models/baseinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,23 @@ class AvailableApiVersion(BaseModel):
url: AnyHttpUrl = Field(
...,
description="a string specifying a versioned base URL that MUST adhere to the rules in section Base URL",
pattern=r".+/v[0-1](\.[0-9]+)*/?$",
)

version: str = Field(
...,
description="a string containing the full version number of the API served at that versioned base URL. "
"The version number string MUST NOT be prefixed by, e.g., 'v'.",
regex=r"^[0-9]+(\.[0-9]+){,2}$",
)

@validator("url")
def url_must_be_versioned_base_url(cls, v):
"""The URL must be a versioned Base URL"""
if not re.match(r".*/v[0-9]+(\.[0-9]+)*/?", v):
"""The URL must be a valid versioned Base URL"""
if not re.match(r".+/v[0-1](\.[0-9]+)*/?$", v):
raise ValueError(f"url MUST be a versioned base URL. It is: {v}")
return v

@validator("version")
def version_must_be_valid(cls, v):
"""The version number string MUST NOT be prefixed by, e.g., 'v'"""
if not re.match(r"[0-9]+\.[0-9]+(\.[0-9]+)?", v):
raise ValueError(f"version MUST NOT be prefixed by, e.g., 'v'. It is: {v}")
try:
tuple(int(val) for val in v.split("."))
except ValueError:
raise ValueError(f"failed to parse version {v} sections as integers.")

return v

@root_validator(pre=False, skip_on_failure=True)
def crosscheck_url_and_version(cls, values):
""" Check that URL version and API version are compatible. """
Expand All @@ -57,7 +47,6 @@ def crosscheck_url_and_version(cls, values):
raise ValueError(
f"API version {api_version} is not compatible with url version {url_version}."
)

return values


Expand Down
68 changes: 38 additions & 30 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,36 +133,44 @@ def test_bad_references(self):
with self.assertRaises(ValidationError):
ReferenceResource(**ReferenceMapper.map_back(ref))

def test_available_api_versions(self):
bad_urls = [
{"url": "asfdsafhttps://example.com/v0.0", "version": "0.0.0"},
{"url": "https://example.com/optimade", "version": "1.0.0"},
{"url": "https://example.com/v0999", "version": "0999.0.0"},
{"url": "http://example.com/v2.3", "version": "2.3.0"},
]
good_urls = [
{"url": "https://example.com/v0", "version": "0.1.9"},
{"url": "https://example.com/v1.0.2", "version": "1.0.2"},
{"url": "https://example.com/optimade/v1.2", "version": "1.2.3"},
]
bad_combos = [
{"url": "https://example.com/v0", "version": "1.0.0"},
{"url": "https://example.com/v1.0.2", "version": "1.0.3"},
{"url": "https://example.com/optimade/v1.2", "version": "1.3.2"},
]

for data in bad_urls:
with pytest.raises(ValueError) as exc:
AvailableApiVersion(**data)
pytest.fail(f"Url {data['url']} should have failed")
self.assertTrue(
"url MUST be a versioned base URL" in exc.exconly()
or "URL scheme not permitted" in exc.exconly(),
msg=f"Validator 'url_must_be_versioned_base_url' not triggered as it should.\nException message: {exc.exconly()}.\nInputs: {data}",
)

def test_available_api_versions():
bad_urls = [
"asfdsafhttps://example.com/v0.0",
"https://example.com/optimade",
"https://example.com/v0",
"https://example.com/v0999",
]
good_urls = [
{"url": "https://example.com/v0", "version": "0.1.9"},
{"url": "https://example.com/v1.0.2", "version": "1.0.2"},
{"url": "http://example.com/v2.3", "version": "2.3.1"},
]
bad_combos = [
{"url": "https://example.com/v0", "version": "1.0.0"},
{"url": "https://example.com/v1.0.2", "version": "1.0.3"},
{"url": "http://example.com/v2.3", "version": "2.0.1"},
]

for url in bad_urls:
with pytest.raises(ValueError):
AvailableApiVersion(url=url, version="1.0")
pytest.fail(f"Url {url} should have failed")

for data in bad_combos:
with pytest.raises(ValueError):
AvailableApiVersion(**data)
pytest.fail(
f"{data['url']} should have failed with version {data['version']}"
for data in bad_combos:
with pytest.raises(ValueError) as exc:
AvailableApiVersion(**data)
pytest.fail(
f"{data['url']} should have failed with version {data['version']}"
)
self.assertTrue(
"is not compatible with url version" in exc.exconly(),
msg=f"Validator 'crosscheck_url_and_version' not triggered as it should.\nException message: {exc.exconly()}.\nInputs: {data}",
)

for data in good_urls:
AvailableApiVersion(**data)
for data in good_urls:
self.assertIsInstance(AvailableApiVersion(**data), AvailableApiVersion)

0 comments on commit 8d72b8f

Please sign in to comment.