From 2677021a055f51de532c4ba0a0904f7e3b4ae4d2 Mon Sep 17 00:00:00 2001 From: Alan Pryor Date: Sat, 18 May 2019 11:22:18 -0400 Subject: [PATCH] Initial commit --- .gitignore | 122 ++++++++++++++++++++++++++++++++++++ README.md | 7 +++ app/__init__.py | 17 +++++ app/__init__test.py | 14 +++++ app/client/__init__.py | 0 app/client/requests_test.py | 16 +++++ app/config.py | 37 +++++++++++ app/test/__init__.py | 0 app/test/fixtures.py | 23 +++++++ 9 files changed, 236 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app/__init__.py create mode 100644 app/__init__test.py create mode 100644 app/client/__init__.py create mode 100644 app/client/requests_test.py create mode 100644 app/config.py create mode 100644 app/test/__init__.py create mode 100644 app/test/fixtures.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..131955a --- /dev/null +++ b/.gitignore @@ -0,0 +1,122 @@ +.idea/ +*.pyc +py3/ +data/ +build/ +dist/ +.pytest_cache/ +.vscode/ + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST +*.pkl + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.idea + +*.pptx + +misc/ +.Rproj.user + +fte/app-test.db diff --git a/README.md b/README.md new file mode 100644 index 0000000..022268e --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# flask_testing_examples + +A collection of examples for testing Flask applications + +## Running the tests + +All tests in this repository can be run by installing PyTest with `pip install pytest` and invoking the command `pytest` diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..3150c72 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,17 @@ +from flask import Flask, jsonify +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + + +def create_app(env=None): + from fte.config import config_by_name + app = Flask(__name__) + app.config.from_object(config_by_name[env or 'test']) + + db.init_app(app) + + @app.route('/health') + def health(): + return jsonify('healthy') + return app diff --git a/app/__init__test.py b/app/__init__test.py new file mode 100644 index 0000000..8dd503c --- /dev/null +++ b/app/__init__test.py @@ -0,0 +1,14 @@ +from fte.test.fixtures import app, client # noqa + + +def test_app_creates(app): # noqa + assert app + + +def test_app_healthy(app, client): # noqa + #with app.app_context(): + with client: + resp = client.get('/health') + assert resp.status_code == 200 + assert resp.is_json + assert resp.json == 'healthy' diff --git a/app/client/__init__.py b/app/client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/client/requests_test.py b/app/client/requests_test.py new file mode 100644 index 0000000..34e8af7 --- /dev/null +++ b/app/client/requests_test.py @@ -0,0 +1,16 @@ +from fte import create_app +from fte.decorators import accepts + +from fte.test.fixtures import app, client # noqa + + +def test_reqparse(app): + @app.route('/hello_world') + def respond(): + from flask import jsonify + return jsonify('Hello, World') + with app.test_client() as cl: + response = cl.get('/hello_world') + + assert response.status_code == 200 + assert response.json == 'Hello, World' diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..50de454 --- /dev/null +++ b/app/config.py @@ -0,0 +1,37 @@ +import os + +basedir = os.path.abspath(os.path.dirname(__file__)) + + +class BaseConfig: + USE_MOCK_EQUIVALENCY = False + DEBUG = False + + +class DevelopmentConfig(BaseConfig): + CONFIG_NAME = 'dev' + SECRET_KEY = os.getenv( + "DEV_SECRET_KEY", "You can't see California without Marlon Brando's eyes") + DEBUG = True + TESTING = False + SQLALCHEMY_DATABASE_URI = "sqlite:///{0}/app-dev.db".format(basedir) + + +class TestingConfig(BaseConfig): + CONFIG_NAME = 'test' + SECRET_KEY = os.getenv("TEST_SECRET_KEY", "Thanos did nothing wrong") + DEBUG = True + TESTING = True + SQLALCHEMY_DATABASE_URI = "sqlite:///{0}/app-test.db".format(basedir) + + +class ProductionConfig(BaseConfig): + CONFIG_NAME = 'prod' + SECRET_KEY = os.getenv("PROD_SECRET_KEY", "I'm Ron Burgundy?") + DEBUG = False + TESTING = False + SQLALCHEMY_DATABASE_URI = "sqlite:///{0}/app-prod.db".format(basedir) + + +EXPORT_CONFIGS = [DevelopmentConfig, TestingConfig, ProductionConfig] +config_by_name = {cfg.CONFIG_NAME: cfg for cfg in EXPORT_CONFIGS} diff --git a/app/test/__init__.py b/app/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/test/fixtures.py b/app/test/fixtures.py new file mode 100644 index 0000000..377de72 --- /dev/null +++ b/app/test/fixtures.py @@ -0,0 +1,23 @@ +import pytest + +from fte import create_app + + +@pytest.fixture +def app(): + return create_app('test') + + +@pytest.fixture +def client(app): + return app.test_client() + + +@pytest.fixture +def db(app): + from fte import db + with app.app_context(): + db.create_all() + yield db + db.drop_all() + db.session.commit()