|
1 | 1 | import json |
| 2 | +import pathlib |
2 | 3 |
|
| 4 | +import attr |
| 5 | +import graphql |
3 | 6 | import pytest |
4 | 7 | from hypothesis import HealthCheck, Phase, Verbosity, given, settings |
5 | 8 | from hypothesis import strategies as st |
6 | 9 |
|
| 10 | +from hypothesis_graphql import nodes |
7 | 11 | from hypothesis_graphql import strategies as gql_st |
| 12 | +from hypothesis_graphql._strategies.strategy import BUILT_IN_SCALAR_TYPE_NAMES |
| 13 | +from hypothesis_graphql.cache import cached_build_schema |
8 | 14 |
|
9 | | -with open("test/corpus-api-guru-catalog.json") as fd: |
| 15 | +HERE = pathlib.Path(__file__).parent |
| 16 | + |
| 17 | +with open(HERE / "corpus-api-guru-catalog.json") as fd: |
10 | 18 | schemas = json.load(fd) |
11 | 19 |
|
12 | 20 | INVALID_SCHEMAS = { |
13 | 21 | # Error: The directive '@deprecated' can only be used once at this location |
14 | 22 | "Gitlab", |
15 | 23 | } |
16 | | -SCHEMAS_WITH_CUSTOM_SCALARS = { |
17 | | - "MongoDB Northwind demo", |
18 | | - "Bitquery", |
19 | | - "MusicBrainz", |
20 | | - "Spacex Land", |
21 | | - "TravelgateX", |
22 | | - "HIVDB", |
23 | | - "Contentful", |
24 | | - "Universe", |
25 | | -} |
| 24 | + |
| 25 | + |
| 26 | +@attr.s(slots=True) |
| 27 | +class Schema: |
| 28 | + raw = attr.ib() |
| 29 | + custom_scalars = attr.ib() |
| 30 | + |
| 31 | + |
| 32 | +PLACEHOLDER_STRATEGY = st.just("placeholder").map(nodes.String) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def schema(request): |
| 37 | + raw_schema = schemas[request.param] |
| 38 | + parsed = cached_build_schema(raw_schema) |
| 39 | + custom_scalars = {} |
| 40 | + # Put placeholders for every custom scalar. Their value is pretty much irrelevant here, it is more important to |
| 41 | + # test query generation, therefore a placeholder will allow these tests to run on schemas that contain custom |
| 42 | + # scalars |
| 43 | + for name, type_ in parsed.type_map.items(): |
| 44 | + if name not in BUILT_IN_SCALAR_TYPE_NAMES and isinstance(type_, graphql.GraphQLScalarType): |
| 45 | + custom_scalars[name] = PLACEHOLDER_STRATEGY |
| 46 | + return Schema(raw_schema, custom_scalars or None) |
26 | 47 |
|
27 | 48 |
|
28 | 49 | def get_names(corpus, predicate=None): |
29 | 50 | for name in sorted(corpus): |
30 | 51 | if name in INVALID_SCHEMAS or (predicate and not predicate(name)): |
31 | 52 | continue |
32 | | - if name in SCHEMAS_WITH_CUSTOM_SCALARS: |
33 | | - yield pytest.param(name, marks=pytest.mark.xfail(reason="Custom scalars are not supported")) |
34 | | - else: |
35 | | - yield name |
| 53 | + yield name |
36 | 54 |
|
37 | 55 |
|
38 | 56 | CORPUS_SETTINGS = { |
39 | | - "suppress_health_check": [HealthCheck.too_slow, HealthCheck.data_too_large, HealthCheck.filter_too_much], |
| 57 | + "suppress_health_check": [ |
| 58 | + HealthCheck.too_slow, |
| 59 | + HealthCheck.data_too_large, |
| 60 | + HealthCheck.filter_too_much, |
| 61 | + HealthCheck.function_scoped_fixture, |
| 62 | + ], |
40 | 63 | "phases": [Phase.generate], |
41 | 64 | "verbosity": Verbosity.quiet, |
42 | 65 | "deadline": None, |
43 | | - "max_examples": 5, |
| 66 | + "max_examples": 10, |
44 | 67 | } |
45 | 68 |
|
46 | 69 |
|
47 | | -@pytest.mark.parametrize("name", get_names(schemas)) |
| 70 | +@pytest.mark.parametrize("schema", get_names(schemas), indirect=["schema"]) |
48 | 71 | @settings(**CORPUS_SETTINGS) |
49 | 72 | @given(data=st.data()) |
50 | | -def test_corpus(data, name, validate_operation): |
51 | | - schema = schemas[name] |
52 | | - query = data.draw(gql_st.queries(schema)) |
53 | | - validate_operation(schema, query) |
| 73 | +def test_corpus(data, schema: Schema, validate_operation): |
| 74 | + query = data.draw(gql_st.queries(schema.raw, custom_scalars=schema.custom_scalars)) |
| 75 | + validate_operation(schema.raw, query) |
54 | 76 |
|
55 | 77 |
|
56 | | -@pytest.mark.parametrize("name", get_names(schemas, lambda name: "type Mutation" in schemas[name])) |
| 78 | +@pytest.mark.parametrize( |
| 79 | + "schema", |
| 80 | + get_names(schemas, lambda name: "type Mutation {" in schemas[name] and name != "HIVDB"), |
| 81 | + indirect=["schema"], |
| 82 | +) |
57 | 83 | @settings(**CORPUS_SETTINGS) |
58 | 84 | @given(data=st.data()) |
59 | | -def test_corpus_mutations(data, name, validate_operation): |
60 | | - schema = schemas[name] |
61 | | - mutation = data.draw(gql_st.mutations(schema)) |
62 | | - validate_operation(schema, mutation) |
| 85 | +def test_corpus_mutations(data, schema: Schema, validate_operation): |
| 86 | + mutation = data.draw(gql_st.mutations(schema.raw, custom_scalars=schema.custom_scalars)) |
| 87 | + validate_operation(schema.raw, mutation) |
0 commit comments