Ejemplo n.º 1
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]]
Ejemplo n.º 2
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]]
Ejemplo n.º 3
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]
Ejemplo n.º 4
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]]
Ejemplo n.º 5
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]
Ejemplo n.º 6
0
def test_string_of_object():
    @strawberry.type
    class StrType:
        thing: int

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

    assert resolved is StrType
Ejemplo n.º 7
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]]
Ejemplo n.º 8
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]
Ejemplo n.º 9
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]
Ejemplo n.º 10
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]
Ejemplo n.º 11
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
Ejemplo n.º 12
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]
Ejemplo n.º 13
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]
Ejemplo n.º 14
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
Ejemplo n.º 15
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]
Ejemplo n.º 16
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]
Ejemplo n.º 17
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]
Ejemplo n.º 18
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]
Ejemplo n.º 19
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
Ejemplo n.º 20
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
Ejemplo n.º 21
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]
Ejemplo n.º 22
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
Ejemplo n.º 23
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]
Ejemplo n.º 24
0
def test_forward_reference_locals_and_globals():
    global BackwardClass

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

    annotation = StrawberryAnnotation("BackwardClass", namespace=namespace)

    @strawberry.type
    class BackwardClass:
        backward: bool

    resolved = annotation.resolve()

    assert resolved is BackwardClass

    del BackwardClass
Ejemplo n.º 25
0
def test_none():
    annotation = StrawberryAnnotation(None)
    with pytest.raises(ValueError,
                       match="Annotation cannot be plain None type"):
        annotation.resolve()

    annotation = StrawberryAnnotation(Optional[int])
    annotation.resolve()

    annotation = StrawberryAnnotation(Union[None, int])
    annotation.resolve()
Ejemplo n.º 26
0
def test_optional_union():
    @strawberry.type
    class CoolType:
        foo: float

    @strawberry.type
    class UncoolType:
        bar: bool

    annotation = StrawberryAnnotation(Optional[Union[CoolType, UncoolType]])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryOptional)
    assert resolved.of_type == Union[CoolType, UncoolType]

    assert resolved == StrawberryOptional(of_type=Union[CoolType, UncoolType])
    assert resolved == Optional[Union[CoolType, UncoolType]]
Ejemplo n.º 27
0
def test_list_of_union():
    @strawberry.type
    class Animal:
        feet: bool

    @strawberry.type
    class Fungus:
        spore: bool

    annotation = StrawberryAnnotation(List[Union[Animal, Fungus]])
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryList)
    assert resolved.of_type == Union[Animal, Fungus]

    assert resolved == StrawberryList(of_type=Union[Animal, Fungus])
    assert resolved == List[Union[Animal, Fungus]]
Ejemplo n.º 28
0
def test_generic_objects():
    T = TypeVar("T")

    @strawberry.type
    class FooBar(Generic[T]):
        thing: T

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

    # TODO: Simplify with StrawberryObject
    assert isinstance(resolved, type)
    assert hasattr(resolved, "_type_definition")
    assert isinstance(resolved._type_definition, TypeDefinition)
    assert resolved._type_definition.is_generic

    field: StrawberryField = resolved._type_definition.fields[0]
    assert isinstance(field.type, StrawberryTypeVar)
    assert field.type == T
Ejemplo n.º 29
0
def test_python_union_short_syntax():
    @strawberry.type
    class User:
        name: str

    @strawberry.type
    class Error:
        name: str

    annotation = StrawberryAnnotation(User | Error)
    resolved = annotation.resolve()

    assert isinstance(resolved, StrawberryUnion)
    assert resolved.types == (User, Error)

    assert resolved == StrawberryUnion(
        name="UserError",
        type_annotations=(StrawberryAnnotation(User),
                          StrawberryAnnotation(Error)),
    )
    assert resolved == Union[User, Error]
Ejemplo n.º 30
0
def test_lazy_type_generic():
    T = TypeVar("T")

    @strawberry.type
    class GenericType(Generic[T]):
        item: T

    # Module path is short and relative because of the way pytest runs the file
    LazierType = LazyType["LaziestType", "test_lazy_types"]
    ResolvedType = GenericType[LazierType]

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

    # TODO: Simplify with StrawberryObject
    assert isinstance(resolved, type)
    assert hasattr(resolved, "_type_definition")
    assert isinstance(resolved._type_definition, TypeDefinition)

    items_field: StrawberryField = resolved._type_definition.fields[0]
    assert items_field.type is LazierType
    assert items_field.type.resolve_type() is LaziestType