Exemple #1
0
    def test_switch_state_can_be_changed(self):
        switch = Switch('foo')
        old_state = switch.state

        switch.state = Switch.states.GLOBAL
        eq_(switch.state, Switch.states.GLOBAL)
        ok_(old_state is not switch.state)
Exemple #2
0
def create_switch_parent_and_child():
    """
    Heriarchical Switches, with children.
    :return:
    """
    _switches = [
        Switch(PARENT_SWITCH),
        # This one it will respect the parent status
        Switch(PARENT_SWITCH_CHILD_1, state=Switch.states.GLOBAL,
               concent=True),
        # It will return true even parent is disabled
        Switch(PARENT_SWITCH_CHILD_2,
               state=Switch.states.GLOBAL,
               concent=False)
    ]

    _ = [(manager.register(_switch), print_new_switch(_switch))
         for _switch in _switches]

    print_parent_switches(_switches, 'FALSE')

    manager.switch(PARENT_SWITCH).state = Switch.states.GLOBAL

    print('\n')

    print_parent_switches(_switches, 'TRUE')
Exemple #3
0
    def test_can_register_signals_and_get_notified(self):
        signals.switch_registered.connect(self.callback.switch_added)
        signals.switch_unregistered.connect(self.callback.switch_removed)
        signals.switch_updated.connect(self.callback.switch_updated)

        eq_(self.callback.register_calls, [])
        eq_(self.callback.unregister_calls, [])
        eq_(self.callback.update_calls, [])

        switch = Switch('foo')

        self.manager.register(switch)
        eq_(self.callback.register_calls, [switch])

        self.manager.unregister(switch.name)
        eq_(self.callback.unregister_calls, [switch])

        self.manager.register(switch)
        eq_(self.callback.register_calls, [switch, switch])

        switch.state = Switch.states.GLOBAL
        self.manager.update(switch)

        change = self.callback.update_calls[0]
        eq_(change[0], switch)
        changes = change[1]
        eq_(changes['state'], dict(current=Switch.states.GLOBAL, previous=Switch.states.DISABLED))
Exemple #4
0
    def test_switch_state_can_be_changed(self):
        switch = Switch("foo")
        old_state = switch.state

        switch.state = Switch.states.GLOBAL
        eq_(switch.state, Switch.states.GLOBAL)
        ok_(old_state is not switch.state)
Exemple #5
0
    def test_can_register_signals_and_get_notified(self):
        signals.switch_registered.connect(self.callback.switch_added)
        signals.switch_unregistered.connect(self.callback.switch_removed)
        signals.switch_updated.connect(self.callback.switch_updated)

        eq_(self.callback.register_calls, [])
        eq_(self.callback.unregister_calls, [])
        eq_(self.callback.update_calls, [])

        switch = Switch("foo")

        self.manager.register(switch)
        eq_(self.callback.register_calls, [switch])

        self.manager.unregister(switch.name)
        eq_(self.callback.unregister_calls, [switch])

        self.manager.register(switch)
        eq_(self.callback.register_calls, [switch, switch])

        switch.state = Switch.states.GLOBAL
        self.manager.update(switch)

        change = self.callback.update_calls[0]
        eq_(change[0], switch)
        changes = change[1]
        eq_(changes["state"], dict(current=Switch.states.GLOBAL, previous=Switch.states.DISABLED))
Exemple #6
0
    def test_switches_are_still_equal_with_different_managers(self):
        a = Switch("a")
        b = Switch("a")

        eq_(a, b)

        a.manager = "foo"
        b.manager = "bar"

        eq_(a, b)
Exemple #7
0
    def test_switches_are_still_equal_with_different_managers(self):
        a = Switch('a')
        b = Switch('a')

        eq_(a, b)

        a.manager = 'foo'
        b.manager = 'bar'

        eq_(a, b)
Exemple #8
0
    def test_retrieved_switches_can_be_updated(self):
        switch = Switch("foo")
        self.manager.register(switch)

        self.assertEquals(self.manager.switch("foo").name, "foo")

        switch.state = Switch.states.GLOBAL
        switch.save()

        self.assertEquals(self.manager.switch("foo").state, Switch.states.GLOBAL)
Exemple #9
0
    def test_switches_are_equal_if_they_have_the_same_properties(self):
        a = Switch('a')
        b = Switch('b')

        for prop, (a_value, b_value) in self.possible_properties:
            setattr(a, prop, a_value)
            setattr(b, prop, b_value)
            self.assertNotEqual(a, b, "expected %s to not be equals" % prop)

            setattr(b, prop, a_value)
            eq_(a, b, "expected %s to be equals" % prop)
Exemple #10
0
    def add_switch(self, name, condition=None, *conditions, **kwargs):
        switch = Switch(name, compounded=kwargs.get('compounded', False))
        switch.state = kwargs.get('state', Switch.states.SELECTIVE)
        conditions = list(conditions)

        if condition:
            conditions.append(condition)

        [switch.conditions.append(c) for c in conditions]
        kwargs.get('manager', self.manager).register(switch)
        return switch
    def test_retrieved_switches_can_be_updated(self):
        switch = Switch('foo')
        self.manager.register(switch)

        self.assertEquals(self.manager.switch('foo').name, 'foo')

        switch.state = Switch.states.GLOBAL
        switch.save()

        self.assertEquals(
            self.manager.switch('foo').state, Switch.states.GLOBAL)
Exemple #12
0
    def add_switch(self, name, condition=None, *conditions, **kwargs):
        switch = Switch(name, compounded=kwargs.get("compounded", False))
        switch.state = kwargs.get("state", Switch.states.SELECTIVE)
        conditions = list(conditions)

        if condition:
            conditions.append(condition)

        [switch.conditions.append(c) for c in conditions]
        kwargs.get("manager", self.manager).register(switch)
        return switch
Exemple #13
0
def create_selective_switch_enable_with_conditional():
    """
    Create a switch with type selective.
    :return:
    """
    _conditional_switch = Switch(ENABLE_WITH_CONDITIONAL,
                                 state=Switch.states.SELECTIVE)
    # _conditional_switch.name
    _condition = Condition(
        argument=UserArgument,
        attribute='age',
        operator=MoreThan(lower_limit=50),
        # negative=True
    )

    _conditional_switch.conditions.append(_condition)

    manager.register(_conditional_switch)

    print_new_switch(_conditional_switch)

    ### Validate ###
    _users = [
        User('Alisson'),
        User('Fred', 40),
        User('Elisson', 70),
        User('Ulisses', 51)
    ]

    [
        print('--> {} :: {} :: {}'.format(
            ENABLE_WITH_CONDITIONAL, _u,
            manager.active(ENABLE_WITH_CONDITIONAL, _u))) for _u in _users
    ]
Exemple #14
0
 def expected_switch(self):
     return Switch(
         name='name',
         label='label',
         description='description',
         state=1,
         compounded=False,
         concent=False,
     )
Exemple #15
0
def create_simple_switch_enable():
    """
    Create a simple switch enabled.
    :return:
    """
    switch = Switch(SIMPLE_SWITCH_ENABLE, state=Switch.states.GLOBAL)

    manager.register(switch)

    print_and_check_flag(switch, SIMPLE_SWITCH_ENABLE)
Exemple #16
0
def create_simple_switch_disabled():
    """
    Create a simple swith disabled and register it.
    :return: anything
    """
    switch = Switch(SWITCH_DISABLED)

    manager.register(switch)

    print_and_check_flag(switch, SWITCH_DISABLED)
Exemple #17
0
    def test_condtions_can_be_added_and_removed(self):
        switch = Switch('foo')
        condition = lambda: False

        ok_(condition not in switch.conditions)

        switch.conditions.append(condition)
        ok_(condition in switch.conditions)

        switch.conditions.remove(condition)
        ok_(condition not in switch.conditions)
Exemple #18
0
    def to_object(self):
        switch = Switch(
            name=self.cleaned_data['name'],
            label=self.cleaned_data['label'],
            description=self.cleaned_data['description'],
            state=self.cleaned_data['state'],
            compounded=self.cleaned_data['compounded'],
            concent=self.cleaned_data['concent'],
        )

        return switch
Exemple #19
0
def create_simple_switch_enable_with_description():
    """
    Create a simple switch enabled with description.
    :return:
    """
    switch = Switch(ENABLE_WITH_DESCRIPTION,
                    state=Switch.states.GLOBAL,
                    description='Simple switch enabled with description \\o/')

    manager.register(switch)

    print_and_check_flag(switch, ENABLE_WITH_DESCRIPTION)
Exemple #20
0
    def test_retrieved_switches_can_be_updated(self):
        switch = Switch('foo')
        self.manager.register(switch)

        switch.name = 'steve'
        switch.save()

        self.assertRaises(ValueError, self.manager.switch, 'foo')
        self.assertEquals(self.manager.switch('steve').name, 'steve')

        switch.name = 'bob'
        switch.state = Switch.states.GLOBAL
        switch.save()

        self.assertEquals(self.manager.switch('bob').name, 'bob')
        self.assertEquals(
            self.manager.switch('bob').state, Switch.states.GLOBAL)
        self.assertRaises(ValueError, self.manager.switch, 'steve')
Exemple #21
0
    def test_switches_are_still_equal_with_different_managers(self):
        a = Switch('a')
        b = Switch('a')

        eq_(a, b)

        a.manager = 'foo'
        b.manager = 'bar'

        eq_(a, b)
Exemple #22
0
    def post(self):
        logging.info('[POST] -> Payload={}'.format(api.payload))
        _state = Switch.states.DISABLED
        _description = api.payload['id']

        if 'state' in api.payload:
            _state = translate_state(api.payload['state'])

        if 'description' in api.payload:
            _description = api.payload['description']

        _switch = Switch(api.payload['id'],
                         state=_state,
                         description=_description)
        manager.register(_switch)

        return prepare_to_return(_switch)
    def test_retrieved_switches_can_be_updated(self):
        switch = Switch('foo')
        self.manager.register(switch)

        switch.name = 'steve'
        switch.save()

        self.assertRaises(ValueError, self.manager.switch, 'foo')
        self.assertEquals(self.manager.switch('steve').name, 'steve')

        switch.name = 'bob'
        switch.state = Switch.states.GLOBAL
        switch.save()

        self.assertEquals(self.manager.switch('bob').name, 'bob')
        self.assertEquals(
            self.manager.switch('bob').state,
            Switch.states.GLOBAL
        )
        self.assertRaises(ValueError, self.manager.switch, 'steve')
Exemple #24
0
    def test_legacy_unpickle(self):
        d = EncodingDict()

        parent = Switch('a')
        switch = Switch('a:b')

        children = [
            Switch('a:b:c'),
            Switch('a:b:d'),
        ]

        [setattr(child, 'parent', switch) for child in children]

        switch.children = children
        switch.parent = parent

        decoded_switch = d._decode(d._encode(switch))
        self.assertEquals(decoded_switch.name, switch.name)
        self.assertEquals(decoded_switch.parent, switch.parent.name)
        self.assertListEqual([child.name for child in children], decoded_switch.children)
    def test_can_register_signals_and_get_notified(self):
        signals.switch_registered.connect(self.callback.switch_added)
        signals.switch_unregistered.connect(self.callback.switch_removed)
        signals.switch_updated.connect(self.callback.switch_updated)

        eq_(self.callback.register_calls, [])
        eq_(self.callback.unregister_calls, [])
        eq_(self.callback.update_calls, [])

        switch = Switch('foo')

        self.manager.register(switch)
        eq_(self.callback.register_calls, [switch])

        self.manager.unregister(switch.name)
        eq_(self.callback.unregister_calls, [switch])

        self.manager.register(switch)
        eq_(self.callback.register_calls, [switch, switch])

        switch.name = 'a new name'
        switch.state = Switch.states.GLOBAL
        self.manager.update(switch)

        change = self.callback.update_calls[0]
        eq_(change[0], switch)
        changes = change[1]
        eq_(changes['name'], dict(current='a new name', previous='foo'))
        eq_(changes['state'], dict(current=Switch.states.GLOBAL, previous=Switch.states.DISABLED))

        switch.name = 'from save() call'
        switch.save()

        change = self.callback.update_calls[1]
        eq_(change[0], switch)
        changes = change[1]
        eq_(changes['name'], dict(current='from save() call', previous='a new name'))
Exemple #26
0
 def test_switch_enabed_for_skips_switch_active_signal_when_not_enabled(self, signal):
     switch = Switch('foo', manager='manager', state=Switch.states.DISABLED)
     eq_(switch.enabled_for('causing input'), False)
     eq_(signal.call.called, False)
Exemple #27
0
 def build_and_register_switch(self, name, enabled_for=False):
     switch = Switch(name)
     switch.enabled_for = mock.Mock(return_value=enabled_for)
     self.manager.register(switch)
     return switch
Exemple #28
0
 def test_switch_can_be_constructed_with_a_description(self):
     eq_(
         Switch('foo', description='A description').description,
         'A description')
Exemple #29
0
 def test_switch_has_label(self):
     ok_(Switch('foo').label is None)
Exemple #30
0
 def test_switch_name_is_immutable(self):
     switch = Switch("foo")
     with self.assertRaises(AttributeError):
         switch.name = "bar"
Exemple #31
0
 def test_switch_enabed_for_calls_switch_active_signal_when_enabled(self, signal):
     switch = Switch("foo", state=Switch.states.GLOBAL)
     ok_(switch.enabled_for("causing input"))
     signal.call.assert_called_once_with(switch, "causing input")
Exemple #32
0
 def test_switch_enabed_for_skips_switch_active_signal_when_not_enabled(self, signal):
     switch = Switch("foo", state=Switch.states.DISABLED)
     eq_(switch.enabled_for("causing input"), False)
     eq_(signal.call.called, False)
Exemple #33
0
 def test_switch_strs_the_name_argument(self):
     eq_(Switch(name=12345).name, '12345')
Exemple #34
0
 def test_switch_enabed_for_calls_switch_checked_signal(self, signal):
     switch = Switch("foo")
     switch.enabled_for(True)
     signal.call.assert_called_once_with(switch)
Exemple #35
0
 def test_switch_enabed_for_calls_switch_active_signal_when_enabled(self, signal):
     switch = Switch('foo', manager='manager', state=Switch.states.GLOBAL)
     ok_(switch.enabled_for('causing input'))
     signal.call.assert_called_once_with(switch, 'causing input')
Exemple #36
0
 def test_switch_has_description(self):
     ok_(Switch('foo').description is None)
Exemple #37
0
 def build_and_register_switch(self, name, enabled_for=False):
     switch = Switch(name)
     switch.enabled_for = mock.Mock(return_value=enabled_for)
     self.manager.register(switch)
     return switch
Exemple #38
0
 def test_conditions_defaults_to_an_empty_list(self):
     eq_(Switch('foo').conditions, [])
Exemple #39
0
 def test_swtich_can_be_constructed_with_a_compounded_val(self):
     switch = Switch(name='foo', compounded=True)
     eq_(switch.compounded, True)
Exemple #40
0
 def test_swtich_can_be_constructed_with_a_state(self):
     switch = Switch(name='foo', state=Switch.states.GLOBAL)
     eq_(switch.state, Switch.states.GLOBAL)
Exemple #41
0
 def test_switch_compounded_defaults_to_false(self):
     eq_(Switch('foo').compounded, False)
Exemple #42
0
 def test_switch_constructs_with_a_name_attribute(self):
     eq_(Switch('foo').name, 'foo')
Exemple #43
0
 def add_and_remove_switch(self):
     gutter.register(Switch('foo'))
     yield
     gutter.flush()
Exemple #44
0
 def test_switch_state_defaults_to_disabled(self):
     eq_(Switch('foo').state, Switch.states.DISABLED)
Exemple #45
0
 def test_switch_can_be_constructed_with_a_label(self):
     eq_(Switch('foo', label='A label').label, 'A label')