diff --git a/.circleci/config.yml b/.circleci/config.yml index a56a65b..1d378ee 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/flaskr/__init__.py b/flaskr/__init__.py index 3974563..f817675 100644 --- a/flaskr/__init__.py +++ b/flaskr/__init__.py @@ -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) diff --git a/flaskr/exceptions.py b/flaskr/exceptions.py new file mode 100644 index 0000000..131ddee --- /dev/null +++ b/flaskr/exceptions.py @@ -0,0 +1,7 @@ +class Error(Exception): + """Base class for exceptions""" + pass + +class ConfigError(Error): + """Raised when a needed configuration is missing""" + pass diff --git a/tests/test_factory.py b/tests/test_factory.py index caaa0d2..c176c74 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -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)