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=[])
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"))
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)
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)
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('')
def test_no_regex(self): # Acts as a simple CharField test_data = "test_string" f = RegexField() self.assertEqual(test_data, f.clean(test_data))
def test_regex_invalid(self): test_data = "test_string" f = RegexField(regex=r'^test1.*$') with self.assertRaises(ValidationError): f.clean(test_data)
def test_regex_compiled(self): test_data = "test_string" f = RegexField(regex=re.compile('^test.*$')) self.assertEqual(test_data, f.clean(test_data))
def test_regex_valid(self): test_data = "test_string" f = RegexField(regex=r'^test.*$') self.assertEqual(test_data, f.clean(test_data))
def test_initial(self): test_data = "test_string" f = RegexField(required=False, initial=test_data, regex=r'.*') self.assertEqual(test_data, f.clean(None))