Example #1
0
    def test_init_assertions(self):
        with self.assertRaises(AssertionError):
            RegexField(regex=[])

        with self.assertRaises(AssertionError):
            RegexField(regex=r'', flags=None)

        with self.assertRaises(AssertionError):
            RegexField(regex=r'', flags=[])
Example #2
0
    def test_flags(self):
        f = RegexField(regex=r'^test.*$')
        self.assertEqual("test_string", f.clean("test_string"))
        with self.assertRaises(ValidationError):
            f.clean("tESt_string")

        f = RegexField(regex=r'^test.*$', flags=re.I)
        self.assertEqual("test_string", f.clean("test_string"))
        self.assertEqual("tESt_string", f.clean("tESt_string"))
Example #3
0
    def test_required(self):
        f = RegexField(required=False, regex=r'.*')
        self.assertIsNone(f.clean(None))

        f = RegexField(regex=r'.*')
        with self.assertRaises(ValidationError):
            f.clean(None)
Example #4
0
    def test_match(self):
        test_data = "test_string"
        f = RegexField(regex=r'^test(.*)$')
        f.clean(test_data)
        self.assertTrue(isinstance(f.match, self.SRE_MATCH_TYPE))
        self.assertEqual(f.match.group(0), test_data)
        self.assertEqual(f.match.group(1), "_string")

        f = RegexField(regex=r'^test(.*)$', required=False)
        f.clean(None)
        self.assertIsNone(f.match)
Example #5
0
 def test_empty_value_validators(self):
     # By default django skips run_validators methods, if value is in empty_values
     # It's not correct for REST, as empty value is not equal to None value now
     f = RegexField(validators=[TestErrorValidator(0)])
     with self.assertRaises(ValidationError):
         f.clean('')
Example #6
0
 def test_no_regex(self):
     # Acts as a simple CharField
     test_data = "test_string"
     f = RegexField()
     self.assertEqual(test_data, f.clean(test_data))
Example #7
0
 def test_regex_invalid(self):
     test_data = "test_string"
     f = RegexField(regex=r'^test1.*$')
     with self.assertRaises(ValidationError):
         f.clean(test_data)
Example #8
0
 def test_regex_compiled(self):
     test_data = "test_string"
     f = RegexField(regex=re.compile('^test.*$'))
     self.assertEqual(test_data, f.clean(test_data))
Example #9
0
 def test_regex_valid(self):
     test_data = "test_string"
     f = RegexField(regex=r'^test.*$')
     self.assertEqual(test_data, f.clean(test_data))
Example #10
0
 def test_initial(self):
     test_data = "test_string"
     f = RegexField(required=False, initial=test_data, regex=r'.*')
     self.assertEqual(test_data, f.clean(None))