|
| 1 | +from ariadne_codegen.settings import get_validation_rule |
1 | 2 | import httpx
|
2 | 3 | import pytest
|
3 | 4 | from graphql import GraphQLSchema, OperationDefinitionNode, build_schema
|
|
15 | 16 | read_graphql_file,
|
16 | 17 | walk_graphql_files,
|
17 | 18 | )
|
| 19 | +from ariadne_codegen.settings import ValidationRuleSkips |
18 | 20 |
|
19 | 21 |
|
20 | 22 | @pytest.fixture
|
@@ -63,6 +65,49 @@ def test_query_2_str():
|
63 | 65 | }
|
64 | 66 | """
|
65 | 67 |
|
| 68 | +@pytest.fixture |
| 69 | +def test_fragment_str(): |
| 70 | + return """ |
| 71 | + fragment fragmentA on Custom { |
| 72 | + node |
| 73 | + } |
| 74 | + query testQuery2 { |
| 75 | + test { |
| 76 | + default |
| 77 | + ...fragmentA |
| 78 | + } |
| 79 | + } |
| 80 | + """ |
| 81 | + |
| 82 | +@pytest.fixture |
| 83 | +def test_duplicate_fragment_str(): |
| 84 | + return """ |
| 85 | + fragment fragmentA on Custom { |
| 86 | + node |
| 87 | + } |
| 88 | + fragment fragmentA on Custom { |
| 89 | + node |
| 90 | + } |
| 91 | + query testQuery2 { |
| 92 | + test { |
| 93 | + default |
| 94 | + ...fragmentA |
| 95 | + } |
| 96 | + } |
| 97 | + """ |
| 98 | + |
| 99 | +@pytest.fixture |
| 100 | +def test_unused_fragment_str(): |
| 101 | + return """ |
| 102 | + fragment fragmentA on Custom { |
| 103 | + node |
| 104 | + } |
| 105 | + query testQuery2 { |
| 106 | + test { |
| 107 | + default |
| 108 | + } |
| 109 | + } |
| 110 | + """ |
66 | 111 |
|
67 | 112 | @pytest.fixture
|
68 | 113 | def single_file_schema(tmp_path_factory, schema_str):
|
@@ -132,6 +177,24 @@ def single_file_query(tmp_path_factory, test_query_str):
|
132 | 177 | file_.write_text(test_query_str, encoding="utf-8")
|
133 | 178 | return file_
|
134 | 179 |
|
| 180 | +@pytest.fixture |
| 181 | +def single_file_query_with_fragment(tmp_path_factory, test_query_str, test_fragment_str): |
| 182 | + file_ = tmp_path_factory.mktemp("queries").joinpath("query1_fragment.graphql") |
| 183 | + file_.write_text(test_query_str + test_fragment_str, encoding="utf-8") |
| 184 | + return file_ |
| 185 | + |
| 186 | +@pytest.fixture |
| 187 | +def single_file_query_with_duplicate_fragment(tmp_path_factory, test_query_str, test_duplicate_fragment_str): |
| 188 | + file_ = tmp_path_factory.mktemp("queries").joinpath("query1_duplicate_fragment.graphql") |
| 189 | + file_.write_text(test_query_str + test_duplicate_fragment_str, encoding="utf-8") |
| 190 | + return file_ |
| 191 | + |
| 192 | +@pytest.fixture |
| 193 | +def single_file_query_with_unused_fragment(tmp_path_factory, test_query_str, test_unused_fragment_str): |
| 194 | + file_ = tmp_path_factory.mktemp("queries").joinpath("query1_unused_fragment.graphql") |
| 195 | + file_.write_text(test_query_str + test_unused_fragment_str, encoding="utf-8") |
| 196 | + return file_ |
| 197 | + |
135 | 198 |
|
136 | 199 | @pytest.fixture
|
137 | 200 | def invalid_syntax_query_file(tmp_path_factory):
|
@@ -434,3 +497,36 @@ def test_get_graphql_queries_with_invalid_query_for_schema_raises_invalid_operat
|
434 | 497 | get_graphql_queries(
|
435 | 498 | invalid_query_for_schema_file.as_posix(), build_schema(schema_str)
|
436 | 499 | )
|
| 500 | + |
| 501 | + |
| 502 | +def test_get_graphql_queries_with_fragment_returns_schema_definitions( |
| 503 | + single_file_query_with_fragment, schema_str |
| 504 | +): |
| 505 | + queries = get_graphql_queries( |
| 506 | + single_file_query_with_fragment.as_posix(), build_schema(schema_str) |
| 507 | + ) |
| 508 | + |
| 509 | + assert len(queries) == 3 |
| 510 | + |
| 511 | +def test_get_graphql_queries_with_duplicate_fragment_raises_invalid_operation( |
| 512 | + single_file_query_with_duplicate_fragment, schema_str |
| 513 | +): |
| 514 | + with pytest.raises(InvalidOperationForSchema): |
| 515 | + get_graphql_queries( |
| 516 | + single_file_query_with_duplicate_fragment.as_posix(), build_schema(schema_str) |
| 517 | + ) |
| 518 | + |
| 519 | +def test_get_graphql_queries_with_unused_fragment_and_no_skip_rules_raises_invalid_operation( |
| 520 | + single_file_query_with_unused_fragment, schema_str |
| 521 | +): |
| 522 | + with pytest.raises(InvalidOperationForSchema): |
| 523 | + get_graphql_queries( |
| 524 | + single_file_query_with_unused_fragment.as_posix(), build_schema(schema_str), [] |
| 525 | + ) |
| 526 | + |
| 527 | +def test_get_graphql_queries_with_skip_unique_fragment_names_and_duplicate_fragment_returns_schema_definition( |
| 528 | + single_file_query_with_duplicate_fragment, schema_str |
| 529 | +): |
| 530 | + get_graphql_queries( |
| 531 | + single_file_query_with_duplicate_fragment.as_posix(), build_schema(schema_str), [get_validation_rule(ValidationRuleSkips.NoUnusedFragments),get_validation_rule(ValidationRuleSkips.UniqueFragmentNames)] |
| 532 | + ) |
0 commit comments