File: test_inputobjecttype.py

package info (click to toggle)
python-graphene 3.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,935; makefile: 214; sh: 18
file content (169 lines) | stat: -rw-r--r-- 4,892 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
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
from graphql import Undefined

from ..argument import Argument
from ..field import Field
from ..inputfield import InputField
from ..inputobjecttype import InputObjectType
from ..objecttype import ObjectType
from ..scalars import Boolean, String
from ..schema import Schema
from ..unmountedtype import UnmountedType
from ... import NonNull


class MyType:
    pass


class MyScalar(UnmountedType):
    def get_type(self):
        return MyType


def test_generate_inputobjecttype():
    class MyInputObjectType(InputObjectType):
        """Documentation"""

    assert MyInputObjectType._meta.name == "MyInputObjectType"
    assert MyInputObjectType._meta.description == "Documentation"
    assert MyInputObjectType._meta.fields == {}


def test_generate_inputobjecttype_with_meta():
    class MyInputObjectType(InputObjectType):
        class Meta:
            name = "MyOtherInputObjectType"
            description = "Documentation"

    assert MyInputObjectType._meta.name == "MyOtherInputObjectType"
    assert MyInputObjectType._meta.description == "Documentation"


def test_generate_inputobjecttype_with_fields():
    class MyInputObjectType(InputObjectType):
        field = Field(MyType)

    assert "field" in MyInputObjectType._meta.fields


def test_ordered_fields_in_inputobjecttype():
    class MyInputObjectType(InputObjectType):
        b = InputField(MyType)
        a = InputField(MyType)
        field = MyScalar()
        asa = InputField(MyType)

    assert list(MyInputObjectType._meta.fields) == ["b", "a", "field", "asa"]


def test_generate_inputobjecttype_unmountedtype():
    class MyInputObjectType(InputObjectType):
        field = MyScalar(MyType)

    assert "field" in MyInputObjectType._meta.fields
    assert isinstance(MyInputObjectType._meta.fields["field"], InputField)


def test_generate_inputobjecttype_as_argument():
    class MyInputObjectType(InputObjectType):
        field = MyScalar()

    class MyObjectType(ObjectType):
        field = Field(MyType, input=MyInputObjectType())

    assert "field" in MyObjectType._meta.fields
    field = MyObjectType._meta.fields["field"]
    assert isinstance(field, Field)
    assert field.type == MyType
    assert "input" in field.args
    assert isinstance(field.args["input"], Argument)
    assert field.args["input"].type == MyInputObjectType


def test_generate_inputobjecttype_inherit_abstracttype():
    class MyAbstractType:
        field1 = MyScalar(MyType)

    class MyInputObjectType(InputObjectType, MyAbstractType):
        field2 = MyScalar(MyType)

    assert list(MyInputObjectType._meta.fields) == ["field1", "field2"]
    assert [type(x) for x in MyInputObjectType._meta.fields.values()] == [
        InputField,
        InputField,
    ]


def test_generate_inputobjecttype_inherit_abstracttype_reversed():
    class MyAbstractType:
        field1 = MyScalar(MyType)

    class MyInputObjectType(MyAbstractType, InputObjectType):
        field2 = MyScalar(MyType)

    assert list(MyInputObjectType._meta.fields) == ["field1", "field2"]
    assert [type(x) for x in MyInputObjectType._meta.fields.values()] == [
        InputField,
        InputField,
    ]


def test_inputobjecttype_of_input():
    class Child(InputObjectType):
        first_name = String()
        last_name = String()

        @property
        def full_name(self):
            return f"{self.first_name} {self.last_name}"

    class Parent(InputObjectType):
        child = InputField(Child)

    class Query(ObjectType):
        is_child = Boolean(parent=Parent())

        def resolve_is_child(self, info, parent):
            return (
                isinstance(parent.child, Child)
                and parent.child.full_name == "Peter Griffin"
            )

    schema = Schema(query=Query)
    result = schema.execute(
        """query basequery {
        isChild(parent: {child: {firstName: "Peter", lastName: "Griffin"}})
    }
    """
    )

    assert not result.errors
    assert result.data == {"isChild": True}


def test_inputobjecttype_default_input_as_undefined(
    set_default_input_object_type_to_undefined,
):
    class TestUndefinedInput(InputObjectType):
        required_field = String(required=True)
        optional_field = String()

    class Query(ObjectType):
        undefined_optionals_work = Field(NonNull(Boolean), input=TestUndefinedInput())

        def resolve_undefined_optionals_work(self, info, input: TestUndefinedInput):
            # Confirm that optional_field comes as Undefined
            return (
                input.required_field == "required" and input.optional_field is Undefined
            )

    schema = Schema(query=Query)
    result = schema.execute(
        """query basequery {
        undefinedOptionalsWork(input: {requiredField: "required"})
    }
    """
    )

    assert not result.errors
    assert result.data == {"undefinedOptionalsWork": True}