Initial commit

This commit is contained in:
Alan Pryor 2019-05-18 11:22:18 -04:00
commit 2677021a05
9 changed files with 236 additions and 0 deletions

122
.gitignore vendored Normal file
View File

@ -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

7
README.md Normal file
View File

@ -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`

17
app/__init__.py Normal file
View File

@ -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

14
app/__init__test.py Normal file
View File

@ -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'

0
app/client/__init__.py Normal file
View File

View File

@ -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'

37
app/config.py Normal file
View File

@ -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}

0
app/test/__init__.py Normal file
View File

23
app/test/fixtures.py Normal file
View File

@ -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()