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)
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')
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))
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)
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))
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)
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)
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)
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)
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)
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 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 ]
def expected_switch(self): return Switch( name='name', label='label', description='description', state=1, compounded=False, concent=False, )
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)
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)
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)
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
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)
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')
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')
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'))
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)
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
def test_switch_can_be_constructed_with_a_description(self): eq_( Switch('foo', description='A description').description, 'A description')
def test_switch_has_label(self): ok_(Switch('foo').label is None)
def test_switch_name_is_immutable(self): switch = Switch("foo") with self.assertRaises(AttributeError): switch.name = "bar"
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")
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)
def test_switch_strs_the_name_argument(self): eq_(Switch(name=12345).name, '12345')
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)
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')
def test_switch_has_description(self): ok_(Switch('foo').description is None)
def test_conditions_defaults_to_an_empty_list(self): eq_(Switch('foo').conditions, [])
def test_swtich_can_be_constructed_with_a_compounded_val(self): switch = Switch(name='foo', compounded=True) eq_(switch.compounded, True)
def test_swtich_can_be_constructed_with_a_state(self): switch = Switch(name='foo', state=Switch.states.GLOBAL) eq_(switch.state, Switch.states.GLOBAL)
def test_switch_compounded_defaults_to_false(self): eq_(Switch('foo').compounded, False)
def test_switch_constructs_with_a_name_attribute(self): eq_(Switch('foo').name, 'foo')
def add_and_remove_switch(self): gutter.register(Switch('foo')) yield gutter.flush()
def test_switch_state_defaults_to_disabled(self): eq_(Switch('foo').state, Switch.states.DISABLED)
def test_switch_can_be_constructed_with_a_label(self): eq_(Switch('foo', label='A label').label, 'A label')