All Projects → Stranger6667 → hypothesis-graphql

Stranger6667 / hypothesis-graphql

Licence: MIT license
Generate arbitrary queries matching your GraphQL schema, and use them to verify your backend implementation.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to hypothesis-graphql

Deepconvsep
Deep Convolutional Neural Networks for Musical Source Separation
Stars: ✭ 424 (+1225%)
Mutual labels:  data-generation
Copulas
A library to model multivariate data using copulas.
Stars: ✭ 149 (+365.63%)
Mutual labels:  data-generation
k6-example-data-generation
Example repository showing how to utilise k6 and faker to load test using generated data
Stars: ✭ 32 (+0%)
Mutual labels:  data-generation
Stream data
Data generation and property-based testing for Elixir. 🔮
Stars: ✭ 597 (+1765.63%)
Mutual labels:  data-generation
Gratis
GRATIS: GeneRAting TIme Series with diverse and controllable characteristics
Stars: ✭ 51 (+59.38%)
Mutual labels:  data-generation
Pydbgen
Random dataframe and database table generator
Stars: ✭ 191 (+496.88%)
Mutual labels:  data-generation
Sdv
Synthetic Data Generation for tabular, relational and time series data.
Stars: ✭ 360 (+1025%)
Mutual labels:  data-generation
synth
The Declarative Data Generator
Stars: ✭ 958 (+2893.75%)
Mutual labels:  data-generation
Generatedata
A powerful, feature-rich, random test data generator.
Stars: ✭ 1,883 (+5784.38%)
Mutual labels:  data-generation
obsidian-hypothesis-plugin
An Obsidian.md plugin that syncs highlights from Hypothesis.
Stars: ✭ 164 (+412.5%)
Mutual labels:  hypothesis
Data Augmentation Review
List of useful data augmentation resources. You will find here some not common techniques, libraries, links to github repos, papers and others.
Stars: ✭ 785 (+2353.13%)
Mutual labels:  data-generation
Neuralyzer
Neuralyzer is a library and a command line tool to anonymize databases (by updating existing data or populating a table with fake data)
Stars: ✭ 45 (+40.63%)
Mutual labels:  data-generation
Wakefield
Generate random data sets
Stars: ✭ 208 (+550%)
Mutual labels:  data-generation
Regexp Examples
Generate strings that match a given regular expression
Stars: ✭ 483 (+1409.38%)
Mutual labels:  data-generation
frontend-toolkit
Tools, documentation and resources for creating front-end pages and apps in Hypothesis
Stars: ✭ 18 (-43.75%)
Mutual labels:  hypothesis
Mockneat
MockNeat is a Java 8+ library that facilitates the generation of arbitrary data for your applications.
Stars: ✭ 410 (+1181.25%)
Mutual labels:  data-generation
Synth
The Declarative Data Generator
Stars: ✭ 161 (+403.13%)
Mutual labels:  data-generation
gooseberry
A command line utility to generate a knowledge base from Hypothesis annotations
Stars: ✭ 103 (+221.88%)
Mutual labels:  hypothesis
genalog
Genalog is an open source, cross-platform python package allowing generation of synthetic document images with custom degradations and text alignment capabilities.
Stars: ✭ 234 (+631.25%)
Mutual labels:  data-generation
ranger
Ranger is contextual data generator used to make sensible data for integration tests or to play with it in the database
Stars: ✭ 59 (+84.38%)
Mutual labels:  data-generation

hypothesis-graphql

Build Coverage Version Python versions Chat License

Generate queries matching your GraphQL schema, and use them to verify your backend implementation

It is a Python library that provides a set of Hypothesis strategies that let you write tests parametrized by a source of examples. Generated queries have arbitrary depth and may contain any subset of GraphQL types defined in the input schema. They expose edge cases in your code that are unlikely to be found otherwise.

Schemathesis provides a higher-level interface around this library and finds server crashes automatically.

Usage

hypothesis-graphql provides the from_schema function, which takes a GraphQL schema and returns a Hypothesis strategy for GraphQL queries matching the schema:

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

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.

License

The code in this project is licensed under MIT license. By contributing to hypothesis-graphql, you agree that your contributions will be licensed under its MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].