<commit_before>import unittest
from nose.tools import *
from gargoyle.models import Switch, Manager, Condition
from modeldict.dict import MemoryDict
import mock


switch = Switch('test')


class TestSwitch(unittest.TestCase):

    def test_switch_has_state_constants(self):
        self.assertTrue(Switch.states.DISABLED)
        self.assertTrue(Switch.states.SELECTIVE)
        self.assertTrue(Switch.states.GLOBAL)

    def test_no_switch_state_is_equal_to_another(self):
        states = (Switch.states.DISABLED, Switch.states.SELECTIVE,
                  Switch.states.GLOBAL)
        eq_(list(states), list(set(states)))

    def test_switch_constructs_with_a_name_attribute(self):
        eq_(Switch('foo').name, 'foo')

    def test_switch_strs_the_name_argument(self):
        eq_(Switch(name=12345).name, '12345')

    def test_switch_state_defaults_to_disabled(self):
        eq_(Switch('foo').state, 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_switch_compounded_defaults_to_false(self):
        eq_(Switch('foo').compounded, False)

    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_swtich_can_be_constructed_with_a_compounded_val(self):
        switch = Switch(name='foo', compounded=True)
        eq_(switch.compounded, True)

    def test_conditions_defaults_to_an_empty_list(self):
        eq_(Switch('foo').conditions, [])

    def test_parent_property_defaults_to_none(self):
        eq_(Switch('foo').parent, None)

    def test_can_be_constructed_with_parent(self):
        eq_(Switch('foo', parent='dog').parent, 'dog')

    def test_concent_defaults_to_true(self):
        eq_(Switch('foo').concent, True)

    def test_can_be_constructed_with_concent(self):
        eq_(Switch('foo', concent=False).concent, False)

    def test_children_defaults_to_an_empty_list(self):
        eq_(Switch('foo').children, [])


class TestCondition(unittest.TestCase):

    class ReflectiveInput(object):

        def foo(self):
            return (42, self)

    def setUp(self):
        self.operator = mock.Mock(name='operator')
        self.operator.applies_to.return_value = True
        self.condition = Condition(self.ReflectiveInput.foo, self.operator)

    def test_returns_false_if_input_is_not_same_class_as_argument_class(self):
        eq_(self.condition(object()), False)

    def test_returns_results_from_calling_operator_with_argument_value(self):
        """
        This test verifies that when a condition is called with an instance of
        an Input as the argument, the vaue that the condition's operator is
        asked if it applies to is calculated by calling the condition's own
        argument function as bound to the instance of the Input originally
        passed in to the condition.

        By using the ReflectiveInput class, we can verify that it was called
        with expected arguments, which are returned in a tuple with an extra
        value (42), and that that tuple is passed to the operator's applied_to
        method.
        """

        input_instance = self.ReflectiveInput()
        self.condition(input_instance)
        self.operator.applies_to.assert_called_once_with((42, input_instance))

    def test_condition_can_be_negated(self):
        eq_(self.condition(self.ReflectiveInput()), True)
        self.condition.negative = True
        eq_(self.condition(self.ReflectiveInput()), False)

    def test_can_be_negated_via_init_argument(self):
        condition = Condition(self.ReflectiveInput.foo, self.operator)
        eq_(condition(self.ReflectiveInput()), True)
        condition = Condition(self.ReflectiveInput.foo, self.operator, negative=True)
        eq_(condition(self.ReflectiveInput()), False)


class SwitchWithConditions(object):

    def setUp(self):
        self.switch = Switch('with conditions')
        self.switch.conditions.append(self.pessamistic_condition)
        self.switch.conditions.append(self.pessamistic_condition)

    @property
    def pessamistic_condition(self):
        mck = mock.MagicMock()
        mck.return_value = False
        return mck

    def test_condtions_can_be_added_and_removed(self):
        condition = self.pessamistic_condition
        ok_(condition not in self.switch.conditions)

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

        self.switch.conditions.remove(condition)
        ok_(condition not in self.switch.conditions)


class ConcentTest(SwitchWithConditions, unittest.TestCase):

    def setUp(self):
        super(ConcentTest, self).setUp()
        self.parent = mock.Mock()
        self.parent.enabled_for.return_value = False<commit_msg>Fixup switch test to allow more tests for signals.<commit_after>import unittest
from nose.tools import *
from gargoyle.models import Switch, Manager, Condition
from modeldict.dict import MemoryDict
import mock


switch = Switch('test')


class TestSwitch(unittest.TestCase):

    def test_switch_has_state_constants(self):
        self.assertTrue(Switch.states.DISABLED)
        self.assertTrue(Switch.states.SELECTIVE)
        self.assertTrue(Switch.states.GLOBAL)

    def test_no_switch_state_is_equal_to_another(self):
        states = (Switch.states.DISABLED, Switch.states.SELECTIVE,
                  Switch.states.GLOBAL)
        eq_(list(states), list(set(states)))

    def test_switch_constructs_with_a_name_attribute(self):
        eq_(Switch('foo').name, 'foo')

    def test_switch_strs_the_name_argument(self):
        eq_(Switch(name=12345).name, '12345')

    def test_switch_state_defaults_to_disabled(self):
        eq_(Switch('foo').state, 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_switch_compounded_defaults_to_false(self):
        eq_(Switch('foo').compounded, False)

    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_swtich_can_be_constructed_with_a_compounded_val(self):
        switch = Switch(name='foo', compounded=True)
        eq_(switch.compounded, True)

    def test_conditions_defaults_to_an_empty_list(self):
        eq_(Switch('foo').conditions, [])

    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 test_parent_property_defaults_to_none(self):
        eq_(Switch('foo').parent, None)

    def test_can_be_constructed_with_parent(self):
        eq_(Switch('foo', parent='dog').parent, 'dog')

    def test_concent_defaults_to_true(self):
        eq_(Switch('foo').concent, True)

    def test_can_be_constructed_with_concent(self):
        eq_(Switch('foo', concent=False).concent, False)

    def test_children_defaults_to_an_empty_list(self):
        eq_(Switch('foo').children, [])


class TestCondition(unittest.TestCase):

    class ReflectiveInput(object):

        def foo(self):
            return (42, self)

    def setUp(self):
        self.operator = mock.Mock(name='operator')
        self.operator.applies_to.return_value = True
        self.condition = Condition(self.ReflectiveInput.foo, self.operator)

    def test_returns_false_if_input_is_not_same_class_as_argument_class(self):
        eq_(self.condition(object()), False)

    def test_returns_results_from_calling_operator_with_argument_value(self):
        """
        This test verifies that when a condition is called with an instance of
        an Input as the argument, the vaue that the condition's operator is
        asked if it applies to is calculated by calling the condition's own
        argument function as bound to the instance of the Input originally
        passed in to the condition.

        By using the ReflectiveInput class, we can verify that it was called
        with expected arguments, which are returned in a tuple with an extra
        value (42), and that that tuple is passed to the operator's applied_to
        method.
        """

        input_instance = self.ReflectiveInput()
        self.condition(input_instance)
        self.operator.applies_to.assert_called_once_with((42, input_instance))

    def test_condition_can_be_negated(self):
        eq_(self.condition(self.ReflectiveInput()), True)
        self.condition.negative = True
        eq_(self.condition(self.ReflectiveInput()), False)

    def test_can_be_negated_via_init_argument(self):
        condition = Condition(self.ReflectiveInput.foo, self.operator)
        eq_(condition(self.ReflectiveInput()), True)
        condition = Condition(self.ReflectiveInput.foo, self.operator, negative=True)
        eq_(condition(self.ReflectiveInput()), False)


class SwitchWithConditions(object):

    def setUp(self):
        self.switch = Switch('with conditions')
        self.switch.conditions.append(self.pessamistic_condition)
        self.switch.conditions.append(self.pessamistic_condition)

    @property
    def pessamistic_condition(self):
        mck = mock.MagicMock()
        mck.return_value = False
        return mck


class ConcentTest(SwitchWithConditions, unittest.TestCase):

    def setUp(self):
        super(ConcentTest, self).setUp()
        self.parent = mock.Mock()
        self.parent.enabled_for.return_value = False
{'fraction_non_alphanumeric': 0.09326167015029362, 'fraction_numerical': 0.0031850303573205934, 'mean_word_length': 3.4717400979083224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '618960', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2902, 'n_words': 586}