hypothesis-graphql


Namehypothesis-graphql JSON
Version 0.9.0 PyPI version JSON
download
home_pagehttps://github.com/Stranger6667/hypothesis-graphql
SummaryHypothesis strategies for GraphQL queries and mutations
upload_time2022-04-29 19:42:04
maintainer
docs_urlNone
authorDmitry Dygalo
requires_python>=3.6,<4.0
licenseMIT
keywords hypothesis graphql testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # 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)

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](https://github.com/schemathesis/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:

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

```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.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Stranger6667/hypothesis-graphql",
    "name": "hypothesis-graphql",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6,<4.0",
    "maintainer_email": "",
    "keywords": "hypothesis,graphql,testing",
    "author": "Dmitry Dygalo",
    "author_email": "dadygalo@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/cb/c5/35b7d9bff89b2369d350f75d5f79c7164e127c631b68d58ea9c3a88e567d/hypothesis-graphql-0.9.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\nHypothesis strategies for GraphQL operations. Allows you to generate arbitrary GraphQL queries for the given schema.\nIt starts with simple examples and iteratively goes to more complex ones.\n\nFor web API testing, [Schemathesis](https://github.com/schemathesis/schemathesis) provides a higher-level wrapper and can\ndetect internal server errors.\n\n## Usage\n\n`hypothesis_graphql` exposes the `from_schema` function, which takes a GraphQL schema and returns a Hypothesis strategy for\ndefined queries and mutations:\n\n```python\nfrom hypothesis import given\nfrom hypothesis_graphql import from_schema\n\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    ...\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",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Hypothesis strategies for GraphQL queries and mutations",
    "version": "0.9.0",
    "split_keywords": [
        "hypothesis",
        "graphql",
        "testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "eef0e3cba442e4e5b9581662184d736b",
                "sha256": "5022950fe7483addbd242702c71380aec993ca1d0b291bf3dee9de67a638cbbc"
            },
            "downloads": -1,
            "filename": "hypothesis_graphql-0.9.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "eef0e3cba442e4e5b9581662184d736b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6,<4.0",
            "size": 15178,
            "upload_time": "2022-04-29T19:42:02",
            "upload_time_iso_8601": "2022-04-29T19:42:02.868634Z",
            "url": "https://files.pythonhosted.org/packages/44/3e/8ebe37888f5bc3648ac572cf0fbbd3caab7095bc546d98ee87c6de49c6b5/hypothesis_graphql-0.9.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "a16e1068e1e15b849172cc1f7f921f0c",
                "sha256": "7f30e41450f979edbfd951b5161581baea318a04f7a9db56399ee464c313bfae"
            },
            "downloads": -1,
            "filename": "hypothesis-graphql-0.9.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a16e1068e1e15b849172cc1f7f921f0c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6,<4.0",
            "size": 13207,
            "upload_time": "2022-04-29T19:42:04",
            "upload_time_iso_8601": "2022-04-29T19:42:04.246875Z",
            "url": "https://files.pythonhosted.org/packages/cb/c5/35b7d9bff89b2369d350f75d5f79c7164e127c631b68d58ea9c3a88e567d/hypothesis-graphql-0.9.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-04-29 19:42:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Stranger6667",
    "github_project": "hypothesis-graphql",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "hypothesis-graphql"
}
        
Elapsed time: 0.52682s