def prints_line_numbers_with_correct_padding():
        single_digit = GraphQLError(
            "Single digit line number with no padding",
            None,
            Source("*", "Test", SourceLocation(9, 1)),
            [0],
        )
        assert print_error(single_digit) == dedent("""
            Single digit line number with no padding

            Test (9:1)
            9: *
               ^
            """)

        double_digit = GraphQLError(
            "Left padded first line number",
            None,
            Source("*\n", "Test", SourceLocation(9, 1)),
            [0],
        )

        assert print_error(double_digit) == dedent("""
            Left padded first line number

            Test (9:1)
             9: *
                ^
            10:\x20
            """)
Example #2
0
    def prints_single_digit_line_number_with_no_padding():
        result = print_source_location(
            Source("*", "Test", SourceLocation(9, 1)), SourceLocation(1, 1))

        assert result + "\n" == dedent("""
            Test:9:1
            9 | *
              | ^
            """)
Example #3
0
    def prints_line_numbers_with_correct_padding():
        result = print_source_location(
            Source("*\n", "Test", SourceLocation(9, 1)), SourceLocation(1, 1))

        assert result + "\n" == dedent("""
            Test:9:1
             9 | *
               | ^
            10 |
            """)
Example #4
0
    def prints_minified_documents():
        minified_source = Source(
            "query SomeMiniFiedQueryWithErrorInside("
            "$foo:String!=FIRST_ERROR_HERE$bar:String)"
            "{someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE)"
            "{fieldA fieldB{fieldC fieldD...on THIRD_ERROR_HERE}}}")

        first_location = print_source_location(
            minified_source,
            SourceLocation(1,
                           minified_source.body.index("FIRST_ERROR_HERE") + 1),
        )
        assert first_location + "\n" == dedent("""
            GraphQL request:1:53
            1 | query SomeMiniFiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
              |                                                     ^
              | {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
            """

                                               # noqa: E501
                                               )

        second_location = print_source_location(
            minified_source,
            SourceLocation(1,
                           minified_source.body.index("SECOND_ERROR_HERE") +
                           1),
        )
        assert second_location + "\n" == dedent("""
            GraphQL request:1:114
            1 | query SomeMiniFiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
              | {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
              |                                  ^
              | ..on THIRD_ERROR_HERE}}}
            """

                                                # noqa: E501
                                                )

        third_location = print_source_location(
            minified_source,
            SourceLocation(1,
                           minified_source.body.index("THIRD_ERROR_HERE") + 1),
        )
        assert third_location + "\n" == dedent("""
            GraphQL request:1:166
            1 | query SomeMiniFiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
              | {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
              | ..on THIRD_ERROR_HERE}}}
              |      ^
            """

                                               # noqa: E501
                                               )
    def pass_error_from_resolver_wrapped_as_located_graphql_error():
        def resolve(_obj, _info):
            raise ValueError("Some error")

        schema = _test_schema(GraphQLField(GraphQLString, resolve=resolve))
        result = execute_sync(schema, parse("{ test }"))

        assert result == (
            {"test": None},
            [{"message": "Some error", "locations": [(1, 3)], "path": ["test"]}],
        )

        assert result.errors is not None
        error = result.errors[0]
        assert isinstance(error, GraphQLError)
        assert str(error) == "Some error\n\nGraphQL request:1:3\n1 | { test }\n  |   ^"
        assert error.positions == [2]
        locations = error.locations
        assert locations == [(1, 3)]
        location = locations[0]
        assert isinstance(location, SourceLocation)
        assert location == SourceLocation(1, 3)
        original_error = error.original_error
        assert isinstance(original_error, ValueError)
        assert str(original_error) == "Some error"
Example #6
0
    def updates_column_numbers_in_error_for_file_context():
        source = Source("?", "foo.js", SourceLocation(1, 5))
        with raises(GraphQLSyntaxError) as exc_info:
            Lexer(source).advance()
        assert str(exc_info.value) == dedent("""
            Syntax Error: Cannot parse the unexpected character '?'.

            foo.js (1:5)
            1:     ?
                   ^
            """)
    def updates_column_numbers_in_error_for_file_context():
        source = Source("~", "foo.js", SourceLocation(1, 5))
        with raises(GraphQLSyntaxError) as exc_info:
            Lexer(source).advance()
        assert str(exc_info.value) == dedent(
            """
            Syntax Error: Unexpected character: '~'.

            foo.js:1:5
            1 |     ~
              |     ^
            """
        )
Example #8
0
    def updates_line_numbers_in_error_for_file_context():
        s = "\n\n     ?\n\n"
        source = Source(s, "foo.js", SourceLocation(11, 12))
        with raises(GraphQLSyntaxError) as exc_info:
            Lexer(source).advance()
        assert str(exc_info.value) == dedent("""
            Syntax Error: Cannot parse the unexpected character '?'.

            foo.js (13:6)
            12:\x20
            13:      ?
                     ^
            14:\x20
            """)
Example #9
0
    def prints_line_numbers_with_correct_padding():
        single_digit = GraphQLError(
            'Single digit line number with no padding', None,
            Source('*', 'Test', SourceLocation(9, 1)), [0])
        assert print_error(single_digit) == dedent("""
            Single digit line number with no padding

            Test (9:1)
            9: *
               ^
            """)

        double_digit = GraphQLError(
            'Left padded first line number', None,
            Source('*\n', 'Test', SourceLocation(9, 1)), [0])

        assert print_error(double_digit) == dedent("""
            Left padded first line number

            Test (9:1)
             9: *
                ^
            10:\x20
            """)
Example #10
0
    def updates_line_numbers_in_error_for_file_context():
        s = "\n\n     ~\n\n"
        source = Source(s, "foo.js", SourceLocation(11, 12))
        with raises(GraphQLSyntaxError) as exc_info:
            Lexer(source).advance()
        assert str(exc_info.value) == dedent(
            """
            Syntax Error: Unexpected character: '~'.

            foo.js:13:6
            12 |
            13 |      ~
               |      ^
            14 |
            """
        )
Example #11
0
 def accepts_location_offset():
     location_offset = SourceLocation(2, 3)
     source = Source("", "", location_offset)
     assert source.location_offset is location_offset