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

How to use flags in tests? #121

Open
dimaqq opened this issue Jan 31, 2024 · 1 comment
Open

How to use flags in tests? #121

dimaqq opened this issue Jan 31, 2024 · 1 comment

Comments

@dimaqq
Copy link
Contributor

dimaqq commented Jan 31, 2024

For example, I'd like to test 2 code paths, with flag on and off.
In my case the flag is only sourced from the database (not url params).

I imagine this is a common question, if there's a good answer already, I volunteer to add it to documentation.

@willbarton
Copy link
Member

willbarton commented Jan 31, 2024

@dimaqq Sure! Because FLAGS are definable in Django settings, you can use Django's standard override_settings to test with a flag off and on.

An example is in Django-Flags's own tests, where we test flagging URLs:

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

Here one tests overrides FLAGS in Django settings to set it with a boolean condition that is always True, to test the flag-enabled code path, and the other tests with it set to a boolean condition that is always False to test the code path when the flag is not enabled.

Unless you're overriding FLAG_SOURCES in settings to remove flags.sources.SettingsFlagsSource, this will work and will be database independent.

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 isTrueto test the flag-enabled code path, and then create one with a boolean condition that isFalse` to test the not-enabled path.

We don't have a direct example of this in Django-Flags's tests (because it's more complex, and is database-dependent), but the FlagState model tests show how you can do this. It could look something like this:

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

I hope this helps! Generally, there's nothing inherently unique about testing with flags enabled or disabled in Django-Flags, it uses pretty standard Django testing paradigms with either settings or models.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants