Esempio n. 1
0
def test_parser_visitor__on_operation_definition_in_not_unique(
        a_visitor, an_element):
    an_element.name = "getName"
    an_element.get_location = Mock(return_value=Location(2, 1, 2, 2))

    a_visitor._named_operations = {
        "getName": NodeDefinition(None, None, Location(1, 1, 1, 2), "getName")
    }

    a_visitor._on_operation_definition_in(an_element)

    assert len(a_visitor.exceptions) == 1
    assert isinstance(a_visitor.exceptions[0], NotUniqueOperationName)
    assert (str(a_visitor.exceptions[0]) ==
            "Operation name < getName > should be unique.")
    assert a_visitor.exceptions[0].locations == [
        Location(1, 1, 1, 2),
        Location(2, 1, 2, 2),
    ]
Esempio n. 2
0
 def get_location(self):
     location = self._ffi.new("struct GraphQLAstLocation *")
     self._lib.graphql_node_get_location(
         self._ffi.cast("struct GraphQLAstNode *", self._internal_element),
         location
     )
     return Location(
         location.beginLine, location.beginColumn,
         location.endLine, location.endColumn,
     )
Esempio n. 3
0
def test_add_errors_to_execution_context(
    raw_exception, expected_messages, expected_original_errors
):
    execution_context = ExecutionContext()

    location = Location(line=1, column=1)
    path = ["error", "path"]

    _add_errors_to_execution_context(
        execution_context, raw_exception, path, location
    )

    assert len(execution_context.errors) == len(expected_messages)

    for error, expected_message, expected_original_error in zip(
        execution_context.errors, expected_messages, expected_original_errors
    ):
        assert isinstance(error, GraphQLError)
        assert error.message == expected_message
        assert type(error.original_error) is expected_original_error
Esempio n. 4
0
def test_on_selection_set_in(a_visitor, an_element, operation_type,
                             selections_size, depth, has_error):
    location = Location(1, 1, 1, 2)
    a_visitor._internal_ctx.field_path = []
    for i in range(0, depth):
        a_visitor._internal_ctx.field_path.append(str(i))

    a_visitor._internal_ctx.operation = Mock(location=location)
    a_visitor._internal_ctx.operation.type = operation_type
    an_element.get_selections_size = Mock(return_value=selections_size)

    a_visitor._on_selection_set_in(an_element)

    if not has_error:
        assert a_visitor.exceptions == []
    else:
        assert len(a_visitor.exceptions) == 1
        assert isinstance(a_visitor.exceptions[0],
                          MultipleRootNodeOnSubscriptionOperation)
        assert (str(a_visitor.exceptions[0]) ==
                "Subscription operations must have exactly one root field.")
        assert a_visitor.exceptions[0].locations == [location]
Esempio n. 5
0
 [
     (
         "Init TartifletteError",
         {},
         {},
         {
             "message": "Init TartifletteError",
             "path": None,
             "locations": [],
         },
     ),
     (
         "Init TartifletteError",
         {
             "path": ["init", "path"],
             "locations": [Location(line=1, column=1)],
             "user_message": "User TartifletteError",
         },
         {},
         {
             "message": "User TartifletteError",
             "path": ["init", "path"],
             "locations": [{
                 "line": 1,
                 "column": 1
             }],
         },
     ),
     (
         "Init TartifletteError",
         {},
Esempio n. 6
0
    assert isinstance(a_visitor.exceptions[0], NotUniqueOperationName)
    assert (str(a_visitor.exceptions[0]) ==
            "Operation name < getName > should be unique.")
    assert a_visitor.exceptions[0].locations == [
        Location(1, 1, 1, 2),
        Location(2, 1, 2, 2),
    ]


@pytest.mark.parametrize(
    "named_operations,anonymous_operations,nb_errors",
    [
        (
            {
                "getName":
                NodeDefinition(None, None, Location(1, 1, 1, 2), "getName")
            },
            [],
            0,
        ),
        (
            {
                "getName":
                NodeDefinition(None, None, Location(1, 1, 1, 2), "getName")
            },
            [NodeDefinition(None, None, Location(2, 1, 2, 2), None)],
            1,
        ),
        (
            {},
            [
Esempio n. 7
0
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)