Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: document how flags can be toggled in tests #122

Merged
merged 6 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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
from django.test import TestCase, override_settings

class FlaggedCodeTestCase(TestCase):
dimaqq marked this conversation as resolved.
Show resolved Hide resolved
@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
```

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
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
```
18 changes: 18 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,22 @@ urlpatterns = [
]
```

## Testing with flags

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):
dimaqq marked this conversation as resolved.
Show resolved Hide resolved
@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.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading