-
-
Notifications
You must be signed in to change notification settings - Fork 565
/
Copy pathtest_interface.py
188 lines (144 loc) · 4.09 KB
/
test_interface.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
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
from dataclasses import dataclass
from typing import List
import pytest
import strawberry
from strawberry.exceptions import InvalidSuperclassInterface
def test_query_interface():
@strawberry.interface
class Cheese:
name: str
@strawberry.type
class Swiss(Cheese):
canton: str
@strawberry.type
class Italian(Cheese):
province: str
@strawberry.type
class Root:
@strawberry.field
def assortment(self) -> List[Cheese]:
return [
Italian(name="Asiago", province="Friuli"),
Swiss(name="Tomme", canton="Vaud"),
]
schema = strawberry.Schema(query=Root, types=[Swiss, Italian])
query = """{
assortment {
name
... on Italian { province }
... on Swiss { canton }
}
}"""
result = schema.execute_sync(query)
assert not result.errors
assert result.data["assortment"] == [
{"name": "Asiago", "province": "Friuli"},
{"canton": "Vaud", "name": "Tomme"},
]
def test_interfaces_can_implement_other_interfaces():
@strawberry.interface
class Error:
message: str
@strawberry.interface
class FieldError(Error):
message: str
field: str
@strawberry.type
class PasswordTooShort(FieldError):
message: str
field: str
fix: str
@strawberry.type
class Query:
@strawberry.field
def always_error(self) -> Error:
return PasswordTooShort(
message="Password Too Short",
field="Password",
fix="Choose more characters",
)
schema = strawberry.Schema(Query, types=[PasswordTooShort])
query = """{
alwaysError {
... on Error {
message
}
... on FieldError {
field
}
... on PasswordTooShort {
fix
}
}
}"""
result = schema.execute_sync(query)
assert not result.errors
assert result.data["alwaysError"] == {
"message": "Password Too Short",
"field": "Password",
"fix": "Choose more characters",
}
def test_interface_duck_typing():
@strawberry.interface
class Entity:
id: int
@strawberry.type
class Anime(Entity):
name: str
@dataclass
class AnimeORM:
id: int
name: str
@strawberry.type
class Query:
@strawberry.field
def anime(self) -> Anime:
return AnimeORM(id=1, name="One Piece") # type: ignore
schema = strawberry.Schema(query=Query)
query = """{
anime { name }
}"""
result = schema.execute_sync(query)
assert not result.errors
assert result.data == {"anime": {"name": "One Piece"}}
@pytest.mark.xfail(reason="We don't support returning dictionaries yet")
def test_interface_duck_typing_returning_dict():
@strawberry.interface
class Entity:
id: int
@strawberry.type
class Anime(Entity):
name: str
@dataclass
class AnimeORM:
id: int
name: str
@strawberry.type
class Query:
@strawberry.field
def anime(self) -> Anime:
return dict(id=1, name="One Piece") # type: ignore
schema = strawberry.Schema(query=Query)
query = """{
anime { name }
}"""
result = schema.execute_sync(query)
assert not result.errors
assert result.data == {"anime": {"name": "One Piece"}}
def test_input_cannot_inherit_from_interface():
@strawberry.interface
class SomeInterface:
some_arg: str
with pytest.raises(InvalidSuperclassInterface):
@strawberry.input
class SomeInput(SomeInterface): # this should throw an error
another_arg: str
@strawberry.interface
class SomeOtherInterface:
some_other_arg: str
with pytest.raises(InvalidSuperclassInterface):
@strawberry.input
class SomeOtherInput(
SomeInterface, SomeOtherInterface
): # this should throw an error
another_arg: str