hypothesis-graphql


Namehypothesis-graphql JSON
Version 0.11.0 PyPI version JSON
download
home_pageNone
SummaryHypothesis strategies for GraphQL queries
upload_time2023-11-29 19:58:24
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords graphql hypothesis testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # hypothesis-graphql

[![Build](https://github.com/Stranger6667/hypothesis-graphql/workflows/build/badge.svg)](https://github.com/Stranger6667/hypothesis-graphql/actions)
[![Coverage](https://codecov.io/gh/Stranger6667/hypothesis-graphql/branch/master/graph/badge.svg)](https://codecov.io/gh/Stranger6667/hypothesis-graphql/branch/master)
[![Version](https://img.shields.io/pypi/v/hypothesis-graphql.svg)](https://pypi.org/project/hypothesis-graphql/)
[![Python versions](https://img.shields.io/pypi/pyversions/hypothesis-graphql.svg)](https://pypi.org/project/hypothesis-graphql/)
[![Chat](https://img.shields.io/discord/938139740912369755)](https://discord.gg/VnxfdFmBUp)
[![License](https://img.shields.io/pypi/l/hypothesis-graphql.svg)](https://opensource.org/licenses/MIT)

<h4 align="center">
Generate queries matching your GraphQL schema, and use them to verify your backend implementation
</h4>

It is a Python library that provides a set of [Hypothesis](https://github.com/HypothesisWorks/hypothesis/tree/master/hypothesis-python) 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](https://github.com/schemathesis/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:

```python
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:

```python
@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`:

```python
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](https://opensource.org/licenses/MIT).
By contributing to `hypothesis-graphql`, you agree that your contributions will be licensed under its MIT license.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "hypothesis-graphql",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Dmitry Dygalo <dmitry@dygalo.dev>",
    "keywords": "graphql,hypothesis,testing",
    "author": null,
    "author_email": "Dmitry Dygalo <dmitry@dygalo.dev>",
    "download_url": "https://files.pythonhosted.org/packages/d7/d2/d81cf17dfdd83482dd13c0e1e22f38375db2e14affae5f9f1e23e57db78c/hypothesis_graphql-0.11.0.tar.gz",
    "platform": null,
    "description": "# hypothesis-graphql\n\n[![Build](https://github.com/Stranger6667/hypothesis-graphql/workflows/build/badge.svg)](https://github.com/Stranger6667/hypothesis-graphql/actions)\n[![Coverage](https://codecov.io/gh/Stranger6667/hypothesis-graphql/branch/master/graph/badge.svg)](https://codecov.io/gh/Stranger6667/hypothesis-graphql/branch/master)\n[![Version](https://img.shields.io/pypi/v/hypothesis-graphql.svg)](https://pypi.org/project/hypothesis-graphql/)\n[![Python versions](https://img.shields.io/pypi/pyversions/hypothesis-graphql.svg)](https://pypi.org/project/hypothesis-graphql/)\n[![Chat](https://img.shields.io/discord/938139740912369755)](https://discord.gg/VnxfdFmBUp)\n[![License](https://img.shields.io/pypi/l/hypothesis-graphql.svg)](https://opensource.org/licenses/MIT)\n\n<h4 align=\"center\">\nGenerate queries matching your GraphQL schema, and use them to verify your backend implementation\n</h4>\n\nIt is a Python library that provides a set of [Hypothesis](https://github.com/HypothesisWorks/hypothesis/tree/master/hypothesis-python) strategies that\nlet you write tests parametrized by a source of examples.\nGenerated queries have arbitrary depth and may contain any subset of GraphQL types defined in the input schema.\nThey expose edge cases in your code that are unlikely to be found otherwise.\n\n[Schemathesis](https://github.com/schemathesis/schemathesis) provides a higher-level interface around this library and finds server crashes automatically.\n\n## Usage\n\n`hypothesis-graphql` provides the `from_schema` function, which takes a GraphQL schema and returns a Hypothesis strategy for\nGraphQL queries matching the schema:\n\n```python\nfrom hypothesis import given\nfrom hypothesis_graphql import from_schema\nimport requests\n\n# Strings and `graphql.GraphQLSchema` are supported\nSCHEMA = \"\"\"\ntype Book {\n  title: String\n  author: Author\n}\n\ntype Author {\n  name: String\n  books: [Book]\n}\n\ntype Query {\n  getBooks: [Book]\n  getAuthors: [Author]\n}\n\ntype Mutation {\n  addBook(title: String!, author: String!): Book!\n  addAuthor(name: String!): Author!\n}\n\"\"\"\n\n\n@given(from_schema(SCHEMA))\ndef test_graphql(query):\n    # Will generate samples like these:\n    #\n    # {\n    #   getBooks {\n    #     title\n    #   }\n    # }\n    #\n    # mutation {\n    #   addBook(title: \"H4Z\\u7869\", author: \"\\u00d2\"){\n    #     title\n    #   }\n    # }\n    response = requests.post(\"http://127.0.0.1/graphql\", json={\"query\": query})\n    assert response.status_code == 200\n    assert response.json().get(\"errors\") is None\n```\n\nIt is also possible to generate queries or mutations separately with `hypothesis_graphql.queries` and `hypothesis_graphql.mutations`.\n\n### Customization\n\nTo restrict the set of fields in generated operations use the `fields` argument:\n\n```python\n@given(from_schema(SCHEMA, fields=[\"getAuthors\"]))\ndef test_graphql(query):\n    # Only `getAuthors` will be generated\n    ...\n```\n\nIt is also possible to generate custom scalars. For example, `Date`:\n\n```python\nfrom hypothesis import strategies as st, given\nfrom hypothesis_graphql import from_schema, nodes\n\nSCHEMA = \"\"\"\nscalar Date\n\ntype Query {\n  getByDate(created: Date!): Int\n}\n\"\"\"\n\n\n@given(\n    from_schema(\n        SCHEMA,\n        custom_scalars={\n            # Standard scalars work out of the box, for custom ones you need\n            # to pass custom strategies that generate proper AST nodes\n            \"Date\": st.dates().map(nodes.String)\n        },\n    )\n)\ndef test_graphql(query):\n    # Example:\n    #\n    #  { getByDate(created: \"2000-01-01\") }\n    #\n    ...\n```\n\nThe `hypothesis_graphql.nodes` module includes a few helpers to generate various node types:\n\n- `String` -> `graphql.StringValueNode`\n- `Float` -> `graphql.FloatValueNode`\n- `Int` -> `graphql.IntValueNode`\n- `Object` -> `graphql.ObjectValueNode`\n- `List` -> `graphql.ListValueNode`\n- `Boolean` -> `graphql.BooleanValueNode`\n- `Enum` -> `graphql.EnumValueNode`\n- `Null` -> `graphql.NullValueNode` (a constant, not a function)\n\nThey exist because classes like `graphql.StringValueNode` can't be directly used in `map` calls due to kwarg-only arguments.\n\n## License\n\nThe code in this project is licensed under [MIT license](https://opensource.org/licenses/MIT).\nBy contributing to `hypothesis-graphql`, you agree that your contributions will be licensed under its MIT license.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Hypothesis strategies for GraphQL queries",
    "version": "0.11.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/Stranger6667/hypothesis-graphql",
        "Changelog": "https://github.com/Stranger6667/hypothesis-graphql/blob/master/CHANGELOG.md",
        "Funding": "https://github.com/sponsors/Stranger6667",
        "Source Code": "https://github.com/Stranger6667/hypothesis-graphql"
    },
    "split_keywords": [
        "graphql",
        "hypothesis",
        "testing"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4cd753032581e87f1e2597ee5af0390e90fb2738ca3b3bf2f6b391814487c1f1",
                "md5": "37cd4bcc167405ea5e913197cdf3da1f",
                "sha256": "1beb3b75d60626526f1e40321a9fb42ee13aefb8ff8990c91a1af9108ed1d3e6"
            },
            "downloads": -1,
            "filename": "hypothesis_graphql-0.11.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "37cd4bcc167405ea5e913197cdf3da1f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 16056,
            "upload_time": "2023-11-29T19:58:22",
            "upload_time_iso_8601": "2023-11-29T19:58:22.342703Z",
            "url": "https://files.pythonhosted.org/packages/4c/d7/53032581e87f1e2597ee5af0390e90fb2738ca3b3bf2f6b391814487c1f1/hypothesis_graphql-0.11.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "d7d2d81cf17dfdd83482dd13c0e1e22f38375db2e14affae5f9f1e23e57db78c",
                "md5": "8a50b5e52fbb1e12d790d67ee2d6c459",
                "sha256": "a1a1b693170591dd37d9b40a94e6f37181770c21cebad993e2e14e58ddfebaba"
            },
            "downloads": -1,
            "filename": "hypothesis_graphql-0.11.0.tar.gz",
            "has_sig": false,
            "md5_digest": "8a50b5e52fbb1e12d790d67ee2d6c459",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 305168,
            "upload_time": "2023-11-29T19:58:24",
            "upload_time_iso_8601": "2023-11-29T19:58:24.013460Z",
            "url": "https://files.pythonhosted.org/packages/d7/d2/d81cf17dfdd83482dd13c0e1e22f38375db2e14affae5f9f1e23e57db78c/hypothesis_graphql-0.11.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-29 19:58:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Stranger6667",
    "github_project": "hypothesis-graphql",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "hypothesis-graphql"
}
        
Elapsed time: 0.14730s