2016-04-09 17:17:34 +00:00
|
|
|
import re
|
|
|
|
|
2020-02-12 13:48:49 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.forms import RegexField
|
2016-04-09 17:17:34 +00:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class RegexFieldTest(SimpleTestCase):
|
|
|
|
def test_regexfield_1(self):
|
|
|
|
f = RegexField("^[0-9][A-F][0-9]$")
|
|
|
|
self.assertEqual("2A2", f.clean("2A2"))
|
|
|
|
self.assertEqual("3F3", f.clean("3F3"))
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean("3G3")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean(" 2A2")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean("2A2 ")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean("")
|
|
|
|
|
|
|
|
def test_regexfield_2(self):
|
|
|
|
f = RegexField("^[0-9][A-F][0-9]$", required=False)
|
|
|
|
self.assertEqual("2A2", f.clean("2A2"))
|
|
|
|
self.assertEqual("3F3", f.clean("3F3"))
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean("3G3")
|
|
|
|
self.assertEqual("", f.clean(""))
|
|
|
|
|
|
|
|
def test_regexfield_3(self):
|
|
|
|
f = RegexField(re.compile("^[0-9][A-F][0-9]$"))
|
|
|
|
self.assertEqual("2A2", f.clean("2A2"))
|
|
|
|
self.assertEqual("3F3", f.clean("3F3"))
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean("3G3")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean(" 2A2")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean("2A2 ")
|
|
|
|
|
|
|
|
def test_regexfield_4(self):
|
|
|
|
f = RegexField("^[0-9]+$", min_length=5, max_length=10)
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError, "'Ensure this value has at least 5 characters (it has 3).'"
|
|
|
|
):
|
|
|
|
f.clean("123")
|
2017-01-20 02:10:33 +00:00
|
|
|
with self.assertRaisesMessage(
|
2016-12-07 22:42:31 +00:00
|
|
|
ValidationError,
|
2017-01-20 02:10:33 +00:00
|
|
|
"'Ensure this value has at least 5 characters (it has 3).', "
|
|
|
|
"'Enter a valid value.'",
|
2016-12-07 22:42:31 +00:00
|
|
|
):
|
|
|
|
f.clean("abc")
|
2016-04-09 17:17:34 +00:00
|
|
|
self.assertEqual("12345", f.clean("12345"))
|
|
|
|
self.assertEqual("1234567890", f.clean("1234567890"))
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError,
|
|
|
|
"'Ensure this value has at most 10 characters (it has 11).'",
|
|
|
|
):
|
|
|
|
f.clean("12345678901")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean("12345a")
|
|
|
|
|
|
|
|
def test_regexfield_unicode_characters(self):
|
2016-09-16 16:15:00 +00:00
|
|
|
f = RegexField(r"^\w+$")
|
2016-04-09 17:17:34 +00:00
|
|
|
self.assertEqual("éèøçÎÎ你好", f.clean("éèøçÎÎ你好"))
|
|
|
|
|
|
|
|
def test_change_regex_after_init(self):
|
|
|
|
f = RegexField("^[a-z]+$")
|
|
|
|
f.regex = "^[0-9]+$"
|
|
|
|
self.assertEqual("1234", f.clean("1234"))
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
|
|
|
|
f.clean("abcd")
|
2020-04-10 07:25:17 +00:00
|
|
|
|
|
|
|
def test_get_regex(self):
|
|
|
|
f = RegexField("^[a-z]+$")
|
|
|
|
self.assertEqual(f.regex, re.compile("^[a-z]+$"))
|
|
|
|
|
|
|
|
def test_regexfield_strip(self):
|
|
|
|
f = RegexField("^[a-z]+$", strip=True)
|
|
|
|
self.assertEqual(f.clean(" a"), "a")
|
|
|
|
self.assertEqual(f.clean("a "), "a")
|
2020-09-25 10:10:45 +00:00
|
|
|
|
|
|
|
def test_empty_value(self):
|
|
|
|
f = RegexField("", required=False)
|
|
|
|
self.assertEqual(f.clean(""), "")
|
|
|
|
self.assertEqual(f.clean(None), "")
|
|
|
|
f = RegexField("", empty_value=None, required=False)
|
|
|
|
self.assertIsNone(f.clean(""))
|
|
|
|
self.assertIsNone(f.clean(None))
|