async def test_tartiflette_execute_scalar_type_advanced( input_sdl, resolver_response, expected): schema_sdl = """ scalar Date type Query {{ testField: {} }} """.format(input_sdl) def from_date_to_str(datetime): try: return datetime.isoformat() except AttributeError: return None ttftt = Tartiflette(schema_sdl) ttftt.schema.types["Date"].coerce_output = from_date_to_str @Resolver("Query.testField", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): return resolver_response result = await ttftt.execute(""" query Test{ testField } """) assert expected == result
async def test_tartiflette_execute_object_type_output(): schema_sdl = """ type Test { field1: String } type Query { objectTest: Test } """ ttftt = Tartiflette(schema_sdl) @Resolver("Query.objectTest", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): return {"field1": "Test"} result = await ttftt.execute(""" query Test{ objectTest { field1 } } """) assert """{"data":{"objectTest":{"field1":"Test"}}}""" == result
async def test_issue21_okayquery(query, expected, varis): import json from tartiflette.tartiflette import Tartiflette class default_resolver(Mock): async def __call__(self, ctx, exe): super(default_resolver, self).__call__(ctx, exe) return {"iam": exe.name, "args": exe.arguments} field = Mock() field.gql_type = GQLTypeMock(name="Test") field.name = "test" field.resolver = default_resolver() def get_field_by_name(_): return field sdm = Mock() sdm.query_type = "Query" sdm.get_field_by_name = get_field_by_name sdm.types = { "Query": GQLTypeMock(name="Query"), "Test": GQLTypeMock(name="Test"), } ttftt = Tartiflette(schema=sdm) results = await ttftt.execute(query, context={}, variables=varis) assert json.loads(results) == json.loads(expected)
async def test_tartiflette_execute_enum_type_output(): schema_sdl = """ enum Test { Value1 Value2 Value3 } type Query { enumTest: Test } """ ttftt = Tartiflette(schema_sdl) @Resolver("Query.enumTest", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): return "Value1" result = await ttftt.execute(""" query Test{ enumTest } """) assert """{"data":{"enumTest":"Value1"}}""" == result
async def test_tartiflette_execute_hello_world(): schema_sdl = """ type Query { hello: String! } """ ttftt = Tartiflette(schema_sdl) @Resolver("Query.hello", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): return "world" result = await ttftt.execute(""" query Test{ hello } """) assert """{"data":{"hello":"world"}}""" == result # Try twice to be sure everything works mutliple times result = await ttftt.execute(""" query Test{ hello } """) assert """{"data":{"hello":"world"}}""" == result
async def test_tartiflette_execute_enum_type_advanced(input_sdl, resolver_response, expected): schema_sdl = """ enum Test {{ Value1 Value2 Value3 }} type Query {{ testField: {} }} """.format(input_sdl) ttftt = Tartiflette(schema_sdl) @Resolver("Query.testField", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): return resolver_response result = await ttftt.execute(""" query Test{ testField } """) assert expected == result
async def test_tartiflette_execute_nested_error(): schema_sdl = """ scalar Date type Obj { deep: Nested # Try [Nested!] later } type Nested { lastUpdate: [Date!] } type Query { test: Obj } """ def from_date_to_str(datetime): return datetime.isoformat() def from_str_to_date(datetime_str): return datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%S.%fZ") ttftt = Tartiflette(schema_sdl) ttftt.schema.types["Date"].coerce_output = from_date_to_str ttftt.schema.types["Date"].coerce_input = from_str_to_date @Resolver("Nested.lastUpdate", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): return [ datetime(year=2018, month=4, day=19, hour=14, minute=57, second=38), None ] result = await ttftt.execute(""" query Test{ test { deep { lastUpdate } } } """) assert """{"data":{"test":{"deep":{"lastUpdate":["2018-04-19T14:57:38"]}}},"errors":[{"message":"Invalid value (value: None) for field `lastUpdate` of type `[Date!]`","path":["test","deep","lastUpdate",1],"locations":[{"line":1,"column":68}]}]}""" == result
async def test_tartiflette_execute_basic(): schema_sdl = """ schema { query: RootQuery } type RootQuery { defaultField: Int testField: Test } type Test { field: String } """ ttftt = Tartiflette(schema_sdl) mock_one = Mock() mock_two = Mock() @Resolver("Test.field", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): mock_one() return @Resolver("RootQuery.defaultField", schema=ttftt.schema) async def func_default_query_resolver(*args, **kwargs): mock_two() return result = await ttftt.execute(""" query Test{ testField { field } } """) assert result == """{"data":{"testField":{"field":null}}}""" assert mock_one.called is True assert mock_two.called is False
async def test_tartiflette_nested_resolvers(): schema_sdl = """ type Query { rootField: RootType } type RootType { nestedField: NestedType } type NestedType { endField: String } """ ttftt = Tartiflette(schema_sdl) @Resolver("Query.rootField", schema=ttftt.schema) async def func_resolver(request_ctx, execution_data): return {"nestedField": "Nested ?"} @Resolver("RootType.nestedField", schema=ttftt.schema) async def func_resolver(request_ctx, execution_data): return {"endField": "Another"} @Resolver("NestedType.endField", schema=ttftt.schema) async def func_resolver(request_ctx, execution_data): return "Test" result = await ttftt.execute(""" query Test{ rootField { nestedField { endField } } } """) assert result == """{"data":{"rootField":{"nestedField":{"endField":"Test"}}}}"""
async def test_get_field_by_name_call_order(query, varis, expected): from tartiflette.tartiflette import Tartiflette async def _resolver(ctx, exec_data): return {} field = Mock() field.name = "test" field.gql_type = GQLTypeMock(name="Object") field.resolver = _resolver sdm = Mock() sdm.query_type = "Query" sdm.get_field_by_name = Mock(return_value=field) sdm.types = { "Query": GQLTypeMock(name="Query"), } ttftt = Tartiflette(schema=sdm) await ttftt.execute(query, context={}, variables=varis) sdm.get_field_by_name.assert_has_calls(expected, any_order=False)
async def test_tartiflette_execute_object_type_unknown_field(): schema_sdl = """ type Post { content: Content meta_creator: String } type Content { title: String } type Query { posts: [Post!] } """ ttftt = Tartiflette(schema_sdl) mock_call = Mock() @Resolver("Content.title", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): mock_call() return "Test" @Resolver("Post.content", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): return {"title": "Stuff"} result = await ttftt.execute(""" query Test{ posts { content { title } } } """)
async def test_tartiflette_execute_scalar_type_output(): schema_sdl = """ scalar Date type Query { lastUpdate: Date } """ def from_date_to_str(datetime): return datetime.isoformat() def from_str_to_date(datetime_str): return datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%S.%fZ") ttftt = Tartiflette(schema_sdl) ttftt.schema.types["Date"].coerce_output = from_date_to_str ttftt.schema.types["Date"].coerce_input = from_str_to_date @Resolver("Query.lastUpdate", schema=ttftt.schema) async def func_field_resolver(*args, **kwargs): return datetime(year=2018, month=4, day=19, hour=14, minute=57, second=38) result = await ttftt.execute(""" query Test{ lastUpdate } """) assert """{"data":{"lastUpdate":"2018-04-19T14:57:38"}}""" == result
async def test_calling_get_field_by_name_with_correct_value(): class default_resolver(Mock): async def __call__(self, ctx, exe): super(default_resolver, self).__call__(ctx, exe) try: return getattr(exe.parent_result, exe.name) except: return {} from tartiflette.tartiflette import Tartiflette class resolver_a(Mock): async def __call__(self, ctx, exedata): super(resolver_a, self).__call__(ctx, exedata) return [{"id": 1}, {"id": 2}] class resolver_b(Mock): class resolver_b_result(): def __init__(self): self.C = {"id": "b.c"} def __repr__(self): return "IAmABResults" def collect_value(self): return {"C": self.C} def __init__(self, *args, **kwargs): super(resolver_b, self).__init__(*args, **kwargs) self.rtrn = resolver_b.resolver_b_result() async def __call__(self, ctx, exedata): super(resolver_b, self).__call__(ctx, exedata) return self.rtrn class resolver_d(Mock): async def __call__(self, ctx, exedata): super(resolver_d, self).__call__(ctx, exedata) return "ValueD" field_a = Mock() field_a.resolver = resolver_a() field_a.gql_type = GQLTypeMock(name="Test") field_a.name = "test" field_b = Mock() field_b.resolver = resolver_b() field_b.gql_type = GQLTypeMock(name="Test") field_b.name = "test" field_d = Mock() field_d.resolver = resolver_d() field_d.gql_type = GQLTypeMock(name="Test") field_d.name = "test" default_field = Mock() default_field.resolver = default_resolver() default_field.gql_type = GQLTypeMock(name="Test") default_field.name = "test" def get_field(name): fields = {"Query.A": field_a, "Test.B": field_b, "Test.D": field_d} return fields.get(name, default_field) sdm = Mock() sdm.query_type = "Query" sdm.get_field_by_name = get_field sdm.types = { "Query": GQLTypeMock(name="Query"), "Test": GQLTypeMock(name="Test"), } ttftt = Tartiflette(schema=sdm) r = await ttftt.execute(""" query a_request { A { B { C { K } } D E F { H { I } } } } """, context={}, variables={}) field_a.resolver.assert_has_calls( [ call({}, ExecutionData(parent_result={}, path=['A'], arguments={}, name='A', field=field_a, location=Location(line=1, column=40, line_end=1, column_end=313, context=''))) ], any_order=True, ) field_b.resolver.assert_has_calls([ call({}, ExecutionData( parent_result={"id": 1}, path=['A', 'B', 0], arguments={}, name='B', field=field_b, location=Location( line=1, column=60, line_end=1, column_end=153, context=''))), call({}, ExecutionData( parent_result={"id": 2}, path=['A', 'B', 1], arguments={}, name='B', field=field_b, location=Location( line=1, column=60, line_end=1, column_end=153, context=''))) ], any_order=True) field_d.resolver.assert_has_calls([ call({}, ExecutionData(parent_result={"id": 1}, path=['A', 'D', 0], arguments={}, name='D', field=field_d, location=Location(line=1, column=170, line_end=1, column_end=171, context=''))), call({}, ExecutionData(parent_result={"id": 2}, path=['A', 'D', 1], arguments={}, name='D', field=field_d, location=Location(line=1, column=170, line_end=1, column_end=171, context=''))) ], any_order=True) default_field.resolver.assert_has_calls([ call({}, ExecutionData(parent_result={"id": 1}, path=['A', 'F', 0], arguments={}, name='F', field=default_field, location=Location(line=1, column=206, line_end=1, column_end=299, context=''))), call({}, ExecutionData(parent_result={"id": 1}, path=['A', 'E', 0], arguments={}, name='E', field=default_field, location=Location(line=1, column=188, line_end=1, column_end=189, context=''))), call({}, ExecutionData(parent_result={"id": 2}, path=['A', 'F', 1], arguments={}, name='F', field=default_field, location=Location(line=1, column=206, line_end=1, column_end=299, context=''))), call({}, ExecutionData(parent_result={"id": 2}, path=['A', 'E', 1], arguments={}, name='E', field=default_field, location=Location(line=1, column=188, line_end=1, column_end=189, context=''))), call({}, ExecutionData(parent_result=field_b.resolver.rtrn, path=['A', 'B', 'C', 0], arguments={}, name='C', field=default_field, location=Location(line=1, column=84, line_end=1, column_end=135, context=''))), call({}, ExecutionData(parent_result=field_b.resolver.rtrn, path=['A', 'B', 'C', 1], arguments={}, name='C', field=default_field, location=Location(line=1, column=84, line_end=1, column_end=135, context=''))), call({}, ExecutionData(parent_result={"id": "b.c"}, path=['A', 'B', 'C', 'K', 0], arguments={}, name='K', field=default_field, location=Location(line=1, column=112, line_end=1, column_end=113, context=''))), call({}, ExecutionData(parent_result={"id": "b.c"}, path=['A', 'B', 'C', 'K', 1], arguments={}, name='K', field=default_field, location=Location(line=1, column=112, line_end=1, column_end=113, context=''))), call({}, ExecutionData(parent_result={}, path=['A', 'F', 'H', 1], arguments={}, name='H', field=default_field, location=Location(line=1, column=230, line_end=1, column_end=281, context=''))), call({}, ExecutionData(parent_result={}, path=['A', 'F', 'H', 0], arguments={}, name='H', field=default_field, location=Location(line=1, column=230, line_end=1, column_end=281, context=''))), call({}, ExecutionData(parent_result={}, path=['A', 'F', 'H', 'I', 1], arguments={}, name='I', field=default_field, location=Location(line=1, column=258, line_end=1, column_end=259, context=''))), call({}, ExecutionData(parent_result={}, path=['A', 'F', 'H', 'I', 0], arguments={}, name='I', field=default_field, location=Location(line=1, column=258, line_end=1, column_end=259, context=''))), ], any_order=True)