Skip to content

Commit

Permalink
chore: Update pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Mar 29, 2022
1 parent 8ceaa12 commit b1e77fd
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 109 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.9

- run: pip install pre-commit
- run: SKIP=pylint,mypy pre-commit run --all-files
Expand All @@ -46,7 +46,7 @@ jobs:

- uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.9

- run: pip install pre-commit
- run: pre-commit run pylint --all-files
Expand All @@ -61,7 +61,7 @@ jobs:

- uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.9

- run: pip install pre-commit
- run: pre-commit run mypy --all-files
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:

- uses: actions/setup-python@v2
with:
python-version: 3.x
python-version: 3.9

- run: pip install tox coverage poetry

Expand Down
37 changes: 20 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
default_language_version:
python: python3.7
python: python3.9

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
rev: v4.1.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
Expand All @@ -15,17 +15,17 @@ repos:
- id: check-merge-conflict

- repo: https://github.com/jorisroovers/gitlint
rev: v0.15.0
rev: v0.17.0
hooks:
- id: gitlint

- repo: https://github.com/adrienverge/yamllint
rev: v1.25.0
rev: v1.26.3
hooks:
- id: yamllint

- repo: https://github.com/PyCQA/pydocstyle
rev: 5.1.1
rev: 6.1.1
hooks:
- id: pydocstyle

Expand All @@ -35,42 +35,45 @@ repos:
- id: relint

- repo: https://github.com/ambv/black
rev: 20.8b1
rev: 22.3.0
hooks:
- id: black
types: [python]

- repo: https://github.com/asottile/seed-isort-config
rev: v2.2.0
- repo: https://github.com/asottile/blacken-docs
rev: v1.12.1
hooks:
- id: seed-isort-config
- id: blacken-docs
additional_dependencies: [ black==22.3.0 ]

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.6.4
rev: v5.10.1
hooks:
- id: isort
exclude: ^example/.*$
additional_dependencies: ["isort[pyproject]"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790
rev: v0.942
hooks:
- id: mypy
exclude: ^(docs/|test/).*$
exclude: ^(docs/|test/|test-corpus/|example/).*$
args: ["--ignore-missing-imports"]
additional_dependencies: [types-attrs, types-click, types-requests, types-PyYAML]

- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.6.0
rev: v3.0.0a4
hooks:
- id: pylint
additional_dependencies: ["isort[pyproject]"]
exclude: ^(docs/|test/).*$
exclude: ^(docs/|test/|test-corpus/|example/).*$
# disabled import-error as may be run out of environment with deps
args: ["--disable=import-error"]

- repo: https://github.com/myint/rstcheck
rev: master
rev: 3f92957
hooks:
- id: rstcheck
additional_dependencies:
- sphinx==3.1.2
args: [--ignore-directives=code]
- sphinx==4.4.0
- sphinx-click==3.1.0
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Or in your Python tests:
schema = schemathesis.from_uri("http://0.0.0.0:8081/schema.yaml")
@schema.parametrize()
def test_api(case):
case.call_and_validate()
Expand Down
3 changes: 3 additions & 0 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ To use your custom checks with Schemathesis CLI, you need to register them via t
import schemathesis
@schemathesis.register_check
def new_check(response, case):
# some awesome assertions!
Expand All @@ -457,10 +458,12 @@ response code is ``200``.
import schemathesis
@schemathesis.register_check
def conditional_check(response, case):
if response.status_code == 200:
# some awesome assertions!
pass
else:
# check not relevant to this response, skip test
return True
Expand Down
34 changes: 21 additions & 13 deletions docs/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,24 @@ To register a new hook function, you need to use special decorators - ``register
import schemathesis
@schemathesis.hooks.register
def before_generate_query(context, strategy):
return strategy.filter(lambda x: x["id"].isdigit())
schema = schemathesis.from_uri("http://0.0.0.0:8080/swagger.json")
@schema.hooks.register("before_generate_query")
def schema_hook(context, strategy):
return strategy.filter(lambda x: int(x["id"]) % 2 == 0)
def before_generate_headers(context, strategy):
return strategy.filter(lambda x: len(x["id"]) > 5)
@schema.hooks.apply(before_generate_headers)
@schema.parametrize()
def test_api(case):
Expand Down Expand Up @@ -104,9 +109,7 @@ Then, with this hook, you can query the database for some existing order and set
.. code:: python
def before_process_path(
context: schemathesis.hooks.HookContext,
path: str,
methods: Dict[str, Any]
context: schemathesis.hooks.HookContext, path: str, methods: Dict[str, Any]
) -> None:
if path == "/orders/{order_id}":
order_id = database.get_orders().first().id
Expand Down Expand Up @@ -138,9 +141,7 @@ With this hook, you can add additional test cases that will be executed in Hypot
context: schemathesis.hooks.HookContext,
examples: List[Case],
) -> None:
examples.append(
Case(endpoint=context.endpoint, query={"foo": "bar"})
)
examples.append(Case(endpoint=context.endpoint, query={"foo": "bar"}))
To load CLI hooks, you need to put them into a separate module and pass an importable path in the ``--pre-run`` CLI option.
For example, you have your hooks definition in ``myproject/hooks.py``, and ``myproject`` is importable:
Expand All @@ -161,17 +162,18 @@ This hook allows you to extend or redefine a list of CLI handlers that will be u
from schemathesis.cli.handlers import EventHandler
from schemathesis.runner import events
class SimpleHandler(EventHandler):
class SimpleHandler(EventHandler):
def handle_event(self, context, event):
if isinstance(event, events.Finished):
click.echo("Done!")
@schemathesis.hooks.register
def after_init_cli_run_handlers(
context: HookContext,
handlers: List[EventHandler],
execution_context: ExecutionContext
execution_context: ExecutionContext,
) -> None:
handlers[:] = [SimpleHandler()]
Expand All @@ -191,7 +193,9 @@ behavior in the API by changing the duplicate request's specific details.

.. code:: python
def add_case(context: HookContext, case: Case, response: GenericResponse) -> Optional[Case]:
def add_case(
context: HookContext, case: Case, response: GenericResponse
) -> Optional[Case]:
case.headers["Content-Type"] = "application/json"
return case
Expand All @@ -200,7 +204,9 @@ an additional test case if the original case received a successful response from

.. code:: python
def add_case(context: HookContext, case: Case, response: GenericResponse) -> Optional[Case]:
def add_case(
context: HookContext, case: Case, response: GenericResponse
) -> Optional[Case]:
if 200 <= response.status_code < 300:
# if the original case was successful, see if an invalid content type header produces a failure
case.headers["Content-Type"] = "invalid/content/type"
Expand All @@ -225,9 +231,7 @@ You can teach Schemathesis to generate values that fit this format by registerin

.. code-block:: python
strategy = strategies.from_regex(
r"\A4[0-9]{15}\Z"
).filter(luhn_validator)
strategy = strategies.from_regex(r"\A4[0-9]{15}\Z").filter(luhn_validator)
schemathesis.register_string_format("visa_cards", strategy)
Schemathesis test runner
Expand All @@ -243,6 +247,7 @@ It can run tests against the given schema URI and will do some simple checks for
events = runner.prepare("http://127.0.0.1:8080/swagger.json")
for event in events:
# do something with event
pass
``runner.prepare`` creates a generator that yields events of different kinds - ``BeforeExecution``, ``AfterExecution``, etc.
They provide a lot of useful information about what happens during tests, but your responsibility is handling these events.
Expand All @@ -256,9 +261,12 @@ You can provide your custom checks to the execute function; the check is a calla
from datetime import timedelta
from schemathesis import runner, models
def not_too_long(response, case: models.Case):
assert response.elapsed < timedelta(milliseconds=300)
events = runner.prepare("http://127.0.0.1:8080/swagger.json", checks=[not_too_long])
for event in events:
# do something with event
pass
5 changes: 2 additions & 3 deletions docs/graphql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ Usage
import schemathesis
schema = schemathesis.graphql.from_url(
"https://bahnql.herokuapp.com/graphql"
)
schema = schemathesis.graphql.from_url("https://bahnql.herokuapp.com/graphql")
@schema.parametrize()
@settings(deadline=None)
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Or in your Python tests:
schema = schemathesis.from_uri("http://example.com/swagger.json")
@schema.parametrize()
def test_api(case):
case.call_and_validate()
Expand Down

0 comments on commit b1e77fd

Please sign in to comment.