File: test_resolver_default_parameter_not_serializable.py

package info (click to toggle)
python-apischema 0.18.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,608 kB
  • sloc: python: 15,266; sh: 7; makefile: 7
file content (43 lines) | stat: -rw-r--r-- 1,125 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
from functools import wraps
from typing import Union

import pytest
from graphql import graphql_sync
from graphql.utilities import print_schema

from apischema import Undefined, UndefinedType, Unsupported
from apischema.graphql import graphql_schema
from apischema.typing import Annotated


class Foo:
    pass


@pytest.mark.parametrize(
    "tp, default",
    [
        (Union[UndefinedType, int], Undefined),
        (Union[int, Annotated[Foo, Unsupported]], Foo()),
    ],
)
def test_resolver_default_parameter_not_serializable(tp, default):
    def resolver(arg=default) -> bool:
        return arg is default

    resolver.__annotations__["arg"] = tp
    # wraps in order to trigger the bug of get_type_hints with None default value
    resolver2 = wraps(resolver)(lambda arg=default: resolver(arg))
    schema = graphql_schema(query=[resolver2])
    assert (
        print_schema(schema)
        == """\
type Query {
  resolver(arg: Int): Boolean!
}"""
    )
    assert (
        graphql_sync(schema, "{resolver}").data
        == graphql_sync(schema, "{resolver(arg: null)}").data
        == {"resolver": True}
    )