File: test_names.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (140 lines) | stat: -rw-r--r-- 3,438 bytes parent folder | download
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
import textwrap
from typing import Annotated, Generic, NewType, TypeVar

import pytest

import strawberry
from strawberry.schema.config import StrawberryConfig
from strawberry.types.base import StrawberryList, StrawberryOptional
from strawberry.types.enum import StrawberryEnumDefinition
from strawberry.types.union import StrawberryUnion

T = TypeVar("T")
K = TypeVar("K")
V = TypeVar("V")


Enum = StrawberryEnumDefinition(None, name="Enum", values=[], description=None)  # type: ignore
CustomInt = strawberry.scalar(NewType("CustomInt", int))


@strawberry.type
class TypeA:
    name: str


@strawberry.type
class TypeB:
    age: int


@pytest.mark.parametrize(
    ("types", "expected_name"),
    [
        ([StrawberryList(str)], "StrListExample"),
        ([StrawberryList(StrawberryList(str))], "StrListListExample"),
        ([StrawberryOptional(StrawberryList(str))], "StrListOptionalExample"),
        ([StrawberryList(StrawberryOptional(str))], "StrOptionalListExample"),
        ([StrawberryList(Enum)], "EnumListExample"),
        ([StrawberryUnion("Union", (TypeA, TypeB))], "UnionExample"),  # pyright: ignore
        ([TypeA], "TypeAExample"),
        ([CustomInt], "CustomIntExample"),
        ([TypeA, TypeB], "TypeATypeBExample"),
        (
            [
                TypeA,
                Annotated[
                    "TypeB", strawberry.lazy("tests.objects.generics.test_names")
                ],
            ],
            "TypeATypeBExample",
        ),
    ],
)
def test_name_generation(types, expected_name):
    config = StrawberryConfig()

    @strawberry.type
    class Example(Generic[T]):
        a: T

    type_definition = Example.__strawberry_definition__  # type: ignore

    assert config.name_converter.from_generic(type_definition, types) == expected_name


def test_nested_generics():
    config = StrawberryConfig()

    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Connection(Generic[T]):
        edges: list[T]

    type_definition = Connection.__strawberry_definition__  # type: ignore

    assert (
        config.name_converter.from_generic(
            type_definition,
            [
                Edge[int],
            ],
        )
        == "IntEdgeConnection"
    )


def test_nested_generics_aliases_with_schema():
    """This tests is similar to the previous test, but it also tests against
    the schema, since the resolution of the type name might be different.
    """
    config = StrawberryConfig()

    @strawberry.type
    class Value(Generic[T]):
        value: T

    @strawberry.type
    class DictItem(Generic[K, V]):
        key: K
        value: V

    type_definition = Value.__strawberry_definition__  # type: ignore

    assert (
        config.name_converter.from_generic(
            type_definition,
            [
                StrawberryList(DictItem[int, str]),
            ],
        )
        == "IntStrDictItemListValue"
    )

    @strawberry.type
    class Query:
        d: Value[list[DictItem[int, str]]]

    schema = strawberry.Schema(query=Query)

    expected = textwrap.dedent(
        """
        type IntStrDictItem {
          key: Int!
          value: String!
        }

        type IntStrDictItemListValue {
          value: [IntStrDictItem!]!
        }

        type Query {
          d: IntStrDictItemListValue!
        }
        """
    ).strip()

    assert str(schema) == expected