~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/features/testing.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-06-25 08:55:37 UTC
  • mfrom: (13287.1.8 bug-800652)
  • Revision ID: launchpad@pqm.canonical.com-20110625085537-moikyoo2pe98zs7r
[r=jcsackett, julian-edwards][bug=800634,
        800652] Enable and display overrides on sync package uploads.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2010,2011 Canonical Ltd.  This software is licensed under the
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
"""Helpers for writing tests that use feature flags."""
5
5
 
6
6
__metaclass__ = type
7
 
__all__ = ['FeatureFixture']
 
7
__all__ = ['active_features']
8
8
 
9
9
 
10
10
from fixtures import Fixture
11
 
from lazr.restful.utils import get_current_browser_request
12
11
 
13
12
from lp.services.features import (
14
13
    get_relevant_feature_controller,
19
18
    Rule,
20
19
    StormFeatureRuleSource,
21
20
    )
22
 
from lp.services.features.scopes import ScopesFromRequest
23
21
 
24
22
 
25
23
class FeatureFixture(Fixture):
39
37
    The values are restored when the block exits.
40
38
    """
41
39
 
42
 
    def __init__(self, features_dict, full_feature_rules=None,
43
 
            override_scope_lookup=None):
 
40
    def __init__(self, features_dict):
44
41
        """Constructor.
45
42
 
46
43
        :param features_dict: A dictionary-like object with keys and values
47
44
            that are flag names and those flags' settings.
48
 
        :param override_scope_lookup: If non-None, an argument that takes
49
 
            a scope name and returns True if it matches.  If not specified, 
50
 
            scopes are looked up from the current request.
51
45
        """
52
46
        self.desired_features = features_dict
53
 
        self.full_feature_rules = full_feature_rules
54
 
        self.override_scope_lookup = override_scope_lookup
55
47
 
56
48
    def setUp(self):
57
49
        """Set the feature flags that this fixture is responsible for."""
63
55
        rule_source.setAllRules(self.makeNewRules())
64
56
 
65
57
        original_controller = get_relevant_feature_controller()
66
 
 
67
 
        def scope_lookup(scope_name):
68
 
            request = get_current_browser_request()
69
 
            return ScopesFromRequest(request).lookup(scope_name)
70
 
 
71
 
        if self.override_scope_lookup:
72
 
            scope_lookup = self.override_scope_lookup
73
58
        install_feature_controller(
74
 
            FeatureController(scope_lookup, rule_source))
 
59
            FeatureController(lambda _: True, rule_source))
75
60
        self.addCleanup(install_feature_controller, original_controller)
76
61
 
77
62
    def makeNewRules(self):
92
77
            for flag_name, value in self.desired_features.iteritems()
93
78
                if value is not None]
94
79
 
95
 
        if self.full_feature_rules is not None:
96
 
            new_rules.extend(
97
 
                Rule(**rule_spec)
98
 
                for rule_spec in self.full_feature_rules)
99
 
 
100
80
        return new_rules