Skip to content

Commit

Permalink
Merge pull request #170 from boxine/feature-flag-reset
Browse files Browse the repository at this point in the history
Feature Flag: add reset()
  • Loading branch information
flbraun authored Oct 28, 2024
2 parents 10eaecf + 9b22e16 commit c963a11
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Feature flags: https://github.com/boxine/bx_django_utils/blob/master/bx_django_u

#### bx_django_utils.feature_flags.data_classes

* [`FeatureFlag()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/feature_flags/data_classes.py#L18-L164) - A feature flag that persistent the state into django cache/database.
* [`FeatureFlag()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/feature_flags/data_classes.py#L18-L168) - A feature flag that persistent the state into django cache/database.

#### bx_django_utils.feature_flags.test_utils

Expand Down
4 changes: 4 additions & 0 deletions bx_django_utils/feature_flags/data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def _add2cache(self, state_value: int) -> bool:
cache.set(self.cache_key, state_value, timeout=None) # set forever
return bool(state_value)

def reset(self) -> None:
FeatureFlagModel.objects.filter(cache_key=self.cache_key).delete()
cache.delete(self.cache_key)

@property
def is_enabled(self) -> bool:
try:
Expand Down
22 changes: 22 additions & 0 deletions bx_django_utils/feature_flags/tests/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bx_django_utils.feature_flags import data_classes
from bx_django_utils.feature_flags.data_classes import FeatureFlag
from bx_django_utils.feature_flags.exceptions import FeatureFlagDisabled, NotUniqueFlag
from bx_django_utils.feature_flags.models import FeatureFlagModel
from bx_django_utils.feature_flags.state import State
from bx_django_utils.feature_flags.test_utils import FeatureFlagTestCaseMixin, get_feature_flag_db_info
from bx_django_utils.feature_flags.utils import if_feature, validate_cache_key
Expand Down Expand Up @@ -247,3 +248,24 @@ def increment():
with self.assertRaisesMessage(FeatureFlagDisabled, ''):
increment()
self.assertEqual(some_var, 1)


class IsolatedFeatureFlagsTestCase(FeatureFlagTestCaseMixin, TestCase):
"""""" # noqa - Don't add to README

def test_reset(self):
flag = FeatureFlag(
cache_key='reset-me',
human_name='Testing reset',
initial_enabled=False,
)
flag.enable()
self.assertTrue(FeatureFlagModel.objects.filter(cache_key=flag.cache_key).exists())
self.assertNotEqual(cache.get(flag.cache_key), None)

flag.reset()
self.assertFalse(FeatureFlagModel.objects.filter(cache_key=flag.cache_key).exists())
self.assertEqual(cache.get(flag.cache_key), None)

# Reset again – should not error
flag.reset()

0 comments on commit c963a11

Please sign in to comment.