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

Use pytest instead of unittest #390

Merged
merged 9 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 4 additions & 4 deletions optimade/server/entry_collections/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ def _parse_params(

if getattr(params, "sort", False):
sort_spec = []
for elt in params.sort.split(","):
field = elt
for field in params.sort.split(","):
sort_dir = 1
if elt.startswith("-"):
if field.startswith("-"):
field = field[1:]
sort_dir = -1
sort_spec.append((field, sort_dir))
aliased_field = self.resource_mapper.alias_for(field)
sort_spec.append((aliased_field, sort_dir))
cursor_kwargs["sort"] = sort_spec

if getattr(params, "page_offset", False):
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ filterwarnings =
ignore:.*Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated.*:DeprecationWarning
ignore:.*the imp module is deprecated in favour of importlib.*:DeprecationWarning
ignore:.*not found, cannot convert structure.*:UserWarning
testpaths = tests
147 changes: 147 additions & 0 deletions tests/server/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import pytest

from optimade.server.config import CONFIG


@pytest.fixture(scope="session")
def client():
"""Return TestClient for the regular OPTIMADE server"""
from .utils import client_factory

return client_factory()(server="regular")


@pytest.fixture(scope="session")
def index_client():
"""Return TestClient for the index OPTIMADE server"""
from .utils import client_factory

return client_factory()(server="index")


@pytest.fixture(scope="session", params=["regular", "index"])
def both_clients(request):
"""Return TestClient for both the regular and index OPTIMADE server"""
from .utils import client_factory

return client_factory()(server=request.param)


@pytest.fixture
def get_good_response(client, index_client):
"""Get response with some sanity checks, expecting '200 OK'"""

def inner(request: str, server: str = "regular") -> dict:
if server == "regular":
used_client = client
elif server == "index":
used_client = index_client
else:
pytest.fail(
f"Wrong value for 'server': {server}. It must be either 'regular' or 'index'."
)

try:
response = used_client.get(request)
assert response.status_code == 200, f"Request failed: {response.json()}"
response = response.json()
except Exception as exc:
print("Request attempted:")
print(f"{used_client.base_url}{used_client.version}{request}")
raise exc
else:
return response

return inner


@pytest.fixture
def check_response(get_good_response):
"""Fixture to check "good" response"""
from typing import List

def inner(
request: str,
expected_ids: List[str],
page_limit: int = CONFIG.page_limit,
expected_return: int = None,
expected_as_is: bool = False,
server: str = "regular",
):
response = get_good_response(request, server)

response_ids = [struct["id"] for struct in response["data"]]

if expected_return is None:
expected_return = len(expected_ids)

assert response["meta"]["data_returned"] == expected_return

if not expected_as_is:
expected_ids = sorted(expected_ids)

if len(expected_ids) > page_limit:
assert expected_ids[:page_limit] == response_ids
else:
assert expected_ids == response_ids

return inner


@pytest.fixture
def check_error_response(client, index_client):
"""General method for testing expected erroneous response"""

def inner(
request: str,
expected_status: int = None,
expected_title: str = None,
expected_detail: str = None,
server: str = "regular",
):
response = None
if server == "regular":
used_client = client
elif server == "index":
used_client = index_client
else:
pytest.fail(
f"Wrong value for 'server': {server}. It must be either 'regular' or 'index'."
)

try:
response = used_client.get(request)
assert response.status_code == expected_status, (
f"Request should have been an error with status code {expected_status}, "
f"but instead {response.status_code} was received.\nResponse:\n{response.json()}",
)

response = response.json()
assert len(response["errors"]) == 1, response.get(
"errors", "'errors' not found in response"
)
assert response["meta"]["data_returned"] == 0, response.get(
"meta", "'meta' not found in response"
)

error = response["errors"][0]
assert str(expected_status) == error["status"], error
assert expected_title == error["title"], error

if expected_detail is None:
expected_detail = "Error trying to process rule "
CasperWA marked this conversation as resolved.
Show resolved Hide resolved
assert error["detail"].startswith(expected_detail), (
"No expected_detail provided and the error did not start with a standard Lark "
"error message."
)
else:
assert expected_detail == error["detail"], error

except Exception:
print("Request attempted:")
print(f"{used_client.base_url}{used_client.version}{request}")
if response:
print(f"\nCaptured response:\n{response}")
raise

return inner
80 changes: 80 additions & 0 deletions tests/server/query_params/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import pytest


@pytest.fixture
def check_include_response(get_good_response):
"""Fixture to check "good" `include` response"""
from typing import Union, List, Set

def inner(
request: str,
expected_included_types: Union[List, Set],
expected_included_resources: Union[List, Set],
expected_relationship_types: Union[List, Set] = None,
server: str = "regular",
):
response = get_good_response(request, server)

response_data = (
response["data"]
if isinstance(response["data"], list)
else [response["data"]]
)

included_resource_types = list({_["type"] for _ in response["included"]})
assert sorted(expected_included_types) == sorted(included_resource_types), (
f"Expected relationship types: {expected_included_types}. "
f"Does not match relationship types in response's included field: {included_resource_types}",
)

if expected_relationship_types is None:
expected_relationship_types = expected_included_types
relationship_types = set()
for entry in response_data:
relationship_types.update(set(entry.get("relationships", {}).keys()))
assert sorted(expected_relationship_types) == sorted(relationship_types), (
f"Expected relationship types: {expected_relationship_types}. "
f"Does not match relationship types found in response data: {relationship_types}",
)

included_resources = [_["id"] for _ in response["included"]]
assert len(included_resources) == len(expected_included_resources), response[
"included"
]
assert sorted(set(included_resources)) == sorted(expected_included_resources)

return inner


@pytest.fixture
def check_required_fields_response(get_good_response):
"""Fixture to check "good" `required_fields` response"""
from optimade.server import mappers

get_mapper = {
"links": mappers.LinksMapper,
"references": mappers.ReferenceMapper,
"structures": mappers.StructureMapper,
}

def inner(
endpoint: str,
known_unused_fields: set,
expected_fields: set,
server: str = "regular",
):
expected_fields |= (
get_mapper[endpoint].get_required_fields() - known_unused_fields
)
expected_fields.add("attributes")
request = f"/{endpoint}?response_fields={','.join(expected_fields)}"

response = get_good_response(request, server)

response_fields = set()
for entry in response["data"]:
response_fields.update(set(entry.keys()))
response_fields.update(set(entry["attributes"].keys()))
assert sorted(expected_fields) == sorted(response_fields)

return inner