Skip to content

Commit 6660f92

Browse files
authored
Merge pull request #87 from valdergallo/master
add setup.py test command
2 parents 705195b + 95c5f66 commit 6660f92

File tree

3 files changed

+150
-66
lines changed

3 files changed

+150
-66
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ before_install:
2121
source "$HOME/virtualenvs/pypy-$PYPY_VERSION/bin/activate"
2222
fi
2323
install:
24-
- pip install pytest-cov pytest-mock coveralls flake8 isort==3.9.6 gevent==1.1b5 six>=1.10.0 promise>=0.4.2 pytest-benchmark
24+
- pip install pytest-cov pytest-mock coveralls flake8 gevent==1.1b5 six>=1.10.0 promise>=0.4.2 pytest-benchmark
2525
- pip install pytest==2.9.2
2626
- pip install -e .
2727
script:
@@ -36,5 +36,4 @@ matrix:
3636
- pip install pytest-asyncio
3737
script:
3838
- flake8
39-
- isort --check-only graphql/ -rc
4039
- py.test --cov=graphql graphql tests tests_py35

graphql/execution/tests/test_benchmark.py

Lines changed: 115 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,48 @@
55
GraphQLSchema, Source, execute, parse)
66

77

8-
def test_big_list_of_ints(benchmark):
9-
big_int_list = [x for x in range(5000)]
8+
# set global fixtures
9+
Container = namedtuple('Container', 'x y z o')
10+
big_int_list = [x for x in range(5000)]
11+
big_container_list = [Container(x=x, y=x, z=x, o=x) for x in range(5000)]
12+
13+
ContainerType = GraphQLObjectType('Container',
14+
fields={
15+
'x': GraphQLField(GraphQLInt),
16+
'y': GraphQLField(GraphQLInt),
17+
'z': GraphQLField(GraphQLInt),
18+
'o': GraphQLField(GraphQLInt),
19+
})
20+
21+
22+
def resolve_all_containers(root, args, context, info):
23+
return big_container_list
1024

11-
def resolve_all_ints(root, args, context, info):
12-
return big_int_list
1325

26+
def resolve_all_ints(root, args, context, info):
27+
return big_int_list
28+
29+
30+
def test_big_list_of_ints_to_graphql_schema(benchmark):
31+
@benchmark
32+
def schema():
33+
Query = GraphQLObjectType('Query', fields={
34+
'allInts': GraphQLField(
35+
GraphQLList(GraphQLInt),
36+
resolver=resolve_all_ints
37+
)
38+
})
39+
return GraphQLSchema(Query)
40+
41+
42+
def test_big_list_of_ints_to_graphql_ast(benchmark):
43+
@benchmark
44+
def ast():
45+
source = Source('{ allInts }')
46+
return parse(source)
47+
48+
49+
def test_big_list_of_ints_to_graphql_partial(benchmark):
1450
Query = GraphQLObjectType('Query', fields={
1551
'allInts': GraphQLField(
1652
GraphQLList(GraphQLInt),
@@ -20,43 +56,55 @@ def resolve_all_ints(root, args, context, info):
2056
hello_schema = GraphQLSchema(Query)
2157
source = Source('{ allInts }')
2258
ast = parse(source)
23-
big_list_query = partial(execute, hello_schema, ast)
24-
result = benchmark(big_list_query)
25-
# result = big_list_query()
26-
assert not result.errors
27-
assert result.data == {'allInts': big_int_list}
2859

60+
@benchmark
61+
def b():
62+
return partial(execute, hello_schema, ast)
63+
64+
def test_big_list_of_ints_to_graphql_total(benchmark):
65+
@benchmark
66+
def total():
67+
Query = GraphQLObjectType('Query', fields={
68+
'allInts': GraphQLField(
69+
GraphQLList(GraphQLInt),
70+
resolver=resolve_all_ints
71+
)
72+
})
73+
hello_schema = GraphQLSchema(Query)
74+
source = Source('{ allInts }')
75+
ast = parse(source)
76+
return partial(execute, hello_schema, ast)
2977

3078

31-
def test_big_list_of_ints_base(benchmark):
32-
big_int_list = [x for x in range(5000)]
79+
def test_big_list_of_ints_base_serialize(benchmark):
3380
from ..executor import complete_leaf_value
34-
# def convert_item(i):
35-
# return i
3681

37-
def convert_list():
38-
r = []
82+
@benchmark
83+
def serialize():
3984
for i in big_int_list:
40-
r.append(GraphQLInt.serialize(i))
41-
return r
42-
benchmark(convert_list)
85+
GraphQLInt.serialize(i)
4386

4487

45-
def test_big_list_of_containers_with_one_field(benchmark):
46-
Container = namedtuple('Container', 'x y z o')
88+
def test_total_big_list_of_containers_with_one_field_schema(benchmark):
89+
@benchmark
90+
def schema():
91+
Query = GraphQLObjectType('Query', fields={
92+
'allContainers': GraphQLField(
93+
GraphQLList(ContainerType),
94+
resolver=resolve_all_containers
95+
)
96+
})
97+
return GraphQLSchema(Query)
4798

48-
ContainerType = GraphQLObjectType('Container', fields={
49-
'x': GraphQLField(GraphQLInt),
50-
'y': GraphQLField(GraphQLInt),
51-
'z': GraphQLField(GraphQLInt),
52-
'o': GraphQLField(GraphQLInt),
53-
})
5499

55-
big_container_list = [Container(x=x, y=x, z=x, o=x) for x in range(5000)]
100+
def test_total_big_list_of_containers_with_one_field_parse(benchmark):
101+
@benchmark
102+
def ast():
103+
source = Source('{ allContainers { x } }')
104+
ast = parse(source)
56105

57-
def resolve_all_containers(root, args, context, info):
58-
return big_container_list
59106

107+
def test_total_big_list_of_containers_with_one_field_partial(benchmark):
60108
Query = GraphQLObjectType('Query', fields={
61109
'allContainers': GraphQLField(
62110
GraphQLList(ContainerType),
@@ -66,39 +114,55 @@ def resolve_all_containers(root, args, context, info):
66114
hello_schema = GraphQLSchema(Query)
67115
source = Source('{ allContainers { x } }')
68116
ast = parse(source)
69-
big_list_query = partial(execute, hello_schema, ast)
70-
result = benchmark(big_list_query)
71-
# result = big_list_query()
72-
assert not result.errors
73-
assert result.data == {'allContainers': [{'x': c.x} for c in big_container_list]}
74117

118+
@benchmark
119+
def b():
120+
return partial(execute, hello_schema, ast)
75121

76-
def test_big_list_of_containers_with_multiple_fields(benchmark):
77-
Container = namedtuple('Container', 'x y z o')
78122

79-
ContainerType = GraphQLObjectType('Container', fields={
80-
'x': GraphQLField(GraphQLInt),
81-
'y': GraphQLField(GraphQLInt),
82-
'z': GraphQLField(GraphQLInt),
83-
'o': GraphQLField(GraphQLInt),
84-
})
123+
def test_total_big_list_of_containers_with_one_field_total(benchmark):
124+
@benchmark
125+
def total():
126+
Query = GraphQLObjectType('Query', fields={
127+
'allContainers': GraphQLField(
128+
GraphQLList(ContainerType),
129+
resolver=resolve_all_containers
130+
)
131+
})
132+
hello_schema = GraphQLSchema(Query)
133+
source = Source('{ allContainers { x } }')
134+
ast = parse(source)
135+
result = partial(execute, hello_schema, ast)
85136

86-
big_container_list = [Container(x=x, y=x, z=x, o=x) for x in range(5000)]
87-
88-
def resolve_all_containers(root, args, context, info):
89-
return big_container_list
90137

138+
def test_total_big_list_of_containers_with_multiple_fields_partial(benchmark):
91139
Query = GraphQLObjectType('Query', fields={
92140
'allContainers': GraphQLField(
93141
GraphQLList(ContainerType),
94142
resolver=resolve_all_containers
95143
)
96144
})
145+
97146
hello_schema = GraphQLSchema(Query)
98147
source = Source('{ allContainers { x, y, z } }')
99148
ast = parse(source)
100-
big_list_query = partial(execute, hello_schema, ast)
101-
result = benchmark(big_list_query)
102-
# result = big_list_query()
103-
assert not result.errors
104-
assert result.data == {'allContainers': [{'x': c.x, 'y': c.y, 'z': c.z} for c in big_container_list]}
149+
150+
@benchmark
151+
def b():
152+
return partial(execute, hello_schema, ast)
153+
154+
155+
def test_total_big_list_of_containers_with_multiple_fields(benchmark):
156+
@benchmark
157+
def total():
158+
Query = GraphQLObjectType('Query', fields={
159+
'allContainers': GraphQLField(
160+
GraphQLList(ContainerType),
161+
resolver=resolve_all_containers
162+
)
163+
})
164+
165+
hello_schema = GraphQLSchema(Query)
166+
source = Source('{ allContainers { x, y, z } }')
167+
ast = parse(source)
168+
result = partial(execute, hello_schema, ast)

setup.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from setuptools import setup, find_packages
2+
from setuptools.command.test import test as TestCommand
23
import sys
34

45
if sys.version_info[0] < 3:
@@ -12,10 +13,39 @@
1213
# the numpy distutils extensions that are used by scikit-learn to recursively
1314
# build the compiled extensions in sub-packages is based on the Python import
1415
# machinery.
15-
builtins.__GRAPHQL_SETUP__ = True
16+
if 'test' not in sys.argv:
17+
builtins.__GRAPHQL_SETUP__ = True
1618

1719
version = __import__('graphql').get_version()
1820

21+
install_requires = [
22+
'six>=1.10.0',
23+
'promise>=0.4.2'
24+
]
25+
26+
tests_requires = [
27+
'pytest==3.0.2',
28+
'pytest-django==2.9.1',
29+
'pytest-cov==2.3.1',
30+
'gevent==1.1rc1',
31+
'six>=1.10.0',
32+
'pytest-benchmark==3.0.0',
33+
'pytest-mock==1.2',
34+
]
35+
36+
class PyTest(TestCommand):
37+
def finalize_options(self):
38+
TestCommand.finalize_options(self)
39+
self.test_args = ['graphql', '-vrsx']
40+
self.test_suite = True
41+
42+
def run_tests(self):
43+
#import here, cause outside the eggs aren't loaded
44+
import pytest
45+
errno = pytest.main(self.test_args)
46+
sys.exit(errno)
47+
48+
1949
setup(
2050
name='graphql-core',
2151
version=version,
@@ -43,18 +73,9 @@
4373

4474
keywords='api graphql protocol rest',
4575
packages=find_packages(exclude=['tests', 'tests_py35']),
46-
install_requires=[
47-
'six>=1.10.0',
48-
'promise>=0.4.2'
49-
],
50-
tests_require=[
51-
'pytest==2.9.2',
52-
'pytest-benchmark',
53-
'pytest-cov',
54-
'gevent==1.1rc1',
55-
'six>=1.10.0',
56-
'pytest-mock'
57-
],
76+
install_requires=install_requires,
77+
tests_require=tests_requires,
78+
cmdclass = {'test': PyTest},
5879
extras_require={
5980
'gevent': [
6081
'gevent==1.1rc1'

0 commit comments

Comments
 (0)