Example #1
0
class ConditionValueRegexFieldTests(TestCase):
    """Unit tests for djblets.conditions.values.ConditionValueRegexField."""
    def setUp(self):
        super(ConditionValueRegexFieldTests, self).setUp()

        self.value_field = ConditionValueRegexField()

    def test_deserialize_value(self):
        """Testing ConditionValueRegexField.deserialize_value"""
        regex_obj = self.value_field.deserialize_value('ab[^c]d+e?')

        # Weird, right? re.RegexObject, the documented result of a
        # re.compile(), does not exist. Instead, it's a _sre.SRE_Pattern,
        # but that's basically an internal detail we don't want to rely upon.
        # Internally, the re module actually does this type() mess to get
        # the type to use, so that's what we're doing!
        regex_type = type(re.compile(''))

        self.assertTrue(isinstance(regex_obj, regex_type))
        self.assertEqual(regex_obj.pattern, re.compile('ab[^c]d+e?').pattern)

    def test_deserialize_value_with_bad_pattern(self):
        """Testing ConditionValueRegexField.deserialize_value with bad
        pattern
        """
        with self.assertRaises(InvalidConditionValueError):
            self.value_field.deserialize_value('*')

    def test_serialize_value(self):
        """Testing ConditionValueRegexField.serialize_value"""
        regex_obj = re.compile('ab[^c]d+e?')

        self.assertEqual(self.value_field.serialize_value(regex_obj),
                         'ab[^c]d+e?')
Example #2
0
class DoesNotMatchRegexOperator(BaseConditionOperator):
    """An operator that checks if a value does not match against a regex.

    It's equivalent to::

        if not condition_value.match(match_value):
            ...
    """

    operator_id = 'does-not-match-regex'
    name = _('Does not match regex')
    value_field = ConditionValueRegexField()

    def matches(self, match_value, condition_value, **kwargs):
        """Return whether the lookup value doesn't match the condition's regex.

        Args:
            match_value (unicode):
                The caller's value to check.

            condition_value (re.RegexObject):
                The regex value that the lookup value must not match.

            **kwargs (dict):
                Unused extra keyword arguments.

        Returns:
            bool:
            ``True`` if the lookup value doesn't match the regex in the
            condition.
        """
        return condition_value.match(match_value) is None
Example #3
0
class ConditionValueRegexFieldTests(TestCase):
    """Unit tests for djblets.conditions.values.ConditionValueRegexField."""

    def setUp(self):
        super(ConditionValueRegexFieldTests, self).setUp()

        self.value_field = ConditionValueRegexField()

    def test_deserialize_value(self):
        """Testing ConditionValueRegexField.deserialize_value"""
        regex_obj = self.value_field.deserialize_value('ab[^c]d+e?')

        # Weird, right? re.RegexObject, the documented result of a
        # re.compile(), does not exist. Instead, it's a _sre.SRE_Pattern,
        # but that's basically an internal detail we don't want to rely upon.
        # Internally, the re module actually does this type() mess to get
        # the type to use, so that's what we're doing!
        regex_type = type(re.compile(''))

        self.assertTrue(isinstance(regex_obj, regex_type))
        self.assertEqual(regex_obj.pattern, re.compile('ab[^c]d+e?').pattern)

    def test_deserialize_value_with_bad_pattern(self):
        """Testing ConditionValueRegexField.deserialize_value with bad
        pattern
        """
        with self.assertRaises(InvalidConditionValueError):
            self.value_field.deserialize_value('*')

    def test_serialize_value(self):
        """Testing ConditionValueRegexField.serialize_value"""
        regex_obj = re.compile('ab[^c]d+e?')

        self.assertEqual(
            self.value_field.serialize_value(regex_obj),
            'ab[^c]d+e?')
Example #4
0
    def setUp(self):
        super(ConditionValueRegexFieldTests, self).setUp()

        self.value_field = ConditionValueRegexField()
Example #5
0
    def setUp(self):
        super(ConditionValueRegexFieldTests, self).setUp()

        self.value_field = ConditionValueRegexField()