Skip to content

Commit 37bcf6a

Browse files
committed
Improved backend tests
1 parent 27e21e4 commit 37bcf6a

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

graphql/backend/compiled.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from six import string_types
12
from .base import GraphQLDocument
23

34

@@ -7,6 +8,9 @@ def from_code(cls, schema, code, uptodate=None, extra_namespace=None):
78
"""Creates a GraphQLDocument object from compiled code and the globals. This
89
is used by the loaders and schema to create a document object.
910
"""
11+
if isinstance(code, string_types):
12+
filename = '<document>'
13+
code = compile(code, filename, 'exec')
1014
namespace = {"__file__": code.co_filename}
1115
exec(code, namespace)
1216
if extra_namespace:

graphql/backend/tests/test_base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
from .. import get_default_backend, set_default_backend, GraphQLCoreBackend
3+
4+
5+
def test_get_default_backend_returns_core_by_default():
6+
backend = get_default_backend()
7+
assert isinstance(backend, GraphQLCoreBackend)
8+
9+
10+
def test_set_default_backend():
11+
default_backend = get_default_backend()
12+
new_backend = GraphQLCoreBackend()
13+
assert new_backend != default_backend
14+
set_default_backend(new_backend)
15+
assert get_default_backend() == new_backend
16+
17+
18+
def test_set_default_backend_fails_if_invalid_backend():
19+
default_backend = get_default_backend()
20+
with pytest.raises(Exception) as exc_info:
21+
set_default_backend(object())
22+
assert str(exc_info.value) == 'backend must be an instance of GraphQLBackend.'
23+
assert get_default_backend() == default_backend

graphql/backend/tests/test_cache.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010
from .schema import schema
1111

1212

13-
def test_backend_is_cached_when_needed():
13+
def test_cached_backend():
1414
cached_backend = GraphQLCachedBackend(GraphQLCoreBackend())
1515
document1 = cached_backend.document_from_string(schema, "{ hello }")
1616
document2 = cached_backend.document_from_string(schema, "{ hello }")
1717
assert document1 == document2
18+
19+
20+
def test_cached_backend_with_use_consistent_hash():
21+
cached_backend = GraphQLCachedBackend(GraphQLCoreBackend(), use_consistent_hash=True)
22+
document1 = cached_backend.document_from_string(schema, "{ hello }")
23+
document2 = cached_backend.document_from_string(schema, "{ hello }")
24+
assert document1 == document2
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from ...language.base import parse
2+
from ...utils.ast_to_code import ast_to_code
3+
from ..compiled import GraphQLCompiledDocument
4+
from .schema import schema
5+
6+
7+
def test_compileddocument_from_module_dict():
8+
document_string = '{ hello }'
9+
document_ast = parse(document_string)
10+
document = GraphQLCompiledDocument.from_module_dict(schema, {
11+
'document_string': document_string,
12+
'document_ast': document_ast,
13+
'execute': lambda *_: True
14+
})
15+
assert document.operations_map == {
16+
None: 'query'
17+
}
18+
assert document.document_string == document_string
19+
assert document.document_ast == document_ast
20+
assert document.schema == schema
21+
assert document.execute()
22+
23+
24+
def test_compileddocument_from_code():
25+
document_string = '{ hello }'
26+
document_ast = parse(document_string)
27+
code = '''
28+
# -*- coding: utf-8 -*-
29+
from __future__ import unicode_literals
30+
31+
from graphql.language import ast
32+
from graphql.language.parser import Loc
33+
from graphql.language.source import Source
34+
35+
36+
schema = None
37+
document_string = """{document_string}"""
38+
source = Source(document_string)
39+
40+
41+
def loc(start, end):
42+
return Loc(start, end, source)
43+
44+
document_ast = {document_ast}
45+
46+
def execute(*_):
47+
return True
48+
'''.format(document_string=document_string, document_ast=ast_to_code(document_ast))
49+
document = GraphQLCompiledDocument.from_code(schema, code)
50+
assert document.operations_map == {
51+
None: 'query'
52+
}
53+
assert document.document_string == document_string
54+
assert document.document_ast == document_ast
55+
assert document.schema == schema
56+
assert document.execute()

setup.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@
22
exclude = tests,scripts,setup.py,docs,graphql/execution/executors/asyncio_utils.py
33
max-line-length = 160
44

5+
[coverage:run]
6+
omit=graphql/backend/quiver_cloud.py,tests,*/tests/*
7+
8+
[coverage:report]
9+
omit=graphql/backend/quiver_cloud.py,tests,*/tests/*
10+
511
[bdist_wheel]
612
universal=1

0 commit comments

Comments
 (0)