Skip to content

Commit

Permalink
Merge pull request #61 from 0b01001001/dev
Browse files Browse the repository at this point in the history
format code
  • Loading branch information
kemingy authored Oct 6, 2020
2 parents 9079144 + 940ae80 commit a09736a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: Python package

on:
pull_request:
push:
branches:
- master
- dev
push:

jobs:
build:
Expand All @@ -29,7 +29,7 @@ jobs:
- name: Lint with flake8
run: |
pip install flake8
make style
make lint
- name: Test with pytest
run: |
pip install pytest
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
check: style test
check: lint test

install:
pip install -e .
Expand All @@ -20,7 +20,7 @@ package: clean
publish: package
twine upload dist/*

style:
lint:
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

setup(
name='spectree',
version='0.3.6',
version='0.3.7',
author='Keming Yang',
author_email='kemingy94@gmail.com',
description=('generate OpenAPI document and validate request&response '
Expand Down
26 changes: 14 additions & 12 deletions spectree/plugins/flask_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class FlaskPlugin(BasePlugin):
def find_routes(self):
from flask import current_app
if self.blueprint_state:
excludes = [f"{self.blueprint_state.blueprint.name}.{ep}"
for ep in ["static", "openapi"]+[f"doc_page_{ui}" for ui in PAGES]]
excludes = [f'{self.blueprint_state.blueprint.name}.{ep}'
for ep in ['static', 'openapi'] + [f'doc_page_{ui}' for ui in PAGES]]
for rule in current_app.url_map.iter_rules():
if self.blueprint_state.url_prefix and not str(rule).startswith(self.blueprint_state.url_prefix):
if self.blueprint_state.url_prefix and \
not str(rule).startswith(self.blueprint_state.url_prefix):
continue
if rule.endpoint in excludes:
continue
Expand All @@ -36,7 +37,7 @@ def parse_func(self, route):
func = self.blueprint_state.app.view_functions[route.endpoint]
else:
func = self.app.view_functions[route.endpoint]

for method in route.methods:
yield method, func

Expand Down Expand Up @@ -168,22 +169,23 @@ def register_route(self, app):

if isinstance(app, Blueprint):
def gen_doc_page(ui):
state = self.blueprint_state

spec_url = self.config.spec_url
if state.url_prefix is not None:
spec_url = "/".join((state.url_prefix.rstrip("/"), self.config.spec_url.lstrip("/")))

if self.blueprint_state.url_prefix is not None:
spec_url = '/'.join((
self.blueprint_state.url_prefix.rstrip('/'),
self.config.spec_url.lstrip('/'))
)

return PAGES[ui].format(spec_url)

for ui in PAGES:
app.add_url_rule(
f'/{self.config.PATH}/{ui}',
f'doc_page_{ui}',
lambda ui=ui: gen_doc_page(ui)
)
app.record(lambda state: setattr(self, "blueprint_state", state))

app.record(lambda state: setattr(self, 'blueprint_state', state))
else:
for ui in PAGES:
self.app.add_url_rule(
Expand Down
32 changes: 20 additions & 12 deletions tests/test_plugin_flask_blueprint.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
from random import randint

import pytest
import json
from flask import Flask, Blueprint, jsonify, request

from spectree import SpecTree, Response

from .common import Query, Resp, JSON, Headers, Cookies


Expand Down Expand Up @@ -51,31 +51,36 @@ def user_score(name):

api.register(app)


@pytest.fixture
def client(request):
parent_app = Flask(__name__)
parent_app.register_blueprint(app, url_prefix=request.param)
with parent_app.test_client() as client:
yield client

@pytest.mark.parametrize("client, prefix", [(None, ""), ("/prefix","/prefix")], indirect=["client"])

@pytest.mark.parametrize(
('client', 'prefix'),
[(None, ''), ('/prefix', '/prefix')],
indirect=['client'])
def test_flask_validate(client, prefix):
resp = client.get(prefix+'/ping')
resp = client.get(prefix + '/ping')
assert resp.status_code == 422
assert resp.headers.get('X-Error') == 'Validation Error'

resp = client.get(prefix+'/ping', headers={'lang': 'en-US'})
resp = client.get(prefix + '/ping', headers={'lang': 'en-US'})
assert resp.json == {'msg': 'pong'}
assert resp.headers.get('X-Error') is None
assert resp.headers.get('X-Validation') == 'Pass'

resp = client.post(prefix+'/api/user/flask')
resp = client.post(prefix + '/api/user/flask')
assert resp.status_code == 422
assert resp.headers.get('X-Error') == 'Validation Error'

client.set_cookie('flask', 'pub', 'abcdefg')
resp = client.post(
prefix+'/api/user/flask?order=1',
prefix + '/api/user/flask?order=1',
data=json.dumps(dict(name='flask', limit=10)),
content_type='application/json',
)
Expand All @@ -86,20 +91,23 @@ def test_flask_validate(client, prefix):
assert resp.json['score'] == sorted(resp.json['score'], reverse=True)

resp = client.post(
prefix+'/api/user/flask?order=0',
prefix + '/api/user/flask?order=0',
data=json.dumps(dict(name='flask', limit=10)),
content_type='application/json',
)
assert resp.json['score'] == sorted(resp.json['score'], reverse=False)


@pytest.mark.parametrize("client, prefix", [(None, ""), ("/prefix","/prefix")], indirect=["client"])
@pytest.mark.parametrize(
('client', 'prefix'),
[(None, ''), ('/prefix', '/prefix')],
indirect=['client'])
def test_flask_doc(client, prefix):
resp = client.get(prefix+'/apidoc/openapi.json')
resp = client.get(prefix + '/apidoc/openapi.json')
assert resp.json == api.spec

resp = client.get(prefix+'/apidoc/redoc')
resp = client.get(prefix + '/apidoc/redoc')
assert resp.status_code == 200

resp = client.get(prefix+'/apidoc/swagger')
resp = client.get(prefix + '/apidoc/swagger')
assert resp.status_code == 200

0 comments on commit a09736a

Please sign in to comment.