Skip to content

Commit 1c97460

Browse files
committed
Tracing tests
1 parent d2987a2 commit 1c97460

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
2+
from graphql.execution import execute
3+
from graphql.language.parser import parse
4+
from graphql.type import (GraphQLArgument, GraphQLBoolean, GraphQLField,
5+
GraphQLID, GraphQLInt, GraphQLList, GraphQLNonNull,
6+
GraphQLObjectType, GraphQLSchema, GraphQLString)
7+
8+
current_time = -1
9+
10+
11+
def test_tracing_result(mocker):
12+
13+
def get_time():
14+
global current_time
15+
current_time += 1
16+
return current_time
17+
18+
time_mock = mocker.patch('time.time')
19+
time_mock.side_effect = get_time
20+
21+
BlogImage = GraphQLObjectType('BlogImage', {
22+
'url': GraphQLField(GraphQLString),
23+
'width': GraphQLField(GraphQLInt),
24+
'height': GraphQLField(GraphQLInt),
25+
})
26+
27+
BlogAuthor = GraphQLObjectType('Author', lambda: {
28+
'id': GraphQLField(GraphQLString),
29+
'name': GraphQLField(GraphQLString),
30+
'pic': GraphQLField(BlogImage,
31+
args={
32+
'width': GraphQLArgument(GraphQLInt),
33+
'height': GraphQLArgument(GraphQLInt),
34+
},
35+
resolver=lambda obj, info, **args:
36+
obj.pic(args['width'], args['height'])
37+
),
38+
'recentArticle': GraphQLField(BlogArticle),
39+
})
40+
41+
BlogArticle = GraphQLObjectType('Article', {
42+
'id': GraphQLField(GraphQLNonNull(GraphQLString)),
43+
'isPublished': GraphQLField(GraphQLBoolean),
44+
'author': GraphQLField(BlogAuthor),
45+
'title': GraphQLField(GraphQLString),
46+
'body': GraphQLField(GraphQLString),
47+
'keywords': GraphQLField(GraphQLList(GraphQLString)),
48+
})
49+
50+
BlogQuery = GraphQLObjectType('Query', {
51+
'article': GraphQLField(
52+
BlogArticle,
53+
args={'id': GraphQLArgument(GraphQLID)},
54+
resolver=lambda obj, info, **args: Article(args['id'])),
55+
'feed': GraphQLField(
56+
GraphQLList(BlogArticle),
57+
resolver=lambda *_: map(Article, range(1, 2 + 1))),
58+
})
59+
60+
BlogSchema = GraphQLSchema(BlogQuery)
61+
62+
class Article(object):
63+
64+
def __init__(self, id):
65+
self.id = id
66+
self.isPublished = True
67+
self.author = Author()
68+
self.title = 'My Article {}'.format(id)
69+
self.body = 'This is a post'
70+
self.hidden = 'This data is not exposed in the schema'
71+
self.keywords = ['foo', 'bar', 1, True, None]
72+
73+
class Author(object):
74+
id = 123
75+
name = 'John Smith'
76+
77+
def pic(self, width, height):
78+
return Pic(123, width, height)
79+
80+
@property
81+
def recentArticle(self): return Article(1)
82+
83+
class Pic(object):
84+
85+
def __init__(self, uid, width, height):
86+
self.url = 'cdn://{}'.format(uid)
87+
self.width = str(width)
88+
self.height = str(height)
89+
90+
request = '''
91+
{
92+
feed {
93+
id
94+
title
95+
}
96+
}
97+
'''
98+
99+
# Note: this is intentionally not validating to ensure appropriate
100+
# behavior occurs when executing an invalid query.
101+
result = execute(BlogSchema, parse(request), tracing=True)
102+
assert not result.errors
103+
assert result.data == \
104+
{
105+
"feed": [
106+
{
107+
"id": "1",
108+
"title": "My Article 1"
109+
},
110+
{
111+
"id": "2",
112+
"title": "My Article 2"
113+
},
114+
],
115+
}
116+
117+
assert result.extensions['tracing'] == {
118+
'version': 1,
119+
'startTime': '1970-01-01T00:00:00.fZ',
120+
'endTime': '1970-01-01T04:26:40.fZ',
121+
'duration': 16000,
122+
'execution': {
123+
'resolvers': [
124+
{
125+
'path': ['feed'],
126+
'parentType': 'Query',
127+
'fieldName': 'feed',
128+
'returnType': '[Article]',
129+
'startOffset': 3000,
130+
'duration': 1000
131+
},
132+
{
133+
'path': ['feed', 0, 'id'],
134+
'parentType': 'Article',
135+
'fieldName': 'id',
136+
'returnType': 'String!',
137+
'startOffset': 6000,
138+
'duration': 1000
139+
},
140+
{
141+
'path': ['feed', 0, 'title'],
142+
'parentType': 'Article',
143+
'fieldName': 'title',
144+
'returnType': 'String',
145+
'startOffset': 9000,
146+
'duration': 1000
147+
},
148+
{
149+
'path': ['feed', 1, 'id'],
150+
'parentType': 'Article',
151+
'fieldName': 'id',
152+
'returnType': 'String!',
153+
'startOffset': 12000,
154+
'duration': 1000
155+
},
156+
{
157+
'path': ['feed', 1, 'title'],
158+
'parentType': 'Article',
159+
'fieldName': 'title',
160+
'returnType': 'String',
161+
'startOffset': 15000,
162+
'duration': 1000
163+
}
164+
]
165+
}
166+
}
167+

0 commit comments

Comments
 (0)