File: test_mutation.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 (206 lines) | stat: -rw-r--r-- 5,750 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from pytest import mark, raises

from ...types import (
    ID,
    Argument,
    Field,
    InputField,
    InputObjectType,
    NonNull,
    ObjectType,
    Schema,
)
from ...types.scalars import String
from ..mutation import ClientIDMutation


class SharedFields:
    shared = String()


class MyNode(ObjectType):
    # class Meta:
    #     interfaces = (Node, )
    id = ID()
    name = String()


class SaySomething(ClientIDMutation):
    class Input:
        what = String()

    phrase = String()

    @staticmethod
    def mutate_and_get_payload(self, info, what, client_mutation_id=None):
        return SaySomething(phrase=str(what))


class FixedSaySomething:
    __slots__ = ("phrase",)

    def __init__(self, phrase):
        self.phrase = phrase


class SaySomethingFixed(ClientIDMutation):
    class Input:
        what = String()

    phrase = String()

    @staticmethod
    def mutate_and_get_payload(self, info, what, client_mutation_id=None):
        return FixedSaySomething(phrase=str(what))


class SaySomethingAsync(ClientIDMutation):
    class Input:
        what = String()

    phrase = String()

    @staticmethod
    async def mutate_and_get_payload(self, info, what, client_mutation_id=None):
        return SaySomething(phrase=str(what))


# MyEdge = MyNode.Connection.Edge
class MyEdge(ObjectType):
    node = Field(MyNode)
    cursor = String()


class OtherMutation(ClientIDMutation):
    class Input(SharedFields):
        additional_field = String()

    name = String()
    my_node_edge = Field(MyEdge)

    @staticmethod
    def mutate_and_get_payload(
        self, info, shared="", additional_field="", client_mutation_id=None
    ):
        edge_type = MyEdge
        return OtherMutation(
            name=shared + additional_field,
            my_node_edge=edge_type(cursor="1", node=MyNode(name="name")),
        )


class RootQuery(ObjectType):
    something = String()


class Mutation(ObjectType):
    say = SaySomething.Field()
    say_fixed = SaySomethingFixed.Field()
    say_async = SaySomethingAsync.Field()
    other = OtherMutation.Field()


schema = Schema(query=RootQuery, mutation=Mutation)


def test_no_mutate_and_get_payload():
    with raises(AssertionError) as excinfo:

        class MyMutation(ClientIDMutation):
            pass

    assert (
        "MyMutation.mutate_and_get_payload method is required in a ClientIDMutation."
        == str(excinfo.value)
    )


def test_mutation():
    fields = SaySomething._meta.fields
    assert list(fields) == ["phrase", "client_mutation_id"]
    assert SaySomething._meta.name == "SaySomethingPayload"
    assert isinstance(fields["phrase"], Field)
    field = SaySomething.Field()
    assert field.type == SaySomething
    assert list(field.args) == ["input"]
    assert isinstance(field.args["input"], Argument)
    assert isinstance(field.args["input"].type, NonNull)
    assert field.args["input"].type.of_type == SaySomething.Input
    assert isinstance(fields["client_mutation_id"], Field)
    assert fields["client_mutation_id"].name == "clientMutationId"
    assert fields["client_mutation_id"].type == String


def test_mutation_input():
    Input = SaySomething.Input
    assert issubclass(Input, InputObjectType)
    fields = Input._meta.fields
    assert list(fields) == ["what", "client_mutation_id"]
    assert isinstance(fields["what"], InputField)
    assert fields["what"].type == String
    assert isinstance(fields["client_mutation_id"], InputField)
    assert fields["client_mutation_id"].type == String


def test_subclassed_mutation():
    fields = OtherMutation._meta.fields
    assert list(fields) == ["name", "my_node_edge", "client_mutation_id"]
    assert isinstance(fields["name"], Field)
    field = OtherMutation.Field()
    assert field.type == OtherMutation
    assert list(field.args) == ["input"]
    assert isinstance(field.args["input"], Argument)
    assert isinstance(field.args["input"].type, NonNull)
    assert field.args["input"].type.of_type == OtherMutation.Input


def test_subclassed_mutation_input():
    Input = OtherMutation.Input
    assert issubclass(Input, InputObjectType)
    fields = Input._meta.fields
    assert list(fields) == ["shared", "additional_field", "client_mutation_id"]
    assert isinstance(fields["shared"], InputField)
    assert fields["shared"].type == String
    assert isinstance(fields["additional_field"], InputField)
    assert fields["additional_field"].type == String
    assert isinstance(fields["client_mutation_id"], InputField)
    assert fields["client_mutation_id"].type == String


def test_node_query():
    executed = schema.execute(
        'mutation a { say(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
    )
    assert not executed.errors
    assert executed.data == {"say": {"phrase": "hello"}}


def test_node_query_fixed():
    executed = schema.execute(
        'mutation a { sayFixed(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
    )
    assert "Cannot set client_mutation_id in the payload object" in str(
        executed.errors[0]
    )


@mark.asyncio
async def test_node_query_async():
    executed = await schema.execute_async(
        'mutation a { sayAsync(input: {what:"hello", clientMutationId:"1"}) { phrase } }'
    )
    assert not executed.errors
    assert executed.data == {"sayAsync": {"phrase": "hello"}}


def test_edge_query():
    executed = schema.execute(
        'mutation a { other(input: {clientMutationId:"1"}) { clientMutationId, myNodeEdge { cursor node { name }} } }'
    )
    assert not executed.errors
    assert dict(executed.data) == {
        "other": {
            "clientMutationId": "1",
            "myNodeEdge": {"cursor": "1", "node": {"name": "name"}},
        }
    }