Skip to content

Commit d1f6412

Browse files
author
Thomas Leonard
committed
chore: add tests
1 parent 9509841 commit d1f6412

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
if: ${{ matrix.python == '3.10' }}
5959
uses: actions/upload-artifact@v3
6060
with:
61-
name: graphene-sqlalchemy-coverage
61+
name: graphene-coverage
6262
path: coverage.xml
6363
if-no-files-found: error
6464
- name: Upload coverage.xml to codecov

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ help:
77
install-dev:
88
pip install -e ".[dev]"
99

10+
.PHONY: test ## Run tests
1011
test:
1112
py.test graphene examples
1213

graphene/relay/tests/test_custom_global_id.py

+107
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,110 @@ def test_get_by_id(self):
216216
assert not result.errors
217217
assert result.data["user"]["id"] == self.user_list[1]["id"]
218218
assert result.data["user"]["name"] == self.user_list[1]["name"]
219+
220+
221+
class TestIncompleteCustomGlobalID:
222+
def setup(self):
223+
self.user_list = [
224+
{"id": 1, "name": "First"},
225+
{"id": 2, "name": "Second"},
226+
{"id": 3, "name": "Third"},
227+
{"id": 4, "name": "Fourth"},
228+
]
229+
self.users = {user["id"]: user for user in self.user_list}
230+
231+
def test_must_define_to_global_id(self):
232+
"""
233+
Test that if the `to_global_id` method is not defined, we can query the object, but we can't request its ID.
234+
"""
235+
236+
class CustomGlobalIDType(BaseGlobalIDType):
237+
graphene_type = Int
238+
239+
@classmethod
240+
def resolve_global_id(cls, info, global_id):
241+
_type = info.return_type.graphene_type._meta.name
242+
return _type, global_id
243+
244+
class CustomNode(Node):
245+
class Meta:
246+
global_id_type = CustomGlobalIDType
247+
248+
class User(ObjectType):
249+
class Meta:
250+
interfaces = [CustomNode]
251+
252+
name = String()
253+
254+
@classmethod
255+
def get_node(cls, _type, _id):
256+
return self.users[_id]
257+
258+
class RootQuery(ObjectType):
259+
user = CustomNode.Field(User)
260+
261+
self.schema = Schema(query=RootQuery, types=[User])
262+
self.graphql_schema = self.schema.graphql_schema
263+
264+
query = """query {
265+
user(id: 2) {
266+
name
267+
}
268+
}"""
269+
result = graphql_sync(self.graphql_schema, query)
270+
assert not result.errors
271+
assert result.data["user"]["name"] == self.user_list[1]["name"]
272+
273+
query = """query {
274+
user(id: 2) {
275+
id
276+
name
277+
}
278+
}"""
279+
result = graphql_sync(self.graphql_schema, query)
280+
assert result.errors is not None
281+
assert len(result.errors) == 1
282+
assert result.errors[0].path == ["user", "id"]
283+
284+
def test_must_define_resolve_global_id(self):
285+
"""
286+
Test that if the `resolve_global_id` method is not defined, we can't query the object by ID.
287+
"""
288+
289+
class CustomGlobalIDType(BaseGlobalIDType):
290+
graphene_type = Int
291+
292+
@classmethod
293+
def to_global_id(cls, _type, _id):
294+
return _id
295+
296+
class CustomNode(Node):
297+
class Meta:
298+
global_id_type = CustomGlobalIDType
299+
300+
class User(ObjectType):
301+
class Meta:
302+
interfaces = [CustomNode]
303+
304+
name = String()
305+
306+
@classmethod
307+
def get_node(cls, _type, _id):
308+
return self.users[_id]
309+
310+
class RootQuery(ObjectType):
311+
user = CustomNode.Field(User)
312+
313+
self.schema = Schema(query=RootQuery, types=[User])
314+
self.graphql_schema = self.schema.graphql_schema
315+
316+
query = """query {
317+
user(id: 2) {
318+
id
319+
name
320+
}
321+
}"""
322+
result = graphql_sync(self.graphql_schema, query)
323+
assert result.errors is not None
324+
assert len(result.errors) == 1
325+
assert result.errors[0].path == ["user"]

graphene/relay/tests/test_node.py

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_node_good():
5555
assert "id" in MyNode._meta.fields
5656
assert is_node(MyNode)
5757
assert not is_node(object)
58+
assert not is_node("node")
5859

5960

6061
def test_node_query():

0 commit comments

Comments
 (0)