File: test_type.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (49 lines) | stat: -rw-r--r-- 1,206 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
import dataclasses
from typing import Optional
from typing_extensions import assert_type

import pytest

import strawberry
from strawberry.types.base import StrawberryObjectDefinition, get_object_definition


def test_get_object_definition():
    @strawberry.type
    class Fruit:
        name: str

    obj_definition = get_object_definition(Fruit)
    assert_type(obj_definition, Optional[StrawberryObjectDefinition])
    assert obj_definition is not None
    assert isinstance(obj_definition, StrawberryObjectDefinition)


def test_get_object_definition_non_strawberry_type():
    @dataclasses.dataclass
    class Fruit:
        name: str

    assert get_object_definition(Fruit) is None

    class OtherFruit: ...

    assert get_object_definition(OtherFruit) is None


def test_get_object_definition_strict():
    @strawberry.type
    class Fruit:
        name: str

    obj_definition = get_object_definition(Fruit, strict=True)
    assert_type(obj_definition, StrawberryObjectDefinition)

    class OtherFruit:
        name: str

    with pytest.raises(
        TypeError,
        match=r".* does not have a StrawberryObjectDefinition",
    ):
        get_object_definition(OtherFruit, strict=True)