|
1 | 1 | import re
|
2 | 2 |
|
| 3 | +from typing import Pattern |
| 4 | + |
3 | 5 | from graphql.utilities import get_introspection_query
|
4 | 6 |
|
5 | 7 |
|
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) |
9 | 11 |
|
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 |
11 | 15 |
|
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) |
13 | 19 |
|
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") |
18 | 23 |
|
19 |
| - assert not has_repeatability(get_introspection_query()) |
20 | 24 |
|
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") |
22 | 30 |
|
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" |
25 | 36 | )
|
26 | 37 |
|
27 | 38 | 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") |
44 | 45 |
|
45 | 46 | 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