Skip to content

Commit

Permalink
refactor(test): Restructure test app
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Apr 1, 2021
1 parent beca586 commit 6d77911
Show file tree
Hide file tree
Showing 26 changed files with 71 additions and 76 deletions.
27 changes: 14 additions & 13 deletions test/apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,57 @@
from schemathesis.cli import CSVOption

try:
from . import _aiohttp, _flask, _graphql
from .utils import OpenAPIVersion, Operation
from . import _graphql, openapi
except ImportError as exc:
# try/except for cases when there is a different ImportError in the block before, that
# doesn't imply another running environment (test_server.sh vs usual pytest run)
# Ref: https://github.com/schemathesis/schemathesis/issues/658
try:
import _aiohttp
import _flask
import _graphql
from utils import OpenAPIVersion, Operation
import openapi
except ImportError:
raise exc


INVALID_OPERATIONS = ("invalid", "invalid_response", "invalid_path_parameter", "missing_path_parameter")
AvailableOperations = CSVOption(Operation)
AvailableOperations = CSVOption(openapi.schema.Operation)


@click.command()
@click.argument("port", type=int)
@click.option("--operations", type=AvailableOperations)
@click.option("--spec", type=click.Choice(["openapi2", "openapi3", "graphql"]), default="openapi2")
@click.option("--framework", type=click.Choice(["aiohttp", "flask"]), default="aiohttp")
def run_app(port: int, operations: List[Operation], spec: str, framework: str) -> None:
def run_app(port: int, operations: List[openapi.schema.Operation], spec: str, framework: str) -> None:
if spec == "graphql":
app = _graphql.create_app()
app = _graphql._flask.create_app()
app.run(port=port)
else:
if operations is not None:
prepared_operations = tuple(operation.name for operation in operations)
if "all" in prepared_operations:
prepared_operations = tuple(operation.name for operation in Operation if operation.name != "all")
prepared_operations = tuple(
operation.name for operation in openapi.schema.Operation if operation.name != "all"
)
else:
prepared_operations = tuple(
operation.name
for operation in Operation
for operation in openapi.schema.Operation
if operation.name not in INVALID_OPERATIONS and operation.name != "all"
)
version = {"openapi2": OpenAPIVersion("2.0"), "openapi3": OpenAPIVersion("3.0")}[spec]
version = {"openapi2": openapi.schema.OpenAPIVersion("2.0"), "openapi3": openapi.schema.OpenAPIVersion("3.0")}[
spec
]
click.secho(
f"Schemathesis test server is running!\n\n"
f"API Schema is available at: http://0.0.0.0:{port}/schema.yaml\n",
bold=True,
)
if framework == "aiohttp":
app = _aiohttp.create_openapi_app(prepared_operations, version)
app = openapi._aiohttp.create_app(prepared_operations, version)
web.run_app(app, port=port)
elif framework == "flask":
app = _flask.create_openapi_app(prepared_operations, version)
app = openapi._flask.create_app(prepared_operations, version)
app.run(port=port)


Expand Down
3 changes: 0 additions & 3 deletions test/apps/_aiohttp/app.py

This file was deleted.

3 changes: 0 additions & 3 deletions test/apps/_flask/app.py

This file was deleted.

13 changes: 1 addition & 12 deletions test/apps/_graphql/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
from flask import Flask
from graphql_server.flask import GraphQLView

from .schema import schema


def create_app(path="/graphql", **kwargs):
app = Flask("test_app")
app.add_url_rule(
path, view_func=GraphQLView.as_view("graphql", schema=schema.graphql_schema, graphiql=True, **kwargs)
)
return app
from . import _flask
12 changes: 12 additions & 0 deletions test/apps/_graphql/_flask/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from flask import Flask
from graphql_server.flask import GraphQLView

from ..schema import schema


def create_app(path="/graphql", **kwargs):
app = Flask("test_app")
app.add_url_rule(
path, view_func=GraphQLView.as_view("graphql", schema=schema.graphql_schema, graphiql=True, **kwargs)
)
return app
File renamed without changes.
1 change: 1 addition & 0 deletions test/apps/openapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import _aiohttp, _fastapi, _flask, schema
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import yaml
from aiohttp import web

from ..schema import OpenAPIVersion, Operation, make_openapi_schema
from . import handlers

try:
from ..utils import OpenAPIVersion, Operation, make_openapi_schema
except (ImportError, ValueError):
from utils import OpenAPIVersion, Operation, make_openapi_schema


def create_openapi_app(
def create_app(
operations: Tuple[str, ...] = ("success", "failure"), version: OpenAPIVersion = OpenAPIVersion("2.0")
) -> web.Application:
"""Factory for aioHTTP app.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from aiohttp import web

try:
from ..utils import PAYLOAD_VALIDATOR
from ..schema import PAYLOAD_VALIDATOR
except (ImportError, ValueError):
from utils import PAYLOAD_VALIDATOR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi import FastAPI, HTTPException, Query
from pydantic import BaseModel, Field

from ..utils import OpenAPIVersion
from ..schema import OpenAPIVersion


class User(BaseModel):
Expand Down
3 changes: 3 additions & 0 deletions test/apps/openapi/_fastapi/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import create_app

app = create_app()
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
from flask import Flask, Response, _request_ctx_stack, jsonify, request
from werkzeug.exceptions import BadRequest, GatewayTimeout, InternalServerError

try:
from ..utils import PAYLOAD_VALIDATOR, OpenAPIVersion, make_openapi_schema
except (ImportError, ValueError):
from utils import PAYLOAD_VALIDATOR, OpenAPIVersion, make_openapi_schema
from ..schema import PAYLOAD_VALIDATOR, OpenAPIVersion, make_openapi_schema


def expect_content_type(value: str):
Expand All @@ -23,7 +20,7 @@ def expect_content_type(value: str):
raise InternalServerError(f"Expected {value} payload")


def create_openapi_app(
def create_app(
operations: Tuple[str, ...] = ("success", "failure"), version: OpenAPIVersion = OpenAPIVersion("2.0")
) -> Flask:
app = Flask("test_app")
Expand Down
3 changes: 3 additions & 0 deletions test/apps/openapi/_flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import create_app

app = create_app()
File renamed without changes.
18 changes: 9 additions & 9 deletions test/cli/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
import sys
import time
from test.apps import OpenAPIVersion
from test.apps.openapi.schema import OpenAPIVersion
from test.utils import HERE, SIMPLE_PATH
from urllib.parse import urljoin

Expand Down Expand Up @@ -416,9 +416,9 @@ def test_load_schema_arguments_headers_to_loader_for_app(testdir, cli, mocker):

module = testdir.make_importable_pyfile(
location="""
from test.apps._flask import create_openapi_app
from test.apps.openapi._flask import create_app
app = create_openapi_app()
app = create_app()
"""
)
cli.run("/schema.yaml", "--app", f"{module.purebasename}:app", "-H", "Authorization: Bearer 123")
Expand Down Expand Up @@ -1310,9 +1310,9 @@ async def test_multiple_files_schema(openapi_2_app, testdir, cli, hypothesis_max
def test_wsgi_app(testdir, cli):
module = testdir.make_importable_pyfile(
location="""
from test.apps._flask import create_openapi_app
from test.apps.openapi._flask import create_app
app = create_openapi_app()
app = create_app()
"""
)
result = cli.run("/schema.yaml", "--app", f"{module.purebasename}:app")
Expand All @@ -1323,7 +1323,7 @@ def test_wsgi_app(testdir, cli):
def test_wsgi_app_exception(testdir, cli):
module = testdir.make_importable_pyfile(
location="""
from test.apps._flask import create_openapi_app
from test.apps.openapi._flask import create_app
1 / 0
"""
Expand All @@ -1337,7 +1337,7 @@ def test_wsgi_app_exception(testdir, cli):
def test_wsgi_app_missing(testdir, cli):
module = testdir.make_importable_pyfile(
location="""
from test.apps._flask import create_openapi_app
from test.apps.openapi._flask import create_app
"""
)
result = cli.run("/schema.yaml", "--app", f"{module.purebasename}:app")
Expand All @@ -1350,9 +1350,9 @@ def test_wsgi_app_missing(testdir, cli):
def test_wsgi_app_internal_exception(testdir, cli):
module = testdir.make_importable_pyfile(
location="""
from test.apps._flask import create_openapi_app
from test.apps.openapi._flask import create_app
app = create_openapi_app()
app = create_app()
app.config["internal_exception"] = True
"""
)
Expand Down
2 changes: 1 addition & 1 deletion test/code_samples/test_curl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from test.apps._fastapi.app import app
from test.apps.openapi._fastapi.app import app

import pytest

Expand Down
21 changes: 11 additions & 10 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from schemathesis.extra._flask import run_server as run_flask_server
from schemathesis.specs.openapi import loaders as oas_loaders

from .apps import Operation, _aiohttp, _fastapi, _flask, _graphql
from .apps.utils import OpenAPIVersion
from .apps import _graphql
from .apps.openapi import _aiohttp, _fastapi, _flask
from .apps.openapi.schema import OpenAPIVersion, Operation
from .utils import get_schema_path, make_schema

pytest_plugins = ["pytester", "aiohttp.pytest_plugin", "pytest_mock"]
Expand Down Expand Up @@ -50,7 +51,7 @@ def pytest_configure(config):
@pytest.fixture(scope="session")
def _app():
"""A global AioHTTP application with configurable API operations."""
return _aiohttp.create_openapi_app(("success", "failure"))
return _aiohttp.create_app(("success", "failure"))


@pytest.fixture
Expand Down Expand Up @@ -159,7 +160,7 @@ def graphql_path():

@pytest.fixture(scope="session")
def graphql_app(graphql_path):
return _graphql.create_app(graphql_path)
return _graphql._flask.create_app(graphql_path)


@pytest.fixture()
Expand Down Expand Up @@ -582,7 +583,7 @@ def run_and_assert(*args, **kwargs):

@pytest.fixture
def wsgi_app_factory():
return _flask.create_openapi_app
return _flask.create_app


@pytest.fixture()
Expand Down Expand Up @@ -610,9 +611,9 @@ def make_importable(module):
def loadable_flask_app(testdir, operations):
module = testdir.make_importable_pyfile(
location=f"""
from test.apps._flask import create_openapi_app
from test.apps.openapi._flask import create_app
app = create_openapi_app({operations})
app = create_app({operations})
"""
)
return f"{module.purebasename}:app"
Expand All @@ -622,9 +623,9 @@ def loadable_flask_app(testdir, operations):
def loadable_aiohttp_app(testdir, operations):
module = testdir.make_importable_pyfile(
location=f"""
from test.apps._aiohttp import create_openapi_app
from test.apps.openapi._aiohttp import create_app
app = create_openapi_app({operations})
app = create_app({operations})
"""
)
return f"{module.purebasename}:app"
Expand All @@ -634,7 +635,7 @@ def loadable_aiohttp_app(testdir, operations):
def loadable_fastapi_app(testdir, operations):
module = testdir.make_importable_pyfile(
location=f"""
from test.apps._fastapi import create_app
from test.apps.openapi._fastapi import create_app
app = create_app({operations})
"""
Expand Down
4 changes: 2 additions & 2 deletions test/extra/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from schemathesis.extra._aiohttp import run_server

from ..apps import _aiohttp
from ..apps.openapi import _aiohttp


def test_exact_port():
app = _aiohttp.create_openapi_app(("success", "failure"))
app = _aiohttp.create_app(("success", "failure"))
run_server(app, 8999)
response = requests.get("http://localhost:8999/schema.yaml")
assert response.status_code == 200
2 changes: 1 addition & 1 deletion test/runner/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import json
import platform
from test.apps import OpenAPIVersion
from test.apps.openapi.schema import OpenAPIVersion
from typing import Dict, Optional

import attr
Expand Down
2 changes: 1 addition & 1 deletion test/specs/graphql/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_(request, case):
def test_from_wsgi(testdir, graphql_path):
testdir.make_test(
f"""
from test.apps._graphql.app import app
from test.apps._graphql._flask.app import app
schema = schemathesis.graphql.from_wsgi("{graphql_path}", app=app)
Expand Down
2 changes: 1 addition & 1 deletion test/specs/openapi/test_stateful.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from test.apps import OpenAPIVersion
from test.apps.openapi.schema import OpenAPIVersion

import pytest
import requests
Expand Down
6 changes: 2 additions & 4 deletions test/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
from schemathesis import Case
from schemathesis.specs.openapi.loaders import from_asgi

from .apps._fastapi import create_app


@pytest.fixture()
def schema():
return from_asgi("/openapi.json", create_app())
def schema(fastapi_app):
return from_asgi("/openapi.json", fastapi_app)


@pytest.mark.hypothesis_nested
Expand Down
2 changes: 1 addition & 1 deletion test/test_parametrization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import schemathesis
from schemathesis.parameters import PayloadAlternatives

from .apps import OpenAPIVersion
from .apps.openapi.schema import OpenAPIVersion
from .utils import integer


Expand Down
2 changes: 1 addition & 1 deletion test/test_stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from schemathesis.stateful import ParsedData
from schemathesis.utils import NOT_SET

from .apps.utils import OpenAPIVersion
from .apps.openapi.schema import OpenAPIVersion


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion test/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_app_with_parametrize(testdir):
testdir.makepyfile(
"""
import schemathesis
from test.apps._flask.app import app
from test.apps.openapi._flask.app import app
from hypothesis import settings
schema = schemathesis.from_wsgi("/schema.yaml", app)
Expand Down

0 comments on commit 6d77911

Please sign in to comment.