Skip to content

Commit

Permalink
Ensure wrapper is setup correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
timsavage committed May 10, 2022
1 parent 0fda360 commit 83265d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/pyapp/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def my_feature(arg):
from typing import Dict
from typing import Optional
from typing import Union
from functools import wraps

from pyapp.conf import settings
from pyapp.utils import text_to_bool
Expand Down Expand Up @@ -170,6 +171,7 @@ def if_enabled(
"""

def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if self._get(flag, default):
return func(*args, **kwargs)
Expand Down
36 changes: 32 additions & 4 deletions tests/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ class FeatureFlagsWrapper(feature_flags.FeatureFlags):

class TestFeatureFlags:
@pytest.mark.parametrize(
"flag, expected", (("enable-a", True), ("Enable b", False), ("ENABLE_C", None),)
"flag, expected",
(
("enable-a", True),
("Enable b", False),
("ENABLE_C", None),
),
)
def test_resolve_from_environment(self, monkeypatch, flag, expected):
monkeypatch.setenv("PYAPP_FLAG_ENABLE_A", "On")
Expand All @@ -22,7 +27,12 @@ def test_resolve_from_environment(self, monkeypatch, flag, expected):
assert actual is expected

@pytest.mark.parametrize(
"flag, expected", (("enable-a", True), ("Enable b", False), ("ENABLE_C", None),)
"flag, expected",
(
("enable-a", True),
("Enable b", False),
("ENABLE_C", None),
),
)
def test_resolve_from_settings(self, flag, expected):
with settings.modify() as patch:
Expand Down Expand Up @@ -102,7 +112,11 @@ def test_a_or_b(self, option_a, option_b, state, expected):
assert actual == expected

@pytest.mark.parametrize(
"state, expected", ((True, "ValueA"), (False, None),),
"state, expected",
(
(True, "ValueA"),
(False, None),
),
)
def test_if_enabled__where_return_is_default(self, state, expected):
target = FeatureFlagsWrapper()
Expand All @@ -117,7 +131,11 @@ def _sample(arg_a):
assert actual == expected

@pytest.mark.parametrize(
"state, expected", ((True, "ValueA"), (False, "ValueB"),),
"state, expected",
(
(True, "ValueA"),
(False, "ValueB"),
),
)
def test_if_enabled__where_return_is_customised(self, state, expected):
target = FeatureFlagsWrapper()
Expand All @@ -130,3 +148,13 @@ def _sample(arg_a):
actual = _sample("ValueA")

assert actual == expected

def test_if_enabled__where_is_wrapped_correctly(self):
target = FeatureFlagsWrapper()
target._get = Mock(return_value=True)

@target.if_enabled("FOO")
def foo():
"""Sample"""

assert "Sample" == foo.__doc__

0 comments on commit 83265d8

Please sign in to comment.