File: test_type.py

package info (click to toggle)
strawberry-graphql-django 0.62.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,968 kB
  • sloc: python: 27,530; sh: 17; makefile: 16
file content (133 lines) | stat: -rw-r--r-- 3,819 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
from typing import Union

import strawberry
from django.db import models
from strawberry import auto
from strawberry.types import get_object_definition
from strawberry.types.base import (
    StrawberryContainer,
    StrawberryList,
    StrawberryOptional,
)

import strawberry_django
from strawberry_django.fields.field import StrawberryDjangoField


class TypeModel(models.Model):
    boolean = models.BooleanField()
    string = models.CharField(max_length=50)
    foreign_key = models.ForeignKey(
        "TypeModel",
        blank=True,
        related_name="related_foreign_key",
        on_delete=models.CASCADE,
    )
    one_to_one = models.OneToOneField(
        "TypeModel",
        blank=True,
        related_name="related_one_to_one",
        on_delete=models.CASCADE,
    )
    many_to_many = models.ManyToManyField(
        "TypeModel",
        related_name="related_many_to_many",
    )


def test_type():
    @strawberry_django.type(TypeModel)
    class Type:
        id: auto
        boolean: auto
        string: auto

    object_definition = get_object_definition(Type, strict=True)
    assert [(f.name, f.type) for f in object_definition.fields] == [
        ("id", strawberry.ID),
        ("boolean", bool),
        ("string", str),
    ]


def test_inherit(testtype):
    @testtype(TypeModel)
    class Base:
        id: auto
        boolean: auto

    @strawberry_django.type(TypeModel)
    class Type(Base):
        string: auto

    object_definition = get_object_definition(Type, strict=True)
    assert [(f.name, f.type) for f in object_definition.fields] == [
        ("id", strawberry.ID),
        ("boolean", bool),
        ("string", str),
    ]


def test_default_value():
    @strawberry_django.type(TypeModel)
    class Type:
        string: auto = "data"
        string2: str = strawberry.field(default="data2")
        string3: str = strawberry_django.field(default="data3")

    object_definition = get_object_definition(Type, strict=True)
    assert [(f.name, f.type) for f in object_definition.fields] == [
        ("string", str),
        ("string2", str),
        ("string3", str),
    ]
    assert Type().string == "data"
    assert Type().string2 == "data2"
    assert Type().string3 == "data3"


def test_relationship_inherit(testtype):
    @testtype(TypeModel)
    class Base:
        foreign_key: auto
        related_foreign_key: auto
        one_to_one: auto
        related_one_to_one: auto
        many_to_many: auto
        related_many_to_many: auto
        another_name: auto = strawberry_django.field(field_name="foreign_key")

    @strawberry_django.type(TypeModel)
    class Type(Base):
        pass

    expected_fields: dict[str, tuple[Union[type, StrawberryContainer], bool]] = {
        "foreign_key": (strawberry_django.DjangoModelType, False),
        "related_foreign_key": (
            StrawberryList(strawberry_django.DjangoModelType),
            True,
        ),
        "one_to_one": (strawberry_django.DjangoModelType, False),
        "related_one_to_one": (
            StrawberryOptional(strawberry_django.DjangoModelType),
            False,
        ),
        "many_to_many": (
            StrawberryList(strawberry_django.DjangoModelType),
            True,
        ),
        "related_many_to_many": (
            StrawberryList(strawberry_django.DjangoModelType),
            True,
        ),
        "another_name": (strawberry_django.DjangoModelType, False),
    }

    object_definition = get_object_definition(Type, strict=True)
    assert len(object_definition.fields) == len(expected_fields)

    for f in object_definition.fields:
        expected_type, expected_is_list = expected_fields[f.name]
        assert isinstance(f, StrawberryDjangoField)
        assert f.is_list == expected_is_list
        assert f.type == expected_type