Hypothesis strategies for GraphQL queries and mutations
Project description
hypothesis-graphql
Hypothesis strategies for GraphQL operations. Allows you to generate arbitrary GraphQL queries for the given schema. It starts with simple examples and iteratively goes to more complex ones.
For web API testing, Schemathesis provides a higher-level wrapper and can detect internal server errors.
Usage
hypothesis_graphql
exposes the from_schema
function, which takes a GraphQL schema and returns a Hypothesis strategy for
defined queries and mutations:
from hypothesis import given
from hypothesis_graphql import from_schema
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
# }
# }
...
It is also possible to generate queries or mutations separately with hypothesis_graphql.queries
and hypothesis_graphql.mutations
.
Customization
To restrict the set of fields in generated operations use the fields
argument:
@given(from_schema(SCHEMA, fields=["getAuthors"]))
def test_graphql(query):
# Only `getAuthors` will be generated
...
It is also possible to generate custom scalars. For example, 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") }
#
...
The hypothesis_graphql.nodes
module includes a few helpers to generate various node types:
String
->graphql.StringValueNode
Float
->graphql.FloatValueNode
Int
->graphql.IntValueNode
Object
->graphql.ObjectValueNode
List
->graphql.ListValueNode
Boolean
->graphql.BooleanValueNode
Enum
->graphql.EnumValueNode
Null
->graphql.NullValueNode
(a constant, not a function)
They exist because classes like graphql.StringValueNode
can't be directly used in map
calls due to kwarg-only arguments.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hypothesis-graphql-0.9.0.tar.gz
.
File metadata
- Download URL: hypothesis-graphql-0.9.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f30e41450f979edbfd951b5161581baea318a04f7a9db56399ee464c313bfae |
|
MD5 | a16e1068e1e15b849172cc1f7f921f0c |
|
BLAKE2b-256 | cbc535b7d9bff89b2369d350f75d5f79c7164e127c631b68d58ea9c3a88e567d |
File details
Details for the file hypothesis_graphql-0.9.0-py3-none-any.whl
.
File metadata
- Download URL: hypothesis_graphql-0.9.0-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5022950fe7483addbd242702c71380aec993ca1d0b291bf3dee9de67a638cbbc |
|
MD5 | eef0e3cba442e4e5b9581662184d736b |
|
BLAKE2b-256 | 443e8ebe37888f5bc3648ac572cf0fbbd3caab7095bc546d98ee87c6de49c6b5 |