Hypothesis strategies for GraphQL schemas and queries
Project description
Hypothesis strategies for GraphQL schemas, queries and data.
NOTE This package is experimental, some features are not supported yet.
Usage
There are a few strategies for different use cases.
Schema generation - hypothesis_graphql.strategies.schemas()
Query - hypothesis_graphql.strategies.queries(schema).
Mutation - hypothesis_graphql.strategies.mutations(schema).
Lets take this schema as an example:
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!
}
Then strategies might be used in this way:
from hypothesis import given
from hypothesis_graphql import strategies as gql_st
SCHEMA = "..." # the one above
@given(gql_st.queries(SCHEMA))
def test_query(query):
...
# This query might be generated:
#
# query {
# getBooks {
# title
# }
# }
@given(gql_st.mutations(SCHEMA))
def test_mutation(mutation):
...
# This mutation might be generated:
#
# mutation {
# addBook(title: "H4Z\u7869", author: "\u00d2"){
# title
# }
# }
Customization
To restrict the set of fields in generated operations use the fields argument:
@given(gql_st.queries(SCHEMA, fields=["getAuthors"]))
def test_query(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 strategies as gql_st, nodes
SCHEMA = """
scalar Date
type Query {
getByDate(created: Date!): Int
}
"""
@given(
gql_st.queries(
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_query(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.8.0.tar.gz
.
File metadata
- Download URL: hypothesis-graphql-0.8.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3c17d1f6007187bd728a1a58d8a161ccb6c61a21eb75591aa18513cf70e2863 |
|
MD5 | 4adf93b8ccddf52b2e796147a0297aeb |
|
BLAKE2b-256 | 7ae499d17b461a794e08043334b81d21a6eae2c09ce56dc6e38b3f7bdbfa307e |
File details
Details for the file hypothesis_graphql-0.8.0-py3-none-any.whl
.
File metadata
- Download URL: hypothesis_graphql-0.8.0-py3-none-any.whl
- Upload date:
- Size: 16.6 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 | e74bc9b0d70f3283e70234a700659b62a02f24aa30a5b5b8a2d641c723908839 |
|
MD5 | 942ec9a9c813a5c06a289d28daca8050 |
|
BLAKE2b-256 | 619995bcd7ca68d9cbb17f009c2fa4a3f4ab856168a04cfea131ec7610f2d24a |