Example #1
0
def test_nested_input_types():
    @strawberry.enum
    class ChangeType(Enum):
        MAJOR = "major"
        MINOR = "minor"
        PATCH = "patch"

    @strawberry.input
    class ReleaseInfo:
        change_type: ChangeType
        changelog: str

    @strawberry.enum
    class ReleaseFileStatus(Enum):
        MISSING = "missing"
        INVALID = "invalid"
        OK = "ok"

    @strawberry.input
    class AddReleaseFileCommentInput:
        pr_number: int
        status: ReleaseFileStatus
        release_info: typing.Optional[ReleaseInfo]

    args = {
        "input": {
            "prNumber": 12,
            "status": ReleaseFileStatus.OK.value,
            "releaseInfo": {
                "changeType": ChangeType.MAJOR.value,
                "changelog": "example",
            },
        }
    }

    annotations = {"input": AddReleaseFileCommentInput}

    assert convert_args(args, annotations) == {
        "input": AddReleaseFileCommentInput(
            pr_number=12,
            status=ReleaseFileStatus.OK,
            release_info=ReleaseInfo(change_type=ChangeType.MAJOR, changelog="example"),
        )
    }

    args = {
        "input": {
            "prNumber": 12,
            "status": ReleaseFileStatus.OK.value,
            "releaseInfo": None,
        }
    }

    annotations = {"input": AddReleaseFileCommentInput}

    assert convert_args(args, annotations) == {
        "input": AddReleaseFileCommentInput(
            pr_number=12, status=ReleaseFileStatus.OK, release_info=None
        )
    }
Example #2
0
def test_input_types():
    @strawberry.input
    class MyInput:
        abc: str
        say_hello_to: str
        was: int = strawberry.field(name="having", is_input=True)
        fun: str = strawberry.field(is_input=True)

    args = {
        "input": {
            "abc": "example",
            "sayHelloTo": "Patrick",
            "having": 10,
            "fun": "yes"
        }
    }

    annotations = {"input": MyInput}

    assert convert_args(args, annotations) == {
        "input": MyInput(abc="example",
                         say_hello_to="Patrick",
                         was=10,
                         fun="yes")
    }
Example #3
0
def test_optional_input_types():
    @strawberry.input
    class MyInput:
        abc: str

    args = {"input": {"abc": "example"}}

    annotations = {"input": typing.Optional[MyInput]}

    assert convert_args(args, annotations) == {"input": MyInput(abc="example")}
Example #4
0
def test_optional_list_of_input_types():
    @strawberry.input
    class MyInput:
        abc: str

    args = {"input_list": [{"abc": "example"}]}

    annotations = {"input_list": typing.Optional[typing.List[MyInput]]}

    assert convert_args(args, annotations) == {"input_list": [MyInput(abc="example")]}
Example #5
0
def test_simple_types():
    args = {"integer": 1, "string": "abc", "float": 1.2, "bool": True}

    annotations = {"integer": int, "string": str, "float": float, "bool": bool}

    assert convert_args(args, annotations) == {
        "integer": 1,
        "string": "abc",
        "float": 1.2,
        "bool": True,
    }
Example #6
0
def test_uses_unset_for_optional_types_when_nothing_is_passed():
    @strawberry.input
    class Number:
        value: int

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

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

    annotations = {"input": Input}

    assert convert_args(args, annotations) == {"input": Input(UNSET, UNSET)}

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

    annotations = {"input": Input}

    assert convert_args(args, annotations) == {"input": Input(UNSET, None)}
Example #7
0
def test_nested_list_of_complex_types():
    @strawberry.input
    class Number:
        value: int

    @strawberry.input
    class Input:
        numbers: typing.List[Number]

    args = {"input": {"numbers": [{"value": 1}, {"value": 2}]}}

    annotations = {"input": Input}

    assert convert_args(args, annotations) == {
        "input": Input(numbers=[Number(1), Number(2)])
    }
Example #8
0
def test_list():
    args = {
        "integerList": [1, 2],
        "stringList": ["abc", "cde"],
        "floatList": [1.2, 2.3],
    }

    annotations = {
        "integer_list": typing.List[int],
        "string_list": typing.List[str],
        "float_list": typing.List[float],
    }

    assert convert_args(args, annotations) == {
        "integer_list": [1, 2],
        "string_list": ["abc", "cde"],
        "float_list": [1.2, 2.3],
    }