Skip to content

Commit

Permalink
Improved backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jun 5, 2018
1 parent 27e21e4 commit 37bcf6a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
4 changes: 4 additions & 0 deletions graphql/backend/compiled.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from six import string_types
from .base import GraphQLDocument


Expand All @@ -7,6 +8,9 @@ def from_code(cls, schema, code, uptodate=None, extra_namespace=None):
"""Creates a GraphQLDocument object from compiled code and the globals. This
is used by the loaders and schema to create a document object.
"""
if isinstance(code, string_types):
filename = '<document>'
code = compile(code, filename, 'exec')
namespace = {"__file__": code.co_filename}
exec(code, namespace)
if extra_namespace:
Expand Down
23 changes: 23 additions & 0 deletions graphql/backend/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
from .. import get_default_backend, set_default_backend, GraphQLCoreBackend


def test_get_default_backend_returns_core_by_default():
backend = get_default_backend()
assert isinstance(backend, GraphQLCoreBackend)


def test_set_default_backend():
default_backend = get_default_backend()
new_backend = GraphQLCoreBackend()
assert new_backend != default_backend
set_default_backend(new_backend)
assert get_default_backend() == new_backend


def test_set_default_backend_fails_if_invalid_backend():
default_backend = get_default_backend()
with pytest.raises(Exception) as exc_info:
set_default_backend(object())
assert str(exc_info.value) == 'backend must be an instance of GraphQLBackend.'
assert get_default_backend() == default_backend
9 changes: 8 additions & 1 deletion graphql/backend/tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
from .schema import schema


def test_backend_is_cached_when_needed():
def test_cached_backend():
cached_backend = GraphQLCachedBackend(GraphQLCoreBackend())
document1 = cached_backend.document_from_string(schema, "{ hello }")
document2 = cached_backend.document_from_string(schema, "{ hello }")
assert document1 == document2


def test_cached_backend_with_use_consistent_hash():
cached_backend = GraphQLCachedBackend(GraphQLCoreBackend(), use_consistent_hash=True)
document1 = cached_backend.document_from_string(schema, "{ hello }")
document2 = cached_backend.document_from_string(schema, "{ hello }")
assert document1 == document2
56 changes: 56 additions & 0 deletions graphql/backend/tests/test_compileddocument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from ...language.base import parse
from ...utils.ast_to_code import ast_to_code
from ..compiled import GraphQLCompiledDocument
from .schema import schema


def test_compileddocument_from_module_dict():
document_string = '{ hello }'
document_ast = parse(document_string)
document = GraphQLCompiledDocument.from_module_dict(schema, {
'document_string': document_string,
'document_ast': document_ast,
'execute': lambda *_: True
})
assert document.operations_map == {
None: 'query'
}
assert document.document_string == document_string
assert document.document_ast == document_ast
assert document.schema == schema
assert document.execute()


def test_compileddocument_from_code():
document_string = '{ hello }'
document_ast = parse(document_string)
code = '''
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from graphql.language import ast
from graphql.language.parser import Loc
from graphql.language.source import Source
schema = None
document_string = """{document_string}"""
source = Source(document_string)
def loc(start, end):
return Loc(start, end, source)
document_ast = {document_ast}
def execute(*_):
return True
'''.format(document_string=document_string, document_ast=ast_to_code(document_ast))
document = GraphQLCompiledDocument.from_code(schema, code)
assert document.operations_map == {
None: 'query'
}
assert document.document_string == document_string
assert document.document_ast == document_ast
assert document.schema == schema
assert document.execute()
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
exclude = tests,scripts,setup.py,docs,graphql/execution/executors/asyncio_utils.py
max-line-length = 160

[coverage:run]
omit=graphql/backend/quiver_cloud.py,tests,*/tests/*

[coverage:report]
omit=graphql/backend/quiver_cloud.py,tests,*/tests/*

[bdist_wheel]
universal=1

0 comments on commit 37bcf6a

Please sign in to comment.