From c20b30245876a62be15e875b7fa16850851e9af4 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 23 Jul 2018 13:21:29 +0200 Subject: [PATCH] Add the alter_config context manager for testing It can temporarily change an app configuration value. --- tests/helpers.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/helpers.py b/tests/helpers.py index 474fba7..971a4bd 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -17,6 +17,8 @@ """Helper functions and fixtures for testing """ +from contextlib import contextmanager + import pytest import calsocial @@ -70,3 +72,22 @@ def database(): yield db db.drop_all() + + +@contextmanager +def alter_config(app, **kwargs): + saved = {} + + for key, value in kwargs.items(): + if key in app.config: + saved[key] = app.config[key] + + app.config[key] = value + + yield + + for key, value in kwargs.items(): + if key in saved: + app.config[key] = saved[key] + else: + del app.config[key]