This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from iromli/inline-featureflags
Add custom handler to add inline feature flags
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from flask import current_app | ||
from flask.ext.featureflags import FEATURE_FLAGS_CONFIG | ||
from flask.ext.featureflags import NoFeatureFlagFound | ||
from flask.ext.featureflags import log | ||
|
||
|
||
class InlineFeatureFlag(object): | ||
def __call__(self, feature): | ||
if not current_app: | ||
log.warn(u"Got a request to check for {feature} but we're outside the request context. Returning False".format(feature=feature)) | ||
return False | ||
|
||
feature_cfg = "{prefix}_{feature}".format(prefix=FEATURE_FLAGS_CONFIG, feature=feature) | ||
|
||
try: | ||
return current_app.config[feature_cfg] | ||
except KeyError: | ||
raise NoFeatureFlagFound() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import unittest | ||
|
||
import flask_featureflags as feature_flags | ||
from flask_featureflags.contrib.inline import InlineFeatureFlag | ||
|
||
from tests.fixtures import app | ||
from tests.fixtures import feature_setup | ||
|
||
|
||
inline_feature_flag = InlineFeatureFlag() | ||
|
||
|
||
class InlineFeatureFlagTest(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
feature_setup.add_handler(inline_feature_flag) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
feature_setup.clear_handlers() | ||
|
||
def setUp(self): | ||
self.app_ctx = app.app_context() | ||
self.app_ctx.push() | ||
app.config["FEATURE_FLAGS_ACTIVE"] = True | ||
app.config["FEATURE_FLAGS_INACTIVE"] = False | ||
|
||
def tearDown(self): | ||
self.app_ctx.pop() | ||
|
||
def test_flag_active(self): | ||
self.assertTrue(feature_flags.is_active("ACTIVE")) | ||
|
||
def test_flag_inactive(self): | ||
self.assertFalse(feature_flags.is_active("INACTIVE")) | ||
|
||
def test_flag_not_found(self): | ||
self.assertFalse(feature_flags.is_active("NOT_FOUND")) | ||
|
||
def test_flag_not_found_raise_handler_exception(self): | ||
self.assertRaises(feature_flags.NoFeatureFlagFound, | ||
inline_feature_flag, "NOT_FOUND") |