跳转到主要内容

GraphQL查询的策略

项目描述

hypothesis-graphql

Build Coverage Version Python versions Chat License

生成与您的GraphQL模式匹配的查询,并使用它们验证您的后端实现

这是一个Python库,提供了一组Hypothesis策略,让您可以通过示例源编写参数化的测试。生成的查询具有任意深度,可能包含输入模式中定义的任何GraphQL类型的子集。它们揭示了代码中不太可能通过其他方式发现的边缘情况。

Schemathesis围绕此库提供了一个更高级的接口,并自动发现服务器崩溃。

用法

hypothesis-graphql提供了from_schema函数,它接受一个GraphQL模式并返回一个匹配该模式的Hypothesis策略。

from hypothesis import given
from hypothesis_graphql import from_schema
import requests

# Strings and `graphql.GraphQLSchema` are supported
SCHEMA = """
type Book {
  title: String
  author: Author
}

type Author {
  name: String
  books: [Book]
}

type Query {
  getBooks: [Book]
  getAuthors: [Author]
}

type Mutation {
  addBook(title: String!, author: String!): Book!
  addAuthor(name: String!): Author!
}
"""


@given(from_schema(SCHEMA))
def test_graphql(query):
    # Will generate samples like these:
    #
    # {
    #   getBooks {
    #     title
    #   }
    # }
    #
    # mutation {
    #   addBook(title: "H4Z\u7869", author: "\u00d2"){
    #     title
    #   }
    # }
    response = requests.post("http://127.0.0.1/graphql", json={"query": query})
    assert response.status_code == 200
    assert response.json().get("errors") is None

也可以单独使用 hypothesis_graphql.querieshypothesis_graphql.mutations 生成查询或突变。

自定义

要限制生成的操作中字段的集合,请使用 fields 参数。

@given(from_schema(SCHEMA, fields=["getAuthors"]))
def test_graphql(query):
    # Only `getAuthors` will be generated
    ...

您可以使用以下参数来自定义字符串生成 from_schema

  • allow_x00(默认 True):确定是否允许在字符串中生成 \x00 字节。这在避免某些网络服务器拒绝测试为无效时很有用。
  • codec(默认 utf-8):指定用于生成字符串的编码。如果您需要将输入限制为,例如,ASCII 范围,则很有帮助。
@given(from_schema(SCHEMA, allow_x00=False, codec="ascii"))
def test_graphql(query):
    assert "\0" not in query
    query.encode("ascii")

还可以生成自定义标量。例如,Date

from hypothesis import strategies as st, given
from hypothesis_graphql import from_schema, nodes

SCHEMA = """
scalar Date

type Query {
  getByDate(created: Date!): Int
}
"""


@given(
    from_schema(
        SCHEMA,
        custom_scalars={
            # Standard scalars work out of the box, for custom ones you need
            # to pass custom strategies that generate proper AST nodes
            "Date": st.dates().map(nodes.String)
        },
    )
)
def test_graphql(query):
    # Example:
    #
    #  { getByDate(created: "2000-01-01") }
    #
    ...

hypothesis_graphql.nodes 模块包含一些辅助函数来生成各种节点类型

  • String -> graphql.StringValueNode
  • Float -> graphql.FloatValueNode
  • Int -> graphql.IntValueNode
  • Object -> graphql.ObjectValueNode
  • List -> graphql.ListValueNode
  • Boolean -> graphql.BooleanValueNode
  • Enum -> graphql.EnumValueNode
  • Null -> graphql.NullValueNode(一个常量,不是一个函数)

它们存在是因为像 graphql.StringValueNode 这样的类不能直接在 map 调用中使用,因为这些类只接受关键字参数。

许可证

本项目代码遵循 MIT 许可协议。通过为 hypothesis-graphql 做出贡献,您同意您的贡献将遵循其 MIT 许可协议。

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

hypothesis_graphql-0.11.1.tar.gz (741.9 kB 查看哈希

上传时间 源代码

构建分发

hypothesis_graphql-0.11.1-py3-none-any.whl (16.4 kB 查看哈希

上传时间 Python 3

由以下支持