Skip to content

Commit

Permalink
use the framework's own unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PWZER committed Aug 23, 2023
1 parent f8fb8f8 commit d0691cf
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-and-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ jobs:
python-version: ${{ matrix.python-version }}
- run: python setup.py install
- run: pip install flake8 isort -r test/requirements.txt
- run: isort --line-width=100 --check-only --force-single-line-imports .
- run: flake8
- run: pytest -s test/
- run: isort --check-only .
3 changes: 2 additions & 1 deletion examples/bottle_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from bottle import Bottle, run
from bottle import Bottle
from bottle import run

app = Bottle()

Expand Down
3 changes: 2 additions & 1 deletion examples/sanic_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from sanic import Sanic, response
from sanic import Sanic
from sanic import response

app = Sanic(__name__)

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from distutils.version import StrictVersion
from pathlib import Path

from setuptools import find_packages, setup
from setuptools import find_packages
from setuptools import setup

if StrictVersion(platform.python_version()) < StrictVersion('3.0.0'):
raise Exception("`swagger-ui-py` support python version >= 3.0.0 only.")
Expand Down
7 changes: 5 additions & 2 deletions swagger_ui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from distutils.version import StrictVersion
from pathlib import Path

from jinja2 import Environment, FileSystemLoader, select_autoescape
from jinja2 import Environment
from jinja2 import FileSystemLoader
from jinja2 import select_autoescape

from swagger_ui.handlers import supported_list
from swagger_ui.utils import SWAGGER_UI_PY_ROOT, _load_config
from swagger_ui.utils import SWAGGER_UI_PY_ROOT
from swagger_ui.utils import _load_config

_DefaultSwaggerUIBundleParameters = {
"dom_id": "\"#swagger-ui\"",
Expand Down
3 changes: 2 additions & 1 deletion swagger_ui/handlers/bottle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def handler(doc):
from bottle import request, static_file
from bottle import request
from bottle import static_file

@doc.app.get(doc.root_uri_absolute(slashes=True))
@doc.app.get(doc.root_uri_absolute(slashes=False))
Expand Down
4 changes: 3 additions & 1 deletion swagger_ui/handlers/chalice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
def handler(doc):
from pathlib import Path

from chalice import Blueprint, NotFoundError, Response
from chalice import Blueprint
from chalice import NotFoundError
from chalice import Response

cur_dir = Path(__file__).resolve().parent
static_dir = cur_dir.parent.joinpath("static")
Expand Down
3 changes: 2 additions & 1 deletion swagger_ui/handlers/flask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def handler(doc):
from flask import jsonify, request
from flask import jsonify
from flask import request
from flask.blueprints import Blueprint

swagger_blueprint = Blueprint(
Expand Down
3 changes: 2 additions & 1 deletion swagger_ui/handlers/quart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def handler(doc):
from quart import Blueprint, request
from quart import Blueprint
from quart import request
from quart.json import jsonify

swagger_blueprint = Blueprint(
Expand Down
3 changes: 2 additions & 1 deletion swagger_ui/handlers/starlette.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def handler(doc):
from starlette.responses import HTMLResponse, JSONResponse
from starlette.responses import HTMLResponse
from starlette.responses import JSONResponse
from starlette.staticfiles import StaticFiles

async def swagger_doc_handler(request):
Expand Down
3 changes: 2 additions & 1 deletion swagger_ui/handlers/tornado.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def handler(doc):
from tornado.web import RequestHandler, StaticFileHandler
from tornado.web import RequestHandler
from tornado.web import StaticFileHandler

class DocHandler(RequestHandler):
def get(self, *args, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion test/aiohttp_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from multiprocessing import Process

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from common import send_requests


def server_process(port, mode, **kwargs):
Expand Down
7 changes: 5 additions & 2 deletions test/bottle_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from multiprocessing import Process

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from common import send_requests


def server_process(port, mode, **kwargs):
from bottle import Bottle, run
from bottle import Bottle
from bottle import run

app = Bottle()

Expand Down
4 changes: 3 additions & 1 deletion test/chalice_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from multiprocessing import Process

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from common import send_requests


def server_process(port, mode, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion test/falcon_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from multiprocessing import Process

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from common import send_requests


def server_process(port, mode, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion test/flask_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from multiprocessing import Process

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from common import send_requests


def server_process(port, mode, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion test/quart_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from multiprocessing import Process

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from common import send_requests


def server_process(port, mode, **kwargs):
Expand Down
61 changes: 48 additions & 13 deletions test/sanic_test.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,66 @@
from multiprocessing import Process
from pathlib import Path

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from sanic import Sanic
from sanic import response
from sanic_testing.testing import SanicTestClient


def server_process(port, mode, **kwargs):
from sanic import Sanic, response
@pytest.fixture(scope="module")
def app():
app = Sanic("test_sanic")
return app

app = Sanic(__name__)

@pytest.mark.parametrize('mode', mode_list)
@pytest.mark.parametrize('kwargs', kwargs_list)
def test_sanic(app, port, mode, kwargs):
# app = Sanic(f"server_{port}")

@app.get(r'/hello/world')
async def index_handler(request):
return response.text('Hello World!!!')

if kwargs["config_rel_url"]:
@app.get(kwargs["config_rel_url"])
async def swagger_config_handler(request):
return response.json(Path(kwargs["config_path"]).read_text())

if mode == 'auto':
from swagger_ui import api_doc
api_doc(app, **kwargs)
else:
from swagger_ui import sanic_api_doc
sanic_api_doc(app, **kwargs)
app.run(host='localhost', port=port)

url_prefix = kwargs["url_prefix"]
if url_prefix.endswith("/"):
url_prefix = url_prefix[:-1]

@pytest.mark.parametrize('mode', mode_list)
@pytest.mark.parametrize('kwargs', kwargs_list)
def test_sanic(port, mode, kwargs):
proc = Process(target=server_process, args=(port, mode), kwargs=kwargs)
proc.start()
send_requests(port, mode, kwargs)
proc.terminate()
client = SanicTestClient(app)
_, resp = client.get("/hello/world")
# print(resp.text)
assert resp.status == 200, resp.text

_, resp = client.get(url_prefix)
# print(resp.text)
assert resp.status == 200, resp.text

_, resp = client.get(f"{url_prefix}/static/LICENSE")
# print(resp.text)
assert resp.status == 200, resp.text

if kwargs.get("editor"):
_, resp = client.get(f"{url_prefix}/editor")
# print(resp.text)
assert resp.status == 200, resp.text
else:
_, resp = client.get(f"{url_prefix}/editor")
# print(resp.text)
assert resp.status == 404, resp.text

_, resp = client.get(f"{url_prefix}/swagger.json")
# print(resp.text)
assert resp.status == 200, resp.text
4 changes: 3 additions & 1 deletion test/starlette_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from multiprocessing import Process

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from common import send_requests


def server_process(port, mode, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion test/tornado_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from multiprocessing import Process

import pytest
from common import kwargs_list, mode_list, send_requests
from common import kwargs_list
from common import mode_list
from common import send_requests


def server_process(port, mode, **kwargs):
Expand Down

0 comments on commit d0691cf

Please sign in to comment.