Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First stab at testing all the examples #69

Merged
merged 6 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: python

python:
- 3.6

before_install:
- pip install -r requirements.txt

script:
- py.test -s -vv
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
.PHONY: run
run:
python wsgi.py

.PHONY: install
install:
python setup.py develop
@python setup.py develop

.PHONY: test
test:
Expand Down
Empty file added examples/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions examples/base_model_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ def get(self, team_id):
methods=['GET']
)


app.run(debug=True)
if __name__ == "__main__":
app.run(debug=True)
1 change: 0 additions & 1 deletion examples/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,5 @@ def colors(palette):

return jsonify(result)


if __name__ == "__main__":
app.run(debug=True)
1 change: 0 additions & 1 deletion examples/colors_from_specdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,5 @@ def colors(palette):

return jsonify(result)


if __name__ == "__main__":
app.run(debug=True)
3 changes: 2 additions & 1 deletion examples/colors_with_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ def get(self, palette):
methods=['GET']
)

app.run(debug=True)
if __name__ == "__main__":
app.run(debug=True)
4 changes: 3 additions & 1 deletion examples/definition_object_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,6 @@ def dispatch_request(self):


app.add_url_rule('/dispatch_request', view_func=Foo.as_view('foo'))
app.run(debug=True)

if __name__ == "__main__":
app.run(debug=True)
1 change: 0 additions & 1 deletion examples/example_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,5 @@ def hello():
</p>
"""


if __name__ == "__main__":
app.run(debug=True)
3 changes: 3 additions & 0 deletions examples/example_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ def usernames2(username):

if __name__ == "__main__":
app.run(debug=True)

if __name__ == "__main__":
app.run(debug=True)
4 changes: 2 additions & 2 deletions examples/marshmallow_apispec.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ def post(self):
methods=['POST']
)


app.run(debug=True)
if __name__ == "__main__":
app.run(debug=True)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ apispec
flask-restful
pep8==1.5.7
flake8==2.4.1
pytest
flex

# install flasgger itself as editable
-e .
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
from importlib import import_module

import pytest


EXAMPLES_DIR = "examples/"


def remove_suffix(fpath):
"""Remove all file ending suffixes"""
return os.path.splitext(fpath)[0]


def is_python_file(fpath):
"""Naive Python module filterer"""
return ".py" in fpath and "__" not in fpath


def pathify(basenames):
"""*nix to python module path"""
example = EXAMPLES_DIR.replace("/", ".")
return [example + basename for basename in basenames]


@pytest.fixture
def examples():
"""All example modules"""
all_files = os.listdir(EXAMPLES_DIR)
python_files = [f for f in all_files if is_python_file(f)]
basenames = [remove_suffix(f) for f in python_files]
return [import_module(module) for module in pathify(basenames)]
19 changes: 19 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json

from flex.core import validate

# allow for different spec routes in examples
spec_urls = [
"/apispec_1.json",
"/v1/spec",
]


def test_validate_example_specs(examples):
for example in examples:
app = example.app.test_client()
responses = [app.get(url) for url in spec_urls]
response = next(filter(lambda r: r.status_code == 200, responses))
decoded = response.data.decode("utf-8")
spec = json.loads(decoded)
validate(spec)