示例#1
0
def test_cyclic_import():
    from .type_a import TypeA
    from .type_b import TypeB

    @strawberry.type
    class Query:
        a: TypeA
        b: TypeB

    assert (
        print_type(Query(None, None))
        == textwrap.dedent(
            """
            type Query {
              a: TypeA!
              b: TypeB!
            }
            """
        ).strip()
    )

    assert (
        print_type(TypeA())
        == textwrap.dedent(
            """
            type TypeA {
              typeB: TypeB!
            }
            """
        ).strip()
    )

    assert (
        print_type(TypeB())
        == textwrap.dedent(
            """
            type TypeB {
              typeA: TypeA!
            }
            """
        ).strip()
    )
示例#2
0
def test_optional():
    @strawberry.type
    class MyType:
        s: Optional[str]

    expected_type = """
    type MyType {
      s: String
    }
    """

    assert print_type(MyType("a")) == textwrap.dedent(expected_type).strip()
示例#3
0
def test_recursive_type():
    @strawberry.type
    class MyType:
        s: "MyType"

    expected_type = """
    type MyType {
      s: MyType!
    }
    """

    assert print_type(MyType("a")) == textwrap.dedent(expected_type).strip()
示例#4
0
def test_interface():
    @strawberry.interface
    class Node:
        id: strawberry.ID

    expected_type = """
    interface Node {
      id: ID!
    }
    """

    assert print_type(Node("a")) == textwrap.dedent(expected_type).strip()
示例#5
0
def test_print_requires():
    @strawberry.federation.type(keys=["upc"])
    class Product:
        name: str = strawberry.federation.field(requires="email")

    expected_representation = """
        type Product @key(fields: "upc") {
          name: String! @requires(fields: "email")
        }
    """

    assert print_type(Product) == textwrap.dedent(
        expected_representation).strip()
示例#6
0
def test_subscription_type():
    @strawberry.type
    class MySub:
        @strawberry.subscription
        async def x(self, info) -> typing.AsyncGenerator[str, None]:
            yield "Hi"

    expected_type = """
    type MySub {
      x: String!
    }
    """

    assert print_type(MySub()) == textwrap.dedent(expected_type).strip()
示例#7
0
def test_optional_default():
    @strawberry.input
    class MyInput:
        s: Optional[str]
        i: int = 0

    expected_type = """
    input MyInput {
      s: String
      i: Int! = 0
    }
    """

    assert print_type(MyInput("a",
                              1)) == textwrap.dedent(expected_type).strip()
示例#8
0
def test_print_external():
    @strawberry.federation.type(keys=["upc"])
    class Product:
        upc: str = strawberry.federation.field(external=True)
        name: str = strawberry.federation.field(external=True)

    expected_representation = """
        type Product @key(fields: "upc") {
          upc: String! @external
          name: String! @external
        }
    """

    assert print_type(Product) == textwrap.dedent(
        expected_representation).strip()
示例#9
0
def test_print_provides():
    @strawberry.federation.type(keys=["upc"])
    class Product:
        upc: str
        name: typing.Optional[str]

    @strawberry.federation.type(keys=["id"])
    class Review:
        product: Product = strawberry.federation.field(provides="name")

    expected_representation = """
        type Review @key(fields: "id") {
          product: Product! @provides(fields: "name")
        }
    """

    assert print_type(Review) == textwrap.dedent(
        expected_representation).strip()
示例#10
0
def test_implementing_interface():
    @strawberry.interface
    class Node:
        id: strawberry.ID

    @strawberry.type
    class Post(Node):
        title: str

    expected_type = """
    type Post implements Node {
      id: ID!
      title: String!
    }
    """

    assert print_type(Post("a",
                           "abc")) == textwrap.dedent(expected_type).strip()
示例#11
0
def test_print_extend():
    @strawberry.federation.type(extend=True)
    class Product:
        upc: str
        name: typing.Optional[str]
        price: typing.Optional[int]
        weight: typing.Optional[int]

    expected_representation = """
        extend type Product {
          upc: String!
          name: String
          price: Int
          weight: Int
        }
    """

    assert print_type(Product) == textwrap.dedent(
        expected_representation).strip()
示例#12
0
def test_print_type_with_key():
    @strawberry.federation.type(keys=["upc"])
    class Product:
        upc: str
        name: typing.Optional[str]
        price: typing.Optional[int]
        weight: typing.Optional[int]

    expected_representation = """
        type Product @key(fields: "upc") {
          upc: String!
          name: String
          price: Int
          weight: Int
        }
    """

    assert print_type(Product) == textwrap.dedent(
        expected_representation).strip()
示例#13
0
def test_simple_required_types():
    @strawberry.type
    class MyType:
        s: str
        i: int
        b: bool
        f: float
        id: strawberry.ID

    expected_type = """
    type MyType {
      s: String!
      i: Int!
      b: Boolean!
      f: Float!
      id: ID!
    }
    """

    assert (print_type(MyType(
        "a", 1, True, 3.2, "123")) == textwrap.dedent(expected_type).strip())