def test_serialize(self):
        """Testing ConditionSet.serialize"""
        basic_choice = BasicTestChoice()
        equals_choice = EqualsTestChoice()

        condition_set = ConditionSet(ConditionSet.MODE_ALL, [
            Condition(basic_choice,
                      basic_choice.get_operator('basic-test-op'),
                      'abc123'),
            Condition(equals_choice,
                      equals_choice.get_operator('equals-test-op'),
                      'def123'),
        ])

        result = condition_set.serialize()

        self.assertEqual(
            result,
            {
                'mode': 'all',
                'conditions': [
                    {
                        'choice': 'basic-test-choice',
                        'op': 'basic-test-op',
                        'value': 'abc123',
                    },
                    {
                        'choice': 'equals-test-choice',
                        'op': 'equals-test-op',
                        'value': 'def123',
                    },
                ],
            })
    def test_matches_with_any_mode_and_custom_value_kwargs_multiple(self):
        """Testing ConditionSet.matches with "any" mode and multiple custom
        value keyword arguments across multiple choices
        """
        class CustomEqualsChoice1(EqualsTestChoice):
            value_kwarg = 'my_value1'

        class CustomEqualsChoice2(EqualsTestChoice):
            value_kwarg = 'my_value2'

        choice1 = CustomEqualsChoice1()
        choice2 = CustomEqualsChoice2()

        condition_set = ConditionSet(ConditionSet.MODE_ANY, [
            Condition(choice1, choice1.get_operator('equals-test-op'),
                      'abc123'),
            Condition(choice2, choice2.get_operator('equals-test-op'),
                      'def456'),
        ])

        self.assertTrue(condition_set.matches(my_value1='abc123',
                                              my_value2='def456'))
        self.assertTrue(condition_set.matches(my_value1='abc123'))
        self.assertTrue(condition_set.matches(my_value2='def456'))
        self.assertTrue(condition_set.matches(my_value1='abc123',
                                              my_value2='xxx'))
        self.assertFalse(condition_set.matches(my_value1='xxx',
                                               my_value2='xxx'))
    def test_matches_with_any_mode_and_no_match(self):
        """Testing ConditionSet.matches with "any" mode and no match"""
        choice = EqualsTestChoice()

        condition_set = ConditionSet(ConditionSet.MODE_ANY, [
            Condition(choice, choice.get_operator('equals-test-op'), 'abc123'),
            Condition(choice, choice.get_operator('equals-test-op'), 'def123'),
        ])

        self.assertFalse(condition_set.matches(value='foo'))
    def test_matches_with_all_mode_and_match(self):
        """Testing ConditionSet.matches with "all" mode and match"""
        choice = EqualsTestChoice()

        condition_set = ConditionSet(ConditionSet.MODE_ALL, [
            Condition(choice, choice.get_operator('equals-test-op'), 'abc123'),
            Condition(choice, choice.get_operator('equals-test-op'), 'abc123'),
        ])

        self.assertTrue(condition_set.matches(value='abc123'))
    def test_matches_with_custom_value_kwargs(self):
        """Testing ConditionSet.matches with custom value keyword arguments"""
        class CustomEqualsChoice(EqualsTestChoice):
            value_kwarg = 'my_value'

        choice = CustomEqualsChoice()

        condition_set = ConditionSet(ConditionSet.MODE_ALL, [
            Condition(choice, choice.get_operator('equals-test-op'),
                      'abc123'),
        ])

        self.assertTrue(condition_set.matches(my_value='abc123'))
        self.assertFalse(condition_set.matches(value='abc123'))
    def test_serialize_without_value_field(self):
        """Testing Condition.serialize without a value_field"""
        class MyChoice(BaseConditionChoice):
            choice_id = 'my-choice'
            operators = ConditionOperators([BasicTestOperator])

        choice = MyChoice()
        condition = Condition(choice, choice.get_operator('basic-test-op'))

        self.assertEqual(
            condition.serialize(),
            {
                'choice': 'my-choice',
                'op': 'basic-test-op',
            })
    def test_serialize(self):
        """Testing Condition.serialize"""
        class MyChoice(BaseConditionChoice):
            choice_id = 'my-choice'
            operators = ConditionOperators([BasicTestOperator])
            default_value_field = ConditionValueFormField(forms.IntegerField())

        choice = MyChoice()
        condition = Condition(choice, choice.get_operator('basic-test-op'),
                              123)

        self.assertEqual(
            condition.serialize(),
            {
                'choice': 'my-choice',
                'op': 'basic-test-op',
                'value': 123,
            })
 def test_matches_with_no_match(self):
     """Testing Condition.matches with no match"""
     choice = EqualsTestChoice()
     condition = Condition(choice, choice.get_operator('equals-test-op'),
                           'abc123')
     self.assertFalse(condition.matches('def123'))