File: test_schema.py

package info (click to toggle)
django-graphene 3.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,996 kB
  • sloc: javascript: 25,534; python: 11,992; makefile: 233; sh: 4
file content (59 lines) | stat: -rw-r--r-- 1,426 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
from pytest import raises

from ..registry import Registry
from ..types import DjangoObjectType
from .models import Reporter


def test_should_raise_if_no_model():
    with raises(Exception) as excinfo:

        class Character1(DjangoObjectType):
            fields = "__all__"

    assert "valid Django Model" in str(excinfo.value)


def test_should_raise_if_model_is_invalid():
    with raises(Exception) as excinfo:

        class Character2(DjangoObjectType):
            class Meta:
                model = 1
                fields = "__all__"

    assert "valid Django Model" in str(excinfo.value)


def test_should_map_fields_correctly():
    class ReporterType2(DjangoObjectType):
        class Meta:
            model = Reporter
            registry = Registry()
            fields = "__all__"

    fields = list(ReporterType2._meta.fields.keys())
    assert fields[:-3] == [
        "id",
        "first_name",
        "last_name",
        "email",
        "pets",
        "a_choice",
        "typed_choice",
        "class_choice",
        "callable_choice",
        "fans",
        "reporter_type",
    ]

    assert sorted(fields[-3:]) == ["apnewsreporter", "articles", "films"]


def test_should_map_only_few_fields():
    class Reporter2(DjangoObjectType):
        class Meta:
            model = Reporter
            fields = ("id", "email")

    assert list(Reporter2._meta.fields.keys()) == ["id", "email"]