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 84 85 86 87 88 89
|
import re
from typing import Pattern
from graphql.language import parse
from graphql.utilities import build_schema, get_introspection_query
from graphql.validation import validate
dummy_schema = build_schema(
"""
type Query {
dummy: String
}
"""
)
class ExcpectIntrospectionQuery:
def __init__(self, **options):
query = get_introspection_query(**options)
validation_errors = validate(dummy_schema, parse(query))
assert validation_errors == []
self.query = query
def to_match(self, name: str, times: int = 1) -> None:
pattern = self.to_reg_exp(name)
assert len(pattern.findall(self.query)) == times
def to_not_match(self, name: str) -> None:
pattern = self.to_reg_exp(name)
assert not pattern.search(self.query)
@staticmethod
def to_reg_exp(name: str) -> Pattern:
return re.compile(rf"\b{name}\b")
def describe_get_introspection_query():
def skips_all_description_fields():
ExcpectIntrospectionQuery().to_match("description", 5)
ExcpectIntrospectionQuery(descriptions=True).to_match("description", 5)
ExcpectIntrospectionQuery(descriptions=False).to_not_match("description")
def includes_is_repeatable_field_on_directives():
ExcpectIntrospectionQuery().to_not_match("isRepeatable")
ExcpectIntrospectionQuery(directive_is_repeatable=True).to_match("isRepeatable")
ExcpectIntrospectionQuery(directive_is_repeatable=False).to_not_match(
"isRepeatable"
)
def includes_description_field_on_schema():
ExcpectIntrospectionQuery().to_match("description", 5)
ExcpectIntrospectionQuery(schema_description=False).to_match("description", 5)
ExcpectIntrospectionQuery(schema_description=True).to_match("description", 6)
ExcpectIntrospectionQuery(
descriptions=False, schema_description=True
).to_not_match("description")
def includes_specified_by_url_field():
ExcpectIntrospectionQuery().to_not_match("specifiedByURL")
ExcpectIntrospectionQuery(specified_by_url=True).to_match("specifiedByURL")
ExcpectIntrospectionQuery(specified_by_url=False).to_not_match("specifiedByURL")
def includes_is_deprecated_field_on_input_values():
ExcpectIntrospectionQuery().to_match("isDeprecated", 2)
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match(
"isDeprecated", 3
)
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match(
"isDeprecated", 2
)
def includes_deprecation_reason_field_on_input_values():
ExcpectIntrospectionQuery().to_match("deprecationReason", 2)
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match(
"deprecationReason", 3
)
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match(
"deprecationReason", 2
)
def includes_deprecated_input_field_and_args():
ExcpectIntrospectionQuery().to_match("includeDeprecated: true", 2)
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match(
"includeDeprecated: true", 5
)
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match(
"includeDeprecated: true", 2
)
|