示例#1
0
def test_simple_types():
    args = {"integer": 1, "string": "abc", "float": 1.2, "bool": True}

    arguments = [
        StrawberryArgument(
            graphql_name="integer",
            type_annotation=StrawberryAnnotation(int),
            python_name="integer",
        ),
        StrawberryArgument(
            graphql_name="string",
            type_annotation=StrawberryAnnotation(str),
            python_name="string",
        ),
        StrawberryArgument(
            graphql_name="float",
            type_annotation=StrawberryAnnotation(float),
            python_name="float",
        ),
        StrawberryArgument(
            graphql_name="bool",
            type_annotation=StrawberryAnnotation(bool),
            python_name="bool",
        ),
    ]

    assert convert_arguments(
        args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY
    ) == {
        "integer": 1,
        "string": "abc",
        "float": 1.2,
        "bool": True,
    }
def test_uses_default_for_optional_types_when_nothing_is_passed():
    @strawberry.input
    class Number:
        value: int

    @strawberry.input
    class Input:
        numbers: Optional[Number] = UNSET
        numbers_second: Optional[Number] = UNSET

    # case 1
    args = {"input": {}}

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(Input),
        ),
    ]

    assert convert_arguments(args, arguments) == {"input": Input(UNSET, UNSET)}

    # case 2
    args = {"input": {"numbersSecond": None}}

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(Input),
        ),
    ]

    assert convert_arguments(args, arguments) == {"input": Input(UNSET, None)}
示例#3
0
def test_list():
    args = {
        "integerList": [1, 2],
        "stringList": ["abc", "cde"],
    }

    arguments = [
        StrawberryArgument(
            graphql_name="integerList",
            python_name="integer_list",
            type_annotation=StrawberryAnnotation(List[int]),
        ),
        StrawberryArgument(
            graphql_name="stringList",
            python_name="string_list",
            type_annotation=StrawberryAnnotation(List[str]),
        ),
    ]

    assert convert_arguments(
        args, arguments, scalar_registry=DEFAULT_SCALAR_REGISTRY
    ) == {
        "integer_list": [1, 2],
        "string_list": ["abc", "cde"],
    }
示例#4
0
def test_basic_optional():
    annotation = StrawberryAnnotation(Optional[str])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert resolved.of_type is str

    assert resolved == StrawberryOptional(of_type=str)
    assert resolved == Optional[str]
示例#5
0
def test_optional_list():
    annotation = StrawberryAnnotation(Optional[List[bool]])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert resolved.of_type == List[bool]

    assert resolved == StrawberryOptional(of_type=List[bool])
    assert resolved == Optional[List[bool]]
示例#6
0
def test_optional_union_containing_a_real_union_and_unset():
    annotation = StrawberryAnnotation(Union[str, int, None, _Unset])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert resolved.of_type == Union[str, int]

    assert resolved == StrawberryOptional(of_type=Union[str, int])
    assert resolved == Optional[Union[str, int]]
示例#7
0
def test_list_of_optional():
    annotation = StrawberryAnnotation(List[Optional[int]])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert resolved.of_type == Optional[int]

    assert resolved == StrawberryList(of_type=Optional[int])
    assert resolved == List[Optional[int]]
示例#8
0
def test_optional_of_string():
    annotation = StrawberryAnnotation(Optional["bool"])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert resolved.of_type is bool

    assert resolved == StrawberryOptional(of_type=bool)
    assert resolved == Optional[bool]
示例#9
0
def test_list_of_string():
    annotation = StrawberryAnnotation(List["int"])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert resolved.of_type is int

    assert resolved == StrawberryList(of_type=int)
    assert resolved == List[int]
示例#10
0
def test_list_of_lists():
    annotation = StrawberryAnnotation(List[List[float]])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert resolved.of_type == List[float]

    assert resolved == StrawberryList(of_type=List[float])
    assert resolved == List[List[float]]
示例#11
0
def test_basic_list():
    annotation = StrawberryAnnotation(List[str])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert resolved.of_type is str

    assert resolved == StrawberryList(of_type=str)
    assert resolved == List[str]
示例#12
0
def test_string_of_object():
    @strawberry.type
    class StrType:
        thing: int

    annotation = StrawberryAnnotation("StrType", namespace=locals())
    resolved = annotation.resolve()

    assert resolved is StrType
示例#13
0
def test_optional_with_unset_as_union():
    annotation = StrawberryAnnotation(Union[_Unset, None, str])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert resolved.of_type is str

    assert resolved == StrawberryOptional(of_type=str)
    assert resolved == Optional[str]
示例#14
0
def test_string_of_type_var():
    T = TypeVar("T")

    annotation = StrawberryAnnotation("T", namespace=locals())
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryTypeVar)
    assert resolved.type_var is T

    assert resolved == T
示例#15
0
def test_generic_optionals():
    T = TypeVar("T")

    annotation = StrawberryAnnotation(Optional[T])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert isinstance(resolved.of_type, StrawberryTypeVar)
    assert resolved.is_generic

    assert resolved == Optional[T]
示例#16
0
def test_string_of_optional():
    namespace = {**locals(), **globals()}

    annotation = StrawberryAnnotation("Optional[int]", namespace=namespace)
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert resolved.of_type is int

    assert resolved == StrawberryOptional(of_type=int)
    assert resolved == Optional[int]
示例#17
0
def test_basic_generic():
    T = TypeVar("T")

    annotation = StrawberryAnnotation(T)
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryTypeVar)
    assert resolved.is_generic
    assert resolved.type_var is T

    assert resolved == T
示例#18
0
def test_generic_lists():
    T = TypeVar("T")

    annotation = StrawberryAnnotation(List[T])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert isinstance(resolved.of_type, StrawberryTypeVar)
    assert resolved.is_generic

    assert resolved == List[T]
示例#19
0
def test_optional_optional():
    """Optional[Optional[...]] is squashed by Python to just Optional[...]"""
    annotation = StrawberryAnnotation(Optional[Optional[bool]])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert resolved.of_type is bool

    assert resolved == StrawberryOptional(of_type=bool)
    assert resolved == Optional[Optional[bool]]
    assert resolved == Optional[bool]
示例#20
0
def test_string_of_list():
    namespace = {**locals(), **globals()}

    annotation = StrawberryAnnotation("List[float]", namespace=namespace)
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert resolved.of_type is float

    assert resolved == StrawberryList(of_type=float)
    assert resolved == List[float]
示例#21
0
def test_generic_unions():
    S = TypeVar("S")
    T = TypeVar("T")

    annotation = StrawberryAnnotation(Union[S, T])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryUnion)
    assert resolved.types == (S, T)
    assert resolved.is_generic

    assert resolved == Union[S, T]
示例#22
0
def test_list_of_string_of_type():
    @strawberry.type
    class NameGoesHere:
        foo: bool

    annotation = StrawberryAnnotation(List["NameGoesHere"], namespace=locals())
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert resolved.of_type is NameGoesHere

    assert resolved == StrawberryList(of_type=NameGoesHere)
    assert resolved == List[NameGoesHere]
示例#23
0
def test_basic():
    @strawberry.enum
    class NumaNuma(Enum):
        MA = "ma"
        I = "i"  # noqa: E741
        A = "a"
        HI = "hi"

    annotation = StrawberryAnnotation(NumaNuma)
    resolved = annotation.resolve()

    # TODO: Remove reference to .enum_definition with StrawberryEnum
    assert resolved is NumaNuma._enum_definition
示例#24
0
def test_lazy_type():
    # Module path is short and relative because of the way pytest runs the file
    LazierType = LazyType["LaziestType", "test_lazy_types"]

    annotation = StrawberryAnnotation(LazierType)
    resolved = annotation.resolve()

    # Currently StrawberryAnnotation(LazyType).resolve() returns the unresolved
    # LazyType. We may want to find a way to directly return the referenced object
    # without a second resolving step.
    assert isinstance(resolved, LazyType)
    assert resolved is LazierType
    assert resolved.resolve_type() is LaziestType
示例#25
0
def test_forward_reference():
    global ForwardClass

    annotation = StrawberryAnnotation("ForwardClass", namespace=globals())

    @strawberry.type
    class ForwardClass:
        backward: bool

    resolved = annotation.resolve()

    assert resolved is ForwardClass

    del ForwardClass
示例#26
0
def test_string_of_list_of_type():
    @strawberry.type
    class BlahBlah:
        foo: bool

    namespace = {**locals(), **globals()}

    annotation = StrawberryAnnotation("List[BlahBlah]", namespace=namespace)
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert resolved.of_type is BlahBlah

    assert resolved == StrawberryList(of_type=BlahBlah)
    assert resolved == List[BlahBlah]
示例#27
0
def test_type_var():
    T = TypeVar("T")

    annotation = StrawberryAnnotation(T)
    field = StrawberryField(type_annotation=annotation)

    assert field.type == T
def test_when_optional():
    @strawberry.input
    class Number:
        value: int

    @strawberry.input
    class Input:
        numbers: Optional[Number] = UNSET
        numbers_second: Optional[Number] = UNSET

    args = {}

    arguments = [
        StrawberryArgument(
            graphql_name=None,
            python_name="input",
            type_annotation=StrawberryAnnotation(Optional[Input]),
        )
    ]

    assert (convert_arguments(
        args,
        arguments,
        scalar_registry=DEFAULT_SCALAR_REGISTRY,
        config=StrawberryConfig(),
    ) == {})
def test_lazy():
    LazierType = LazyType["LaziestType", __name__]

    args = {
        "lazyArg": {
            "something": True
        },
    }

    arguments = [
        StrawberryArgument(
            graphql_name="lazyArg",
            python_name="lazy_arg",
            type_annotation=StrawberryAnnotation(LazierType),
        ),
    ]

    assert (convert_arguments(
        args,
        arguments,
        scalar_registry=DEFAULT_SCALAR_REGISTRY,
        config=StrawberryConfig(),
    ) == {
        "lazy_arg": LaziestType(something=True)
    })
示例#30
0
    def arguments(self) -> List[StrawberryArgument]:
        parameters = inspect.signature(self._unbound_wrapped_func).parameters
        function_arguments = set(parameters) - self._SPECIAL_ARGS

        arguments = self.annotations.copy()
        arguments.pop("return", None)  # Discard return annotation to get just arguments

        arguments_missing_annotations = function_arguments - set(arguments)

        if any(arguments_missing_annotations):
            raise MissingArgumentsAnnotationsError(
                field_name=self.name,
                arguments=arguments_missing_annotations,
            )

        module = sys.modules[self._module]
        annotation_namespace = module.__dict__
        strawberry_arguments = []
        for arg_name, annotation in arguments.items():
            parameter = parameters[arg_name]

            argument = StrawberryArgument(
                python_name=arg_name,
                graphql_name=None,
                type_annotation=StrawberryAnnotation(
                    annotation=annotation, namespace=annotation_namespace
                ),
                default=parameter.default,
            )

            strawberry_arguments.append(argument)

        return strawberry_arguments