1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
"""Tests for GraphQL schema parser."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
import pytest
from datamodel_code_generator.model.dataclass import DataClass
from datamodel_code_generator.parser.graphql import GraphQLParser
from datamodel_code_generator.reference import Reference
from tests.conftest import create_assert_file_content
from tests.main.conftest import GRAPHQL_DATA_PATH, run_main_and_assert
from tests.main.test_main_general import DATA_PATH
if TYPE_CHECKING:
from pathlib import Path
EXPECTED_GRAPHQL_PATH: Path = DATA_PATH / "expected" / "parser" / "graphql"
assert_file_content = create_assert_file_content(EXPECTED_GRAPHQL_PATH)
def test_graphql_field_enum(output_file: Path) -> None:
"""Test parsing GraphQL field with enum default value."""
run_main_and_assert(
input_path=GRAPHQL_DATA_PATH / "field-default-enum.graphql",
output_path=output_file,
input_file_type="graphql",
assert_func=assert_file_content,
expected_file="field-default-enum.py",
extra_args=["--set-default-enum-member"],
)
def test_graphql_union_aliased_bug(output_file: Path) -> None:
"""Test parsing GraphQL union with aliased types."""
run_main_and_assert(
input_path=GRAPHQL_DATA_PATH / "union-aliased-bug.graphql",
output_path=output_file,
input_file_type="graphql",
assert_func=assert_file_content,
expected_file="union-aliased-bug.py",
)
def test_graphql_union_commented(output_file: Path) -> None:
"""Test parsing GraphQL union with comments."""
run_main_and_assert(
input_path=GRAPHQL_DATA_PATH / "union-commented.graphql",
output_path=output_file,
input_file_type="graphql",
assert_func=assert_file_content,
expected_file="union-commented.py",
)
def test_graphql_union_with_prefix(output_file: Path) -> None:
"""Test parsing GraphQL union with class name prefix (Unions should reference prefixed class names)."""
run_main_and_assert(
input_path=GRAPHQL_DATA_PATH / "union.graphql",
output_path=output_file,
input_file_type="graphql",
assert_func=assert_file_content,
expected_file="union_with_prefix.py",
extra_args=["--class-name-prefix", "Foo"],
)
@pytest.mark.parametrize(
("frozen_dataclasses", "keyword_only", "parser_dataclass_args", "kwargs_dataclass_args", "expected"),
[
(False, False, None, None, {}),
(True, False, None, None, {"frozen": True}),
(False, True, None, None, {"kw_only": True}),
(True, True, None, None, {"frozen": True, "kw_only": True}),
(False, False, {"slots": True}, None, {"slots": True}),
(True, True, {"slots": True}, None, {"slots": True}),
(True, True, {"slots": True}, {"order": True}, {"order": True}),
],
)
def test_create_data_model_dataclass_arguments(
frozen_dataclasses: bool,
keyword_only: bool,
parser_dataclass_args: dict | None,
kwargs_dataclass_args: dict | None,
expected: dict,
) -> None:
"""Test _create_data_model handles dataclass_arguments correctly."""
parser = GraphQLParser(
source="type Query { id: ID }",
data_model_type=DataClass,
frozen_dataclasses=frozen_dataclasses,
keyword_only=keyword_only,
)
parser.dataclass_arguments = parser_dataclass_args
reference = Reference(path="test", original_name="Test", name="Test")
kwargs: dict[str, Any] = {"reference": reference, "fields": []}
if kwargs_dataclass_args is not None:
kwargs["dataclass_arguments"] = kwargs_dataclass_args
result = parser._create_data_model(**kwargs)
assert isinstance(result, DataClass)
assert result.dataclass_arguments == expected
def test_create_data_model_class_decorators() -> None:
"""Test _create_data_model applies class_decorators correctly."""
parser = GraphQLParser(
source="type Query { id: ID }",
data_model_type=DataClass,
class_decorators=["@dataclass_json"],
)
reference = Reference(path="test", original_name="Test", name="Test")
result = parser._create_data_model(reference=reference, fields=[])
assert isinstance(result, DataClass)
assert result.decorators == ["@dataclass_json"]
def test_graphql_no_typename(output_file: Path) -> None:
"""Test that --graphql-no-typename excludes typename__ field from all types."""
run_main_and_assert(
input_path=GRAPHQL_DATA_PATH / "no-typename.graphql",
output_path=output_file,
input_file_type="graphql",
assert_func=assert_file_content,
expected_file="no_typename.py",
extra_args=["--graphql-no-typename"],
)
def test_graphql_typename_included_by_default(output_file: Path) -> None:
"""Regression test: typename__ field is included by default."""
def assert_typename_present(output_path: Path, _: str | None, **_kwargs: object) -> None:
content = output_path.read_text(encoding="utf-8")
assert "typename__" in content, "typename__ field should be present by default"
assert "__typename" in content, "__typename alias should be present by default"
run_main_and_assert(
input_path=GRAPHQL_DATA_PATH / "no-typename.graphql",
output_path=output_file,
input_file_type="graphql",
assert_func=assert_typename_present,
expected_file=None,
)
def test_graphql_schema_features() -> None:
"""Test that GraphQLParser has schema_features property returning JsonSchemaFeatures."""
from inline_snapshot import snapshot
from datamodel_code_generator.parser.schema_version import JsonSchemaFeatures
parser = GraphQLParser(
source="type Query { id: ID }",
data_model_type=DataClass,
)
features = parser.schema_features
assert isinstance(features, JsonSchemaFeatures)
assert features == snapshot(
JsonSchemaFeatures(
null_in_type_array=True,
defs_not_definitions=True,
prefix_items=True,
boolean_schemas=True,
id_field="$id",
definitions_key="$defs",
exclusive_as_number=True,
read_only_write_only=True,
recursive_ref=True,
dynamic_ref=True,
)
)
|