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:
parent
1003dcd1ff
commit
a6162261d9
4 changed files with 60 additions and 20 deletions
|
|
@ -4,14 +4,33 @@ jobs:
|
|||
docker:
|
||||
- image: python:3
|
||||
|
||||
environment:
|
||||
SECRET_KEY: DEV
|
||||
|
||||
steps:
|
||||
- checkout
|
||||
- run: pip install pytest coverage
|
||||
- run: pip install -e .
|
||||
- run: coverage run -m pytest
|
||||
- run: coverage report -m
|
||||
- run: pip install codecov
|
||||
- run: codecov
|
||||
- run:
|
||||
name: Install prerequisites
|
||||
command: |
|
||||
pip install pytest coverage
|
||||
pip install -e .
|
||||
|
||||
- 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
|
||||
|
|
|
|||
|
|
@ -2,16 +2,18 @@ import os
|
|||
|
||||
from flask import Flask
|
||||
|
||||
from flaskr.exceptions import ConfigError
|
||||
|
||||
|
||||
def create_app(test_config=None):
|
||||
# create and configure the app
|
||||
app = Flask(__name__, instance_relative_config=True)
|
||||
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", default=False),
|
||||
'SQLALCHEMY_TRACK_MODIFICATIONS', default=False),
|
||||
SQLALCHEMY_DATABASE_URI=os.environ.get(
|
||||
"SQLALCHEMY_DATABASE_URI", default=None)
|
||||
'SQLALCHEMY_DATABASE_URI', default=None)
|
||||
)
|
||||
|
||||
if test_config is None:
|
||||
|
|
@ -21,11 +23,10 @@ def create_app(test_config=None):
|
|||
# load the test config if passed in
|
||||
app.config.from_mapping(test_config)
|
||||
|
||||
# ensure the instance folder exists
|
||||
try:
|
||||
os.makedirs(app.instance_path)
|
||||
except OSError:
|
||||
pass
|
||||
if app.config['SECRET_KEY'] is None:
|
||||
raise ConfigError("SECRET_KEY is not defined")
|
||||
if app.config['SQLALCHEMY_DATABASE_URI'] is None:
|
||||
raise ConfigError("SQLALCHEMY_DATABASE_URI is not defined")
|
||||
|
||||
from . import db
|
||||
db.init_app(app)
|
||||
|
|
|
|||
7
flaskr/exceptions.py
Normal file
7
flaskr/exceptions.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class Error(Exception):
|
||||
"""Base class for exceptions"""
|
||||
pass
|
||||
|
||||
class ConfigError(Error):
|
||||
"""Raised when a needed configuration is missing"""
|
||||
pass
|
||||
|
|
@ -1,6 +1,19 @@
|
|||
import pytest
|
||||
|
||||
from flaskr import create_app
|
||||
from flaskr.exceptions import ConfigError
|
||||
|
||||
|
||||
def test_config():
|
||||
assert not create_app().testing
|
||||
assert create_app({'TESTING': True}).testing
|
||||
@pytest.mark.parametrize('config', (
|
||||
{},
|
||||
{'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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue