File: test_structures.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 (129 lines) | stat: -rw-r--r-- 3,184 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from functools import partial

from pytest import raises

from ..scalars import String
from ..structures import List, NonNull
from .utils import MyLazyType


def test_list():
    _list = List(String)
    assert _list.of_type == String
    assert str(_list) == "[String]"


def test_list_with_unmounted_type():
    with raises(Exception) as exc_info:
        List(String())

    assert (
        str(exc_info.value)
        == "List could not have a mounted String() as inner type. Try with List(String)."
    )


def test_list_with_lazy_type():
    MyType = object()
    field = List(lambda: MyType)
    assert field.of_type == MyType


def test_list_with_lazy_partial_type():
    MyType = object()
    field = List(partial(lambda: MyType))
    assert field.of_type == MyType


def test_list_with_string_type():
    field = List("graphene.types.tests.utils.MyLazyType")
    assert field.of_type == MyLazyType


def test_list_inherited_works_list():
    _list = List(List(String))
    assert isinstance(_list.of_type, List)
    assert _list.of_type.of_type == String


def test_list_inherited_works_nonnull():
    _list = List(NonNull(String))
    assert isinstance(_list.of_type, NonNull)
    assert _list.of_type.of_type == String


def test_nonnull():
    nonnull = NonNull(String)
    assert nonnull.of_type == String
    assert str(nonnull) == "String!"


def test_nonnull_with_lazy_type():
    MyType = object()
    field = NonNull(lambda: MyType)
    assert field.of_type == MyType


def test_nonnull_with_lazy_partial_type():
    MyType = object()
    field = NonNull(partial(lambda: MyType))
    assert field.of_type == MyType


def test_nonnull_with_string_type():
    field = NonNull("graphene.types.tests.utils.MyLazyType")
    assert field.of_type == MyLazyType


def test_nonnull_inherited_works_list():
    _list = NonNull(List(String))
    assert isinstance(_list.of_type, List)
    assert _list.of_type.of_type == String


def test_nonnull_inherited_dont_work_nonnull():
    with raises(Exception) as exc_info:
        NonNull(NonNull(String))

    assert (
        str(exc_info.value)
        == "Can only create NonNull of a Nullable GraphQLType but got: String!."
    )


def test_nonnull_with_unmounted_type():
    with raises(Exception) as exc_info:
        NonNull(String())

    assert (
        str(exc_info.value)
        == "NonNull could not have a mounted String() as inner type. Try with NonNull(String)."
    )


def test_list_comparasion():
    list1 = List(String)
    list2 = List(String)
    list3 = List(None)

    list1_argskwargs = List(String, None, b=True)
    list2_argskwargs = List(String, None, b=True)

    assert list1 == list2
    assert list1 != list3
    assert list1_argskwargs == list2_argskwargs
    assert list1 != list1_argskwargs


def test_nonnull_comparasion():
    nonnull1 = NonNull(String)
    nonnull2 = NonNull(String)
    nonnull3 = NonNull(None)

    nonnull1_argskwargs = NonNull(String, None, b=True)
    nonnull2_argskwargs = NonNull(String, None, b=True)

    assert nonnull1 == nonnull2
    assert nonnull1 != nonnull3
    assert nonnull1_argskwargs == nonnull2_argskwargs
    assert nonnull1 != nonnull1_argskwargs