-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtest_graphene.py
131 lines (96 loc) · 2.76 KB
/
test_graphene.py
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
from typing import List
import graphene
import pydantic
from pydantic import TypeAdapter
from graphene_pydantic import PydanticInputObjectType, PydanticObjectType
class Foo(pydantic.BaseModel):
name: str
class Bar(pydantic.BaseModel):
count: int
class FooBar(Foo, Bar):
pass
class FooBarOutput(PydanticObjectType):
class Meta:
model = FooBar
class FooBarInput(PydanticInputObjectType):
class Meta:
model = FooBar
foo_bars: List[FooBar] = []
class Query(graphene.ObjectType):
list_foo_bars = graphene.List(FooBarOutput, match=FooBarInput())
@staticmethod
def resolve_list_foo_bars(parent, info, match: FooBarInput = None):
if match is None:
return foo_bars
return [f for f in foo_bars if f == FooBar.model_validate(match)]
class CreateFooBar(graphene.Mutation):
class Arguments:
data = FooBarInput()
Output = FooBarOutput
@staticmethod
def mutate(root, info, data: FooBarInput):
foo_bars.append(TypeAdapter(FooBar).validate_python(data))
return data
class Mutation(graphene.ObjectType):
create_foo_bar = CreateFooBar.Field()
def test_query():
global foo_bars
foo_bars = [FooBar(name="test", count=1)]
schema = graphene.Schema(query=Query)
query = """
query {
listFooBars {
name
count
}
}
"""
result = schema.execute(query)
assert result.errors is None
assert result.data is not None
assert (
TypeAdapter(List[FooBar]).validate_python(result.data["listFooBars"])
== foo_bars
)
def test_query_with_match():
global foo_bars
foo_bars = [FooBar(name="test", count=1)]
schema = graphene.Schema(query=Query)
query = """
query {
listFooBars(match: {name: "test", count: 1}) {
name
count
}
}
"""
result = schema.execute(query)
assert result.errors is None
assert result.data is not None
assert (
TypeAdapter(List[FooBar]).validate_python(result.data["listFooBars"])
== foo_bars
)
def test_mutation():
global foo_bars
foo_bars = []
schema = graphene.Schema(query=Query, mutation=Mutation)
new_foo_bar = FooBar(name="mutant", count=-1)
query = """
mutation {
createFooBar(data: {name: "%s", count: %d}) {
name
count
}
}
""" % (
new_foo_bar.name,
new_foo_bar.count,
)
result = schema.execute(query)
assert result.errors is None
assert result.data is not None
assert foo_bars[0] == new_foo_bar
assert (
TypeAdapter(FooBar).validate_python(result.data["createFooBar"]) == new_foo_bar
)