File: test_json.py

package info (click to toggle)
python-graphene 3.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,935; makefile: 214; sh: 18
file content (80 lines) | stat: -rw-r--r-- 2,347 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
from ..json import JSONString
from ..objecttype import ObjectType
from ..schema import Schema


class Query(ObjectType):
    json = JSONString(input=JSONString())

    def resolve_json(self, info, input):
        return input


schema = Schema(query=Query)


def test_jsonstring_query():
    json_value = '{"key": "value"}'

    json_value_quoted = json_value.replace('"', '\\"')
    result = schema.execute("""{ json(input: "%s") }""" % json_value_quoted)
    assert not result.errors
    assert result.data == {"json": json_value}

    result = schema.execute("""{ json(input: "{}") }""")
    assert not result.errors
    assert result.data == {"json": "{}"}


def test_jsonstring_query_variable():
    json_value = '{"key": "value"}'

    result = schema.execute(
        """query Test($json: JSONString){ json(input: $json) }""",
        variables={"json": json_value},
    )
    assert not result.errors
    assert result.data == {"json": json_value}


def test_jsonstring_optional_uuid_input():
    """
    Test that we can provide a null value to an optional input
    """
    result = schema.execute("{ json(input: null) }")
    assert not result.errors
    assert result.data == {"json": None}


def test_jsonstring_invalid_query():
    """
    Test that if an invalid type is provided we get an error
    """
    result = schema.execute("{ json(input: 1) }")
    assert result.errors == [
        {"message": "Expected value of type 'JSONString', found 1."},
    ]

    result = schema.execute("{ json(input: {}) }")
    assert result.errors == [
        {"message": "Expected value of type 'JSONString', found {}."},
    ]

    result = schema.execute('{ json(input: "a") }')
    assert result.errors == [
        {
            "message": "Expected value of type 'JSONString', found \"a\"; "
            "Badly formed JSONString: Expecting value: line 1 column 1 (char 0)",
        },
    ]

    result = schema.execute("""{ json(input: "{\\'key\\': 0}") }""")
    assert result.errors == [
        {"message": "Syntax Error: Invalid character escape sequence: '\\''."},
    ]

    result = schema.execute("""{ json(input: "{\\"key\\": 0,}") }""")
    assert len(result.errors) == 1
    assert result.errors[0].message.startswith(
        'Expected value of type \'JSONString\', found "{\\"key\\": 0,}"; Badly formed JSONString:'
    )