1
0
Fork 0
mirror of https://github.com/shouptech/flask-tutorial.git synced 2026-02-03 07:29:42 +00:00

Raise exception when variables are missing

Raises a ConfigError exception when configuration variables are missing.
This commit is contained in:
Emma 2018-11-04 08:41:43 -07:00
parent 1003dcd1ff
commit a6162261d9
4 changed files with 60 additions and 20 deletions

View file

@ -4,14 +4,33 @@ jobs:
docker: docker:
- image: python:3 - image: python:3
environment:
SECRET_KEY: DEV
steps: steps:
- checkout - checkout
- run: pip install pytest coverage - run:
- run: pip install -e . name: Install prerequisites
- run: coverage run -m pytest command: |
- run: coverage report -m pip install pytest coverage
- run: pip install codecov pip install -e .
- run: codecov
- run:
name: Create config file
command: |
mkdir -p instance
echo << EOF
FLASK_APP='flaskr'
FLASK_ENV='development'
SECRET_KEY='test'
SQLALCHEMY_DATABASE_URI='sqlite:////tmp/flaskr.sqlite.db'
EOF > instance/config.py
- run:
name: Run coverage tests
command: |
coverage run -m pytest
coverage report -m
- run:
name: Upload to codecov.io
command: |
pip install codecov
codecov

View file

@ -2,16 +2,18 @@ import os
from flask import Flask from flask import Flask
from flaskr.exceptions import ConfigError
def create_app(test_config=None): def create_app(test_config=None):
# create and configure the app # create and configure the app
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping( app.config.from_mapping(
SECRET_KEY=os.environ.get("SECRET_KEY", default=None), SECRET_KEY=os.environ.get('SECRET_KEY', default=None),
SQLALCHEMY_TRACK_MODIFICATIONS=os.environ.get( SQLALCHEMY_TRACK_MODIFICATIONS=os.environ.get(
"SQLALCHEMY_TRACK_MODIFICATIONS", default=False), 'SQLALCHEMY_TRACK_MODIFICATIONS', default=False),
SQLALCHEMY_DATABASE_URI=os.environ.get( SQLALCHEMY_DATABASE_URI=os.environ.get(
"SQLALCHEMY_DATABASE_URI", default=None) 'SQLALCHEMY_DATABASE_URI', default=None)
) )
if test_config is None: if test_config is None:
@ -21,11 +23,10 @@ def create_app(test_config=None):
# load the test config if passed in # load the test config if passed in
app.config.from_mapping(test_config) app.config.from_mapping(test_config)
# ensure the instance folder exists if app.config['SECRET_KEY'] is None:
try: raise ConfigError("SECRET_KEY is not defined")
os.makedirs(app.instance_path) if app.config['SQLALCHEMY_DATABASE_URI'] is None:
except OSError: raise ConfigError("SQLALCHEMY_DATABASE_URI is not defined")
pass
from . import db from . import db
db.init_app(app) db.init_app(app)

7
flaskr/exceptions.py Normal file
View file

@ -0,0 +1,7 @@
class Error(Exception):
"""Base class for exceptions"""
pass
class ConfigError(Error):
"""Raised when a needed configuration is missing"""
pass

View file

@ -1,6 +1,19 @@
import pytest
from flaskr import create_app from flaskr import create_app
from flaskr.exceptions import ConfigError
def test_config(): @pytest.mark.parametrize('config', (
assert not create_app().testing {},
assert create_app({'TESTING': True}).testing {'SECRET_KEY':'test'}
))
def test_bad_config(config):
try:
create_app(config).testing
assert False
except ConfigError:
assert True
def test_good_config():
assert create_app(None)