File: test_uuid.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 (83 lines) | stat: -rw-r--r-- 2,413 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
from ..objecttype import ObjectType
from ..schema import Schema
from ..uuid import UUID
from ..structures import NonNull


class Query(ObjectType):
    uuid = UUID(input=UUID())
    required_uuid = UUID(input=NonNull(UUID), required=True)

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

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


schema = Schema(query=Query)


def test_uuidstring_query():
    uuid_value = "dfeb3bcf-70fd-11e7-a61a-6003088f8204"
    result = schema.execute("""{ uuid(input: "%s") }""" % uuid_value)
    assert not result.errors
    assert result.data == {"uuid": uuid_value}


def test_uuidstring_query_variable():
    uuid_value = "dfeb3bcf-70fd-11e7-a61a-6003088f8204"

    result = schema.execute(
        """query Test($uuid: UUID){ uuid(input: $uuid) }""",
        variables={"uuid": uuid_value},
    )
    assert not result.errors
    assert result.data == {"uuid": uuid_value}


def test_uuidstring_invalid_argument():
    uuid_value = {"not": "a string"}

    result = schema.execute(
        """query Test($uuid: UUID){ uuid(input: $uuid) }""",
        variables={"uuid": uuid_value},
    )
    assert result.errors
    assert len(result.errors) == 1
    assert (
        result.errors[0].message
        == "Variable '$uuid' got invalid value {'not': 'a string'}; UUID cannot represent value: {'not': 'a string'}"
    )


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


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

    result = schema.execute('{ uuid(input: "a") }')
    assert result.errors
    assert len(result.errors) == 1
    assert (
        result.errors[0].message
        == "Expected value of type 'UUID', found \"a\"; badly formed hexadecimal UUID string"
    )

    result = schema.execute("{ requiredUuid(input: null) }")
    assert result.errors
    assert len(result.errors) == 1
    assert result.errors[0].message == "Expected value of type 'UUID!', found null."