File: test_source.py

package info (click to toggle)
graphql-core 3.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,384 kB
  • sloc: python: 45,812; makefile: 26; sh: 13
file content (114 lines) | stat: -rw-r--r-- 3,595 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import weakref

from typing import cast, Tuple

from pytest import raises

from graphql.language import Source, SourceLocation

from ..utils import dedent


def describe_source():
    def accepts_body_and_name():
        source = Source("foo", "bar")
        assert source.body == "foo"
        assert source.name == "bar"

    def accepts_location_offset():
        location_offset = SourceLocation(2, 3)
        source = Source("", "", location_offset)
        assert source.location_offset is location_offset

    def accepts_tuple_as_location_offset():
        # noinspection PyTypeChecker
        source = Source("", "", (2, 3))  # type: ignore
        assert isinstance(source.location_offset, SourceLocation)
        assert source.location_offset == (2, 3)

    def uses_default_arguments():
        source = Source("")
        assert source.name == "GraphQL request"
        assert isinstance(source.location_offset, SourceLocation)
        assert source.location_offset == (1, 1)

    def can_get_location():
        body = dedent(
            """
            line 1
            line 2
            line 3
            """
        )
        source = Source(body)
        assert source.body == body
        location = source.get_location(body.find("2"))
        assert isinstance(location, SourceLocation)
        assert location == (2, 6)

    def can_be_stringified():
        source = Source("")
        assert str(source) == "<Source name='GraphQL request'>"

        source = Source("", "Custom source name")
        assert str(source) == "<Source name='Custom source name'>"

    def can_be_compared():
        source = Source("foo")
        assert source == source
        assert not source != source
        assert source == "foo"
        assert not source != "foo"
        same_source = Source("foo")
        assert source == same_source
        assert not source != same_source
        different_source = Source("bar")
        assert not source == different_source
        assert source != different_source
        assert not source == "bar"
        assert source != "bar"

    def can_create_weak_reference():
        source = Source("foo")
        ref = weakref.ref(source)
        assert ref() is source

    def can_create_custom_attribute():
        node = Source("foo")
        node.custom = "bar"  # type: ignore
        assert node.custom == "bar"  # type: ignore

    def rejects_invalid_location_offset():
        def create_source(location_offset: Tuple[int, int]) -> Source:
            return Source("", "", cast(SourceLocation, location_offset))

        with raises(TypeError):
            create_source(None)  # type: ignore
        with raises(TypeError):
            create_source(1)  # type: ignore
        with raises(TypeError):
            create_source((1,))  # type: ignore
        with raises(TypeError):
            create_source((1, 2, 3))  # type: ignore

        with raises(
            ValueError,
            match="line in location_offset is 1-indexed and must be positive\\.",
        ):
            create_source((0, 1))
        with raises(
            ValueError,
            match="line in location_offset is 1-indexed and must be positive\\.",
        ):
            create_source((-1, 1))

        with raises(
            ValueError,
            match="column in location_offset is 1-indexed and must be positive\\.",
        ):
            create_source((1, 0))
        with raises(
            ValueError,
            match="column in location_offset is 1-indexed and must be positive\\.",
        ):
            create_source((1, -1))