File: test_attributes.py

package info (click to toggle)
strawberry-graphql-django 0.78.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,624 kB
  • sloc: python: 31,895; makefile: 24; sh: 21
file content (197 lines) | stat: -rw-r--r-- 4,890 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import textwrap
from typing import TYPE_CHECKING, cast

import strawberry
from django.db import models
from django.test import override_settings
from strawberry import BasePermission, auto, relay
from strawberry.types import get_object_definition

import strawberry_django
from strawberry_django.settings import strawberry_django_settings

if TYPE_CHECKING:
    from strawberry_django.fields.field import StrawberryDjangoField


class FieldAttributeModel(models.Model):
    field = models.CharField(max_length=50)


def test_default_django_name():
    @strawberry_django.type(FieldAttributeModel)
    class Type:
        field: auto
        field2: auto = strawberry_django.field(field_name="field")

    assert [
        (f.name, cast("StrawberryDjangoField", f).django_name)
        for f in get_object_definition(Type, strict=True).fields
    ] == [
        ("field", "field"),
        ("field2", "field"),
    ]


def test_field_permission_classes():
    class TestPermission(BasePermission):
        def has_permission(self, source, info, **kwargs):
            return True

    @strawberry_django.type(FieldAttributeModel)
    class Type:
        field: auto = strawberry.field(permission_classes=[TestPermission])

        @strawberry.field(permission_classes=[TestPermission])
        def custom_resolved_field(self) -> str:
            return self.field

    assert sorted(
        [
            (f.name, f.permission_classes)
            for f in get_object_definition(Type, strict=True).fields
        ],
    ) == sorted(
        [
            ("field", [TestPermission]),
            ("custom_resolved_field", [TestPermission]),
        ],
    )


def test_auto_id():
    @strawberry_django.filter_type(FieldAttributeModel)
    class MyTypeFilter:
        id: auto
        field: auto

    @strawberry_django.type(FieldAttributeModel)
    class MyType:
        id: auto
        other_id: auto = strawberry_django.field(field_name="id")
        field: auto

    @strawberry.type
    class Query:
        my_type: list[MyType] = strawberry_django.field(filters=MyTypeFilter)

    schema = strawberry.Schema(query=Query)
    expected = """\
    type MyType {
      id: ID!
      otherId: ID!
      field: String!
    }

    input MyTypeFilter {
      id: ID
      field: String
      AND: MyTypeFilter
      OR: MyTypeFilter
      NOT: MyTypeFilter
      DISTINCT: Boolean
    }

    type Query {
      myType(filters: MyTypeFilter): [MyType!]!
    }
    """
    assert textwrap.dedent(str(schema)) == textwrap.dedent(expected).strip()


def test_auto_id_with_node():
    @strawberry_django.filter_type(FieldAttributeModel)
    class MyTypeFilter:
        id: auto
        field: auto

    @strawberry_django.type(FieldAttributeModel)
    class MyType(relay.Node):
        other_id: auto = strawberry_django.field(field_name="id")
        field: auto

    @strawberry.type
    class Query:
        my_type: list[MyType] = strawberry_django.field(filters=MyTypeFilter)

    schema = strawberry.Schema(query=Query)
    expected = '''\
    type MyType implements Node {
      """The Globally Unique ID of this object"""
      id: ID!
      otherId: ID!
      field: String!
    }

    input MyTypeFilter {
      id: ID
      field: String
      AND: MyTypeFilter
      OR: MyTypeFilter
      NOT: MyTypeFilter
      DISTINCT: Boolean
    }

    """An object with a Globally Unique ID"""
    interface Node {
      """The Globally Unique ID of this object"""
      id: ID!
    }

    type Query {
      myType(filters: MyTypeFilter): [MyType!]!
    }
    '''
    assert textwrap.dedent(str(schema)) == textwrap.dedent(expected).strip()


@override_settings(
    STRAWBERRY_DJANGO={
        **strawberry_django_settings(),
        "MAP_AUTO_ID_AS_GLOBAL_ID": True,
    },
)
def test_auto_id_with_node_mapping_global_id():
    @strawberry_django.filter_type(FieldAttributeModel)
    class MyTypeFilter:
        id: auto
        field: auto

    @strawberry_django.type(FieldAttributeModel)
    class MyType(relay.Node):
        other_id: auto = strawberry_django.field(field_name="id")
        field: auto

    @strawberry.type
    class Query:
        my_type: list[MyType] = strawberry_django.field(filters=MyTypeFilter)

    schema = strawberry.Schema(query=Query)
    expected = '''\
    type MyType implements Node {
      """The Globally Unique ID of this object"""
      id: ID!
      otherId: ID!
      field: String!
    }

    input MyTypeFilter {
      id: ID
      field: String
      AND: MyTypeFilter
      OR: MyTypeFilter
      NOT: MyTypeFilter
      DISTINCT: Boolean
    }

    """An object with a Globally Unique ID"""
    interface Node {
      """The Globally Unique ID of this object"""
      id: ID!
    }

    type Query {
      myType(filters: MyTypeFilter): [MyType!]!
    }
    '''
    assert textwrap.dedent(str(schema)) == textwrap.dedent(expected).strip()