diff --git a/graphene_mongo/tests/test_query.py b/graphene_mongo/tests/test_query.py index 326606cc..c8d3adef 100644 --- a/graphene_mongo/tests/test_query.py +++ b/graphene_mongo/tests/test_query.py @@ -5,6 +5,7 @@ from graphene.relay import Node from .models import Article, Editor, EmbeddedArticle, Reporter +from .utils import with_local_registry from ..fields import MongoengineConnectionField from ..types import MongoengineObjectType @@ -27,6 +28,7 @@ def setup_fixtures(): setup_fixtures() +@with_local_registry def test_should_query_editor_well(): class EditorType(MongoengineObjectType): class Meta: @@ -73,6 +75,7 @@ def resolve_editors(self, *args, **kwargs): assert all(item in result.data['editors'] for item in expected['editors']) +@with_local_registry def test_should_query_reporter_well(): class ArticleType(MongoengineObjectType): class Meta: @@ -119,13 +122,17 @@ def resolve_reporter(self, *args, **kwargs): assert not result.errors assert dict(result.data['reporter']) == expected['reporter'] + +@with_local_registry def test_should_node(): class ArticleNode(MongoengineObjectType): + class Meta: model = Article interfaces = (Node,) class ReporterNode(MongoengineObjectType): + class Meta: model = Reporter interfaces = (Node,) @@ -180,6 +187,8 @@ def resolve_reporter(self, *args, **kwargs): assert not result.errors assert dict(result.data['reporter']) == expected['reporter'] + +@with_local_registry def test_should_connection_field(): class EditorNode(MongoengineObjectType): @@ -226,9 +235,62 @@ class Query(graphene.ObjectType): assert not result.errors assert dict(result.data['allEditors']) == expected['allEditors'] -# TODO: + +@with_local_registry def test_should_mutate_well(): - pass + class ArticleNode(MongoengineObjectType): + + class Meta: + model = Article + interfaces = (Node,) + + + class CreateArticle(graphene.Mutation): + + class Arguments: + headline = graphene.String() + + article = graphene.Field(ArticleNode) + + def mutate(self, info, headline): + article = Article( + headline=headline + ) + article.save() + + return CreateArticle(article=article) + + + class Query(graphene.ObjectType): + node = Node.Field() + + + class Mutation(graphene.ObjectType): + + create_article = CreateArticle.Field() + + query = ''' + mutation ArticleCreator { + createArticle( + headline: "My Article" + ) { + article { + headline + } + } + } + ''' + expected = { + 'createArticle': { + 'article': { + 'headline': 'My Article' + } + } + } + schema = graphene.Schema(query=Query, mutation=Mutation) + result = schema.execute(query) + assert not result.errors + assert result.data == expected # TODO: def test_should_filter(): diff --git a/graphene_mongo/tests/test_types.py b/graphene_mongo/tests/test_types.py index 293f2d14..982489e7 100644 --- a/graphene_mongo/tests/test_types.py +++ b/graphene_mongo/tests/test_types.py @@ -7,6 +7,7 @@ from ..types import MongoengineObjectType from .models import Article from .models import Reporter +from .utils import with_local_registry registry.reset_global_registry() @@ -80,21 +81,6 @@ class Meta: assert 'valid Mongoengine Model' in str(excinfo.value) -def with_local_registry(func): - def inner(*args, **kwargs): - old = registry.get_global_registry() - registry.reset_global_registry() - try: - retval = func(*args, **kwargs) - except Exception as e: - registry.registry = old - raise e - else: - registry.registry = old - return retval - return inner - - @with_local_registry def test_mongoengine_objecttype_only_fields(): class A(MongoengineObjectType): diff --git a/graphene_mongo/tests/utils.py b/graphene_mongo/tests/utils.py new file mode 100644 index 00000000..4ea749bc --- /dev/null +++ b/graphene_mongo/tests/utils.py @@ -0,0 +1,17 @@ +from .. import registry + + +def with_local_registry(func): + def inner(*args, **kwargs): + old = registry.get_global_registry() + registry.reset_global_registry() + try: + retval = func(*args, **kwargs) + except Exception as e: + registry.registry = old + raise e + else: + registry.registry = old + return retval + return inner +