-
Notifications
You must be signed in to change notification settings - Fork 16
[BD-21] Extract waffle_utils from edx-platform #70
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
34ed7c6
Upgrade django-waffle to match edx-platform version
regisb 88f63d8
Don't pin zipp too precisely
regisb 0dbf80a
Do not override root url in django app!
regisb 8770eeb
[BD-21] Extract WaffleSwitch from edx-platform
regisb 1d7e7a7
Add django-crum as a base requirement
regisb 824c268
Add dependency on edx-django-utils
regisb 414d01b
[BD-21] Extract waffle flag classes from edx-platform
regisb 7543f93
Fix SettingDictToggle constructor
regisb 7a6290d
Import waffle_utils/testutils.py module from edx-platform
regisb 4c8bb69
Fix pii_check test
regisb e2e4c65
Simplify testutils' override
regisb cada11b
Extract caching and monitoring features from edx-platform's waffle_utils
regisb d7f9c01
Move back override features from Waffle classes to edx-platform
regisb 2ae9f15
Get rid of the _get_waffle_flag_custom_attributes_set function
regisb 4f690e6
Release v1.0.0
regisb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """ | ||
| Tests for waffle utils test utilities. | ||
| """ | ||
|
|
||
|
|
||
| import crum | ||
| from django.test import TestCase | ||
| from django.test.client import RequestFactory | ||
| from edx_django_utils.cache import RequestCache | ||
|
|
||
| from edx_toggles.toggles import WaffleFlag, WaffleFlagNamespace | ||
| from edx_toggles.toggles.testutils import override_waffle_flag | ||
|
|
||
|
|
||
| class OverrideWaffleFlagTests(TestCase): | ||
| """ | ||
| Tests for the override_waffle_flag decorator/context manager. | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| super(OverrideWaffleFlagTests, self).setUp() | ||
| namespace_name = "test_namespace" | ||
| flag_name = "test_flag" | ||
| self.namespaced_flag_name = namespace_name + "." + flag_name | ||
| self.namespace = WaffleFlagNamespace(namespace_name) | ||
| self.waffle_flag = WaffleFlag(self.namespace, flag_name) | ||
|
|
||
| request = RequestFactory().request() | ||
| crum.set_current_request(request) | ||
| RequestCache.clear_all_namespaces() | ||
|
|
||
| self.addCleanup(crum.set_current_request, None) | ||
| self.addCleanup(RequestCache.clear_all_namespaces) | ||
|
|
||
| def temporarily_enable_flag(self): | ||
| """ | ||
| Temporarily override flag. | ||
| """ | ||
|
|
||
| @override_waffle_flag(self.waffle_flag, True) | ||
| def test_func(): | ||
| """ | ||
| Decorated test function. | ||
| """ | ||
| self.assertTrue(self.waffle_flag.is_enabled()) | ||
|
|
||
| test_func() | ||
|
|
||
| def test_override_waffle_flag_pre_cached(self): | ||
| # checks and caches the is_enabled value | ||
| self.assertFalse(self.waffle_flag.is_enabled()) | ||
| # pylint: disable=protected-access | ||
| flag_cache = self.namespace._cached_flags | ||
| self.assertIn(self.namespaced_flag_name, flag_cache) | ||
|
|
||
| self.temporarily_enable_flag() | ||
|
|
||
| # test cached flag is restored | ||
| self.assertIn(self.namespaced_flag_name, flag_cache) | ||
| self.assertFalse(self.waffle_flag.is_enabled()) | ||
|
|
||
| def test_override_waffle_flag_not_pre_cached(self): | ||
| # check that the flag is not yet cached | ||
| # pylint: disable=protected-access | ||
| flag_cache = self.namespace._cached_flags | ||
| self.assertNotIn(self.namespaced_flag_name, flag_cache) | ||
|
|
||
| self.temporarily_enable_flag() | ||
|
|
||
| # test cache is removed when no longer using decorator/context manager | ||
| self.assertNotIn(self.namespaced_flag_name, flag_cache) | ||
|
|
||
| def test_override_waffle_flag_as_context_manager(self): | ||
| self.assertFalse(self.waffle_flag.is_enabled()) | ||
|
|
||
| with override_waffle_flag(self.waffle_flag, True): | ||
| self.assertTrue(self.waffle_flag.is_enabled()) | ||
|
|
||
| self.assertFalse(self.waffle_flag.is_enabled()) | ||
|
|
||
| def test_interlocked_overrides(self): | ||
| waffle_flag1 = self.waffle_flag | ||
| waffle_flag2 = WaffleFlag(self.namespace, waffle_flag1.flag_name + "2") | ||
| # pylint: disable=protected-access | ||
| self.namespace._cached_flags[waffle_flag2.namespaced_flag_name] = True | ||
|
|
||
| self.assertFalse(waffle_flag1.is_enabled()) | ||
| self.assertTrue(waffle_flag2.is_enabled()) | ||
|
|
||
| with override_waffle_flag(waffle_flag1, True): | ||
| with override_waffle_flag(waffle_flag2, False): | ||
| self.assertTrue(waffle_flag1.is_enabled()) | ||
| self.assertFalse(waffle_flag2.is_enabled()) | ||
|
|
||
| self.assertFalse(waffle_flag1.is_enabled()) | ||
| self.assertTrue(waffle_flag2.is_enabled()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| """ | ||
| Expose public feature toggle API. | ||
| """ | ||
| from .internal import SettingDictToggle, SettingToggle | ||
| from .internal.setting_toggle import SettingDictToggle, SettingToggle | ||
| from .internal.waffle import WaffleFlag, WaffleFlagNamespace, WaffleSwitch, WaffleSwitchNamespace |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| """ | ||
| This module includes all code related to feature toggles. Remember to import publicly available classes and functions | ||
| in toggles/__init__.py. | ||
| """ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """ | ||
| Feature toggle base classes | ||
| """ | ||
|
|
||
| from abc import ABC | ||
|
|
||
|
|
||
| class BaseToggle(ABC): | ||
| """ | ||
| This abstract base class exposes the basic API required by toggle classes. Toggle instances are tracked in the | ||
| ``_class_instances`` class method, which is exposed via the ``get_instances`` class method. | ||
| """ | ||
|
|
||
| # Each child class should implement its own cache of class instances, for instance via WeakSet objects. | ||
| _class_instances = None | ||
|
|
||
| def __init__(self, name, default=False, module_name=""): | ||
| self.name = name | ||
| self.default = default | ||
| self.module_name = module_name | ||
| self._class_instances.add(self) | ||
|
|
||
| def is_enabled(self): | ||
| raise NotImplementedError | ||
|
|
||
| @classmethod | ||
| def get_instances(cls): | ||
| """ | ||
| Return the list of class instances sorted by name. | ||
| """ | ||
| return sorted(cls._class_instances, key=lambda instance: instance.name) | ||
|
|
||
|
|
||
| class BaseNamespace(ABC): | ||
|
robrap marked this conversation as resolved.
|
||
| """ | ||
| A base class for a request cached namespace for waffle flags/switches. | ||
|
|
||
| An instance of this class represents a single namespace | ||
| (e.g. "course_experience"), and can be used to work with a set of | ||
| flags or switches that will all share this namespace. | ||
| """ | ||
|
|
||
| def __init__(self, name, log_prefix=None): | ||
| """ | ||
| Initializes the waffle namespace instance. | ||
|
|
||
| Arguments: | ||
| name (String): Namespace string appended to start of all waffle | ||
| flags and switches (e.g. "grades") | ||
| log_prefix (String): Optional string to be appended to log messages | ||
| (e.g. "Grades: "). Defaults to ''. | ||
|
|
||
| """ | ||
| assert name, "The name is required." | ||
| self.name = name | ||
| self.log_prefix = log_prefix if log_prefix else "" | ||
|
robrap marked this conversation as resolved.
|
||
|
|
||
| def _namespaced_name(self, setting_name): | ||
| """ | ||
| Returns the namespaced name of the waffle switch/flag. | ||
|
|
||
| For example, the namespaced name of a waffle switch/flag would be: | ||
| my_namespace.my_setting_name | ||
|
|
||
| Arguments: | ||
| setting_name (String): The name of the flag or switch. | ||
| """ | ||
| return "{}.{}".format(self.name, setting_name) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.