From 59503be4d0787e53b441966786ed375b8cff2615 Mon Sep 17 00:00:00 2001 From: Philipp Hagemeister Date: Mon, 28 Oct 2024 23:37:10 +0100 Subject: [PATCH] FeatureFlag: Add __str__ and __repr__ Store the additional name. In retrospect, it was a bad idea to name the `__init__` kwarg `cache_key`, when that has another meaning. --- README.md | 2 +- bx_django_utils/feature_flags/data_classes.py | 8 ++++++++ .../feature_flags/tests/test_feature_flags.py | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac4fb68..6deaf1f 100644 --- a/README.md +++ b/README.md @@ -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-L168) - 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-L176) - A feature flag that persistent the state into django cache/database. #### bx_django_utils.feature_flags.test_utils diff --git a/bx_django_utils/feature_flags/data_classes.py b/bx_django_utils/feature_flags/data_classes.py index fb3bc00..1b24ec5 100644 --- a/bx_django_utils/feature_flags/data_classes.py +++ b/bx_django_utils/feature_flags/data_classes.py @@ -36,6 +36,7 @@ def __init__( validate_cache_key(cache_key) validate_cache_key(cache_key_prefix) + self.name = cache_key self.cache_key = f'{cache_key_prefix}-{cache_key}' validate_cache_key(self.cache_key) # Double check ;) @@ -166,3 +167,10 @@ def get_by_cache_key(cls, cache_key) -> "FeatureFlag": def __bool__(self): return self.is_enabled + + def __str__(self): + return f'' + + def __repr__(self): + initial = self.initial_state == State.ENABLED + return f'FeatureFlag(cache_key={self.name!r}, human_name={self.human_name!r}, initial_enabled={initial!r})' diff --git a/bx_django_utils/feature_flags/tests/test_feature_flags.py b/bx_django_utils/feature_flags/tests/test_feature_flags.py index 6b06529..ef4e269 100644 --- a/bx_django_utils/feature_flags/tests/test_feature_flags.py +++ b/bx_django_utils/feature_flags/tests/test_feature_flags.py @@ -269,3 +269,12 @@ def test_reset(self): # Reset again – should not error flag.reset() + + def test_str(self): + flag = FeatureFlag( + cache_key='be-wild', + human_name='Be wild', + initial_enabled=False, + ) + self.assertEqual(str(flag), '') + self.assertEqual(repr(flag), "FeatureFlag(cache_key='be-wild', human_name='Be wild', initial_enabled=False)")