Skip to content

Commit 385dbb7

Browse files
committed
tests: update get_introspection_query tests to use custom matchers
Replicates graphql/graphql-js@ce03dab
1 parent 7af97e2 commit 385dbb7

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed
Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,49 @@
11
import re
22

3+
from typing import Pattern
4+
35
from graphql.utilities import get_introspection_query
46

57

6-
def describe_get_introspection_query():
7-
def skips_all_description_fields():
8-
has_descriptions = re.compile(r"\bdescription\b").search
8+
class ExcpectIntrospectionQuery:
9+
def __init__(self, **options):
10+
self.query = get_introspection_query(**options)
911

10-
assert has_descriptions(get_introspection_query())
12+
def to_match(self, name: str, times: int = 1) -> None:
13+
pattern = self.to_reg_exp(name)
14+
assert len(pattern.findall(self.query)) == times
1115

12-
assert has_descriptions(get_introspection_query(descriptions=True))
16+
def to_not_match(self, name: str) -> None:
17+
pattern = self.to_reg_exp(name)
18+
assert not pattern.search(self.query)
1319

14-
assert not has_descriptions(get_introspection_query(descriptions=False))
15-
16-
def includes_is_repeatable_field_on_directives():
17-
has_repeatability = re.compile(r"\bisRepeatable\b").search
20+
@staticmethod
21+
def to_reg_exp(name: str) -> Pattern:
22+
return re.compile(fr"\b{name}\b")
1823

19-
assert not has_repeatability(get_introspection_query())
2024

21-
assert has_repeatability(get_introspection_query(directive_is_repeatable=True))
25+
def describe_get_introspection_query():
26+
def skips_all_description_fields():
27+
ExcpectIntrospectionQuery().to_match("description", 5)
28+
ExcpectIntrospectionQuery(descriptions=True).to_match("description", 5)
29+
ExcpectIntrospectionQuery(descriptions=False).to_not_match("description")
2230

23-
assert not has_repeatability(
24-
get_introspection_query(directive_is_repeatable=False)
31+
def includes_is_repeatable_field_on_directives():
32+
ExcpectIntrospectionQuery().to_not_match("isRepeatable")
33+
ExcpectIntrospectionQuery(directive_is_repeatable=True).to_match("isRepeatable")
34+
ExcpectIntrospectionQuery(directive_is_repeatable=False).to_not_match(
35+
"isRepeatable"
2536
)
2637

2738
def includes_description_field_on_schema():
28-
all_descriptions = re.compile(r"\bdescription\b").findall
29-
30-
assert len(all_descriptions(get_introspection_query())) == 5
31-
32-
assert (
33-
len(all_descriptions(get_introspection_query(schema_description=False)))
34-
== 5
35-
)
36-
37-
assert (
38-
len(all_descriptions(get_introspection_query(schema_description=True))) == 6
39-
)
40-
41-
assert not all_descriptions(
42-
get_introspection_query(descriptions=False, schema_description=True)
43-
)
39+
ExcpectIntrospectionQuery().to_match("description", 5)
40+
ExcpectIntrospectionQuery(schema_description=False).to_match("description", 5)
41+
ExcpectIntrospectionQuery(schema_description=True).to_match("description", 6)
42+
ExcpectIntrospectionQuery(
43+
descriptions=False, schema_description=True
44+
).to_not_match("description")
4445

4546
def includes_specified_by_url_field():
46-
all_specified_by_urls = re.compile(r"\bspecifiedByUrl\b").findall
47-
48-
assert not all_specified_by_urls(get_introspection_query())
49-
50-
assert not all_specified_by_urls(
51-
get_introspection_query(specified_by_url=False)
52-
)
53-
54-
assert (
55-
len(all_specified_by_urls(get_introspection_query(specified_by_url=True)))
56-
== 1
57-
)
47+
ExcpectIntrospectionQuery().to_not_match("specifiedByUrl")
48+
ExcpectIntrospectionQuery(specified_by_url=True).to_match("specifiedByUrl")
49+
ExcpectIntrospectionQuery(specified_by_url=False).to_not_match("specifiedByUrl")

0 commit comments

Comments
 (0)