-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconftest.py
More file actions
83 lines (61 loc) · 2.17 KB
/
conftest.py
File metadata and controls
83 lines (61 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
from dataclasses import dataclass
from typing import Any, Callable, Dict, Union, cast
import pytest
from dotenv import load_dotenv
from gql import Client, gql
from gql.utilities.serialize_variable_values import serialize_variable_values
def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption(
"--server-schema",
action="store",
required=True,
help="The GraphQL server schema file",
)
@pytest.fixture(scope="session")
def server_schema_path(pytestconfig: pytest.Config) -> Union[str, None]:
# the typing on this is weird...
return cast(Union[str, None], pytestconfig.getoption("server_schema"))
@pytest.fixture(scope="session")
def server_schema(server_schema_path: str) -> str:
with open(server_schema_path, "r") as schema_file:
schema_str = schema_file.read()
return schema_str
QueryValidator = Callable[[str, Dict[str, None]], None]
@pytest.fixture(scope="session")
def validate_query(server_schema: str) -> QueryValidator:
"""Returns a validator function which ensures the query and its variables are valid against the server schema."""
gql_client = Client(schema=server_schema)
def validator(query_str: str, variables: Dict[str, Any]) -> None:
assert gql_client.schema is not None
query_doc = gql(query_str)
gql_client.validate(document=query_doc)
serialize_variable_values(
gql_client.schema,
query_doc,
variables,
)
return validator
@dataclass
class Credentials:
"""Credentials used for integration testing."""
host: str
environment_id: int
token: str
@classmethod
def from_env(cls) -> "Credentials":
"""Get test credentials from environment variables.
The following environment variables will be consumed:
- SL_HOST
- SL_TOKEN
- SL_ENV_ID
"""
load_dotenv()
return cls(
host=os.environ["SL_HOST"],
token=os.environ["SL_TOKEN"],
environment_id=int(os.environ["SL_ENV_ID"]),
)
@pytest.fixture(scope="session")
def credentials() -> Credentials:
return Credentials.from_env()