| 1 |
import re |
| 2 |
|
| 3 |
from typing import Pattern |
| 4 |
|
| 5 |
from pytest import raises |
| 6 |
|
| 7 |
from graphql.language import parse |
| 8 |
from graphql.utilities import build_schema, get_introspection_query |
| 9 |
from graphql.validation import validate |
| 10 |
|
| 11 |
dummy_schema = build_schema(""" |
| 12 |
type Query { |
| 13 |
dummy: String |
| 14 |
} |
| 15 |
""") |
| 16 |
|
| 17 |
|
| 18 |
class ExcpectIntrospectionQuery: |
| 19 |
def __init__(self, **options): |
| 20 |
query = get_introspection_query(**options) |
| 21 |
validation_errors = validate(dummy_schema, parse(query)) |
| 22 |
assert validation_errors == [] |
| 23 |
self.query = query |
| 24 |
|
| 25 |
def to_match(self, name: str, times: int = 1) -> None: |
| 26 |
pattern = self.to_reg_exp(name) |
| 27 |
assert len(pattern.findall(self.query)) == times |
| 28 |
|
| 29 |
def to_not_match(self, name: str) -> None: |
| 30 |
pattern = self.to_reg_exp(name) |
| 31 |
assert not pattern.search(self.query) |
| 32 |
|
| 33 |
def to_contain(self, text: str) -> None: |
| 34 |
assert text in self.query |
| 35 |
|
| 36 |
def to_not_contain(self, text: str) -> None: |
| 37 |
assert text not in self.query |
| 38 |
|
| 39 |
@staticmethod |
| 40 |
def to_reg_exp(name: str) -> Pattern: |
| 41 |
return re.compile(rf"\b{name}\b") |
| 42 |
|
| 43 |
|
| 44 |
def describe_get_introspection_query(): |
| 45 |
def skips_all_description_fields(): |
| 46 |
ExcpectIntrospectionQuery().to_match("description", 5) |
| 47 |
ExcpectIntrospectionQuery(descriptions=True).to_match("description", 5) |
| 48 |
ExcpectIntrospectionQuery(descriptions=False).to_not_match("description") |
| 49 |
|
| 50 |
def includes_is_repeatable_field_on_directives(): |
| 51 |
ExcpectIntrospectionQuery().to_not_match("isRepeatable") |
| 52 |
ExcpectIntrospectionQuery(directive_is_repeatable=True).to_match("isRepeatable") |
| 53 |
ExcpectIntrospectionQuery(directive_is_repeatable=False).to_not_match( |
| 54 |
"isRepeatable" |
| 55 |
) |
| 56 |
|
| 57 |
def includes_description_field_on_schema(): |
| 58 |
ExcpectIntrospectionQuery().to_match("description", 5) |
| 59 |
ExcpectIntrospectionQuery(schema_description=False).to_match("description", 5) |
| 60 |
ExcpectIntrospectionQuery(schema_description=True).to_match("description", 6) |
| 61 |
ExcpectIntrospectionQuery( |
| 62 |
descriptions=False, schema_description=True |
| 63 |
).to_not_match("description") |
| 64 |
|
| 65 |
def includes_specified_by_url_field(): |
| 66 |
ExcpectIntrospectionQuery().to_not_match("specifiedByURL") |
| 67 |
ExcpectIntrospectionQuery(specified_by_url=True).to_match("specifiedByURL") |
| 68 |
ExcpectIntrospectionQuery(specified_by_url=False).to_not_match("specifiedByURL") |
| 69 |
|
| 70 |
def includes_is_deprecated_field_on_input_values(): |
| 71 |
ExcpectIntrospectionQuery().to_match("isDeprecated", 2) |
| 72 |
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match( |
| 73 |
"isDeprecated", 3 |
| 74 |
) |
| 75 |
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match( |
| 76 |
"isDeprecated", 2 |
| 77 |
) |
| 78 |
|
| 79 |
def includes_deprecation_reason_field_on_input_values(): |
| 80 |
ExcpectIntrospectionQuery().to_match("deprecationReason", 2) |
| 81 |
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match( |
| 82 |
"deprecationReason", 3 |
| 83 |
) |
| 84 |
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match( |
| 85 |
"deprecationReason", 2 |
| 86 |
) |
| 87 |
|
| 88 |
def includes_is_deprecated_field_on_directives(): |
| 89 |
ExcpectIntrospectionQuery().to_match("isDeprecated", 2) |
| 90 |
ExcpectIntrospectionQuery(experimental_directive_deprecation=True).to_match( |
| 91 |
"isDeprecated", 3 |
| 92 |
) |
| 93 |
ExcpectIntrospectionQuery(experimental_directive_deprecation=False).to_match( |
| 94 |
"isDeprecated", 2 |
| 95 |
) |
| 96 |
|
| 97 |
def includes_deprecation_reason_field_on_directives(): |
| 98 |
query = ExcpectIntrospectionQuery() |
| 99 |
query.to_not_contain("directives(includeDeprecated: true) {") |
| 100 |
query.to_match("deprecationReason", 2) |
| 101 |
|
| 102 |
query = ExcpectIntrospectionQuery(experimental_directive_deprecation=True) |
| 103 |
query.to_contain("directives(includeDeprecated: true) {") |
| 104 |
query.to_match("deprecationReason", 3) |
| 105 |
|
| 106 |
query = ExcpectIntrospectionQuery(experimental_directive_deprecation=False) |
| 107 |
query.to_not_contain("directives(includeDeprecated: true) {") |
| 108 |
query.to_match("deprecationReason", 2) |
| 109 |
|
| 110 |
def includes_input_object_one_of_field(): |
| 111 |
ExcpectIntrospectionQuery().to_not_match("isOneOf") |
| 112 |
ExcpectIntrospectionQuery(input_object_one_of=True).to_match("isOneOf") |
| 113 |
ExcpectIntrospectionQuery(input_object_one_of=False).to_not_match("isOneOf") |
| 114 |
|
| 115 |
def includes_deprecated_input_field_and_args(): |
| 116 |
ExcpectIntrospectionQuery().to_match("includeDeprecated: true", 2) |
| 117 |
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match( |
| 118 |
"includeDeprecated: true", 5 |
| 119 |
) |
| 120 |
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match( |
| 121 |
"includeDeprecated: true", 2 |
| 122 |
) |
| 123 |
|
| 124 |
def throws_error_if_type_depth_is_too_high(): |
| 125 |
with raises(ValueError) as exc_info: |
| 126 |
get_introspection_query(type_depth=101) |
| 127 |
assert str(exc_info.value) == ( |
| 128 |
"Please set type_depth to a reasonable value" |
| 129 |
" between 0 and 100; the default is 9." |
| 130 |
) |