From d931af2f3beee8058c48d3b8fa476d6649e802b4 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Fri, 2 Feb 2024 14:51:43 +0900 Subject: [PATCH 1/6] docs: document hot flags can be toggled in tests --- docs/testing.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 docs/testing.md diff --git a/docs/testing.md b/docs/testing.md new file mode 100644 index 0000000..b8ec154 --- /dev/null +++ b/docs/testing.md @@ -0,0 +1,38 @@ +# Testing + +## Testing code that depends on flags + +Because `FLAGS` are definable in Django settings, you can use Django's standard `override_settings` to test with a flag off and on. + +```python +class FlaggedCodeTestCase(TestCase): + @override_settings(FLAGS={"MY_FLAG": [("boolean", True)]}) + def test_flag_enabled(self): + # Do the thing that requires the flag to be enabled + + @override_settings(FLAGS={"MY_FLAG": [("boolean", False)]}) + def test_flag_disabled(self): + # Do the thing that requires the flag to be disabled +``` + +## Advanced + +If for some reason you've completely disabled the `flags.sources.SettingsFlagSource` and are only allow database flags, you can either use `override_settings` to change `FLAG_SOURCES` for the tests, or if you really want to use the database, you can create a `FlagStateobject` along the same lines with a boolean condition that is `True` to test the flag-enabled code path, and then create one with a boolean condition that is` False` to test the not-enabled path. + +It could look something like this: + + +```python +class FlaggedCodeTestCase(TestCase): + def test_flag_enabled(self): + FlagState.objects.create( + name="MY_FLAG", condition="boolean", value="True" + ) + # Do the thing that requires the flag to be enabled + + def test_flag_disabled(self): + FlagState.objects.create( + name="MY_FLAG", condition="boolean", value="True" + ) + # Do the thing that requires the flag to be disabled +``` From 818646ecf8b9aa6558cc7333195bb7e7c455ee11 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Mon, 5 Feb 2024 13:03:37 +0900 Subject: [PATCH 2/6] Update docs/testing.md Co-authored-by: Will Barton --- docs/testing.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/testing.md b/docs/testing.md index b8ec154..f4859bc 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -15,11 +15,7 @@ class FlaggedCodeTestCase(TestCase): # Do the thing that requires the flag to be disabled ``` -## Advanced - -If for some reason you've completely disabled the `flags.sources.SettingsFlagSource` and are only allow database flags, you can either use `override_settings` to change `FLAG_SOURCES` for the tests, or if you really want to use the database, you can create a `FlagStateobject` along the same lines with a boolean condition that is `True` to test the flag-enabled code path, and then create one with a boolean condition that is` False` to test the not-enabled path. - -It could look something like this: +Alternatively, you can create a `FlagState` object along the same lines with a boolean condition that is `True` to test the flag-enabled code path, and then create one with a boolean condition that is` False` to test the not-enabled path. ```python From 40aeb53ac4d57adb17e100fe589f78149f41e870 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Mon, 5 Feb 2024 13:14:09 +0900 Subject: [PATCH 3/6] add new page to ToC --- mkdocs.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/mkdocs.yml b/mkdocs.yml index d55f649..b923a71 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -5,6 +5,7 @@ nav: - Getting started: index.md - Usage guide: usage.md - Debugging: debugging.md + - Testing: testing.md - Settings: settings.md - Conditions: conditions.md - Management commands: management_commands.md From 9d91d5cf6a118291c29a30768c75cecdc98d5f4b Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Mon, 5 Feb 2024 13:15:42 +0900 Subject: [PATCH 4/6] Add testing quip to usage --- docs/usage.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/usage.md b/docs/usage.md index d1434bd..ecc52ab 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -65,4 +65,20 @@ urlpatterns = [ ] ``` +## Testing with flags + +A test case that covers both values of the flag may look like this: + +```python +class FlaggedCodeTestCase(TestCase): + @override_settings(FLAGS={"MY_FLAG": [("boolean", True)]}) + def test_flag_enabled(self): + # Do the thing that requires the flag to be enabled + + @override_settings(FLAGS={"MY_FLAG": [("boolean", False)]}) + def test_flag_disabled(self): + # Do the thing that requires the flag to be disabled +``` + + See the [API reference](/api/state) for more details and examples. From f4fce86294f7250ae6162c997da521a2d290373c Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Thu, 29 Feb 2024 09:48:38 +0900 Subject: [PATCH 5/6] add import Co-authored-by: Will Barton --- docs/testing.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/testing.md b/docs/testing.md index f4859bc..1442d86 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -5,6 +5,8 @@ Because `FLAGS` are definable in Django settings, you can use Django's standard `override_settings` to test with a flag off and on. ```python +from django.test import TestCase, override_settings + class FlaggedCodeTestCase(TestCase): @override_settings(FLAGS={"MY_FLAG": [("boolean", True)]}) def test_flag_enabled(self): From d98572f6545d502ba1d754053ae1a836d2418973 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Thu, 29 Feb 2024 09:48:47 +0900 Subject: [PATCH 6/6] add import Co-authored-by: Will Barton --- docs/usage.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/usage.md b/docs/usage.md index ecc52ab..d262430 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -70,6 +70,8 @@ urlpatterns = [ A test case that covers both values of the flag may look like this: ```python +from django.test import TestCase, override_settings + class FlaggedCodeTestCase(TestCase): @override_settings(FLAGS={"MY_FLAG": [("boolean", True)]}) def test_flag_enabled(self):