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

Fix /vMAJOR/info in index server #414

Merged
merged 1 commit into from
Jul 17, 2020
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
2 changes: 1 addition & 1 deletion optimade/server/main_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
def add_major_version_base_url(app: FastAPI):
""" Add mandatory endpoints to `/vMAJOR` base URL. """
for endpoint in (index_info, links):
app.include_router(links.router, prefix=BASE_URL_PREFIXES["major"])
app.include_router(endpoint.router, prefix=BASE_URL_PREFIXES["major"])


def add_optional_versioned_base_urls(app: FastAPI):
Expand Down
40 changes: 40 additions & 0 deletions tests/server/test_server_validation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from traceback import print_exc

import pytest

from optimade.validator import ImplementationValidator


Expand Down Expand Up @@ -59,3 +61,41 @@ def test_as_type_with_validator(client):
except Exception:
print_exc()
assert validator.valid


@pytest.mark.parametrize("server", ["regular", "index"])
def test_versioned_base_urls(client, index_client, server: str):
"""Test all expected versioned base URLs responds with 200

This depends on the routers for each kind of server.
"""
try:
import simplejson as json
except ImportError:
import json

from optimade.server.routers.utils import BASE_URL_PREFIXES

clients = {
"regular": client,
"index": index_client,
}

valid_endpoints = {
"regular": ("/info", "/links", "/references", "/structures"),
"index": ("/info", "/links"),
}

for version in BASE_URL_PREFIXES.values():
for endpoint in valid_endpoints[server]:
response = clients[server].get(url=version + endpoint)
json_response = response.json()

assert response.status_code == 200, (
f"Request to {response.url} failed for server {server!r}: "
f"{json.dumps(json_response, indent=2)}"
)
assert "meta" in json_response, (
f"Mandatory 'meta' top-level field not found in request to {response.url} for "
f"server {server!r}. Response: {json.dumps(json_response, indent=2)}"
)
6 changes: 3 additions & 3 deletions tests/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def __init__(
)
if version:
if not version.startswith("v"):
version = f"v{version}"
version = f"/v{version}"
if re.match(r"v[0-9](.[0-9]){0,2}", version) is None:
warnings.warn(
f"Invalid version passed to client: '{version}'. "
f"Will use the default: 'v{__api_version__.split('.')[0]}'"
)
version = f"v{__api_version__.split('.')[0]}"
version = f"/v{__api_version__.split('.')[0]}"
self.version = version

def request( # pylint: disable=too-many-locals
Expand Down Expand Up @@ -77,7 +77,7 @@ def request( # pylint: disable=too-many-locals
):
while url.startswith("/"):
url = url[1:]
url = f"/{self.version}{url}"
url = f"{self.version}/{url}"
return super(OptimadeTestClient, self).request(
method=method,
url=url,
Expand Down