summaryrefslogtreecommitdiff
path: root/tests/postgres_tests
diff options
context:
space:
mode:
authorSkyiesac <jainsachi1202@gmail.com>2025-11-17 00:46:37 +0530
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-03 09:18:10 -0500
commitd338c2243fa0786225e056c5d7003f7ad67d44de (patch)
tree9e1af2794830ffc04f8936027fb7e1ce747dd9c0 /tests/postgres_tests
parentdba622ebc1f6a6700b75303a65f8a334bd46bd8e (diff)
Fixed #36280 -- Replaced exception checks with assertRaisesMessage().
Diffstat (limited to 'tests/postgres_tests')
-rw-r--r--tests/postgres_tests/test_array.py46
-rw-r--r--tests/postgres_tests/test_ranges.py48
2 files changed, 38 insertions, 56 deletions
diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py
index d4289d79df..eea9935804 100644
--- a/tests/postgres_tests/test_array.py
+++ b/tests/postgres_tests/test_array.py
@@ -1087,12 +1087,9 @@ class TestValidation(PostgreSQLSimpleTestCase):
def test_with_size(self):
field = ArrayField(models.IntegerField(), size=3)
field.clean([1, 2, 3], None)
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "List contains 4 items, it should contain no more than 3."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean([1, 2, 3, 4], None)
- self.assertEqual(
- cm.exception.messages[0],
- "List contains 4 items, it should contain no more than 3.",
- )
def test_with_size_singular(self):
field = ArrayField(models.IntegerField(), size=1)
@@ -1156,21 +1153,15 @@ class TestSimpleFormField(PostgreSQLSimpleTestCase):
def test_to_python_fail(self):
field = SimpleArrayField(forms.IntegerField())
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Item 1 in the array did not validate: Enter a whole number."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean("a,b,9")
- self.assertEqual(
- cm.exception.messages[0],
- "Item 1 in the array did not validate: Enter a whole number.",
- )
def test_validate_fail(self):
field = SimpleArrayField(forms.CharField(required=True))
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Item 3 in the array did not validate: This field is required."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean("a,b,")
- self.assertEqual(
- cm.exception.messages[0],
- "Item 3 in the array did not validate: This field is required.",
- )
def test_validate_fail_base_field_error_params(self):
field = SimpleArrayField(forms.CharField(max_length=2))
@@ -1203,12 +1194,9 @@ class TestSimpleFormField(PostgreSQLSimpleTestCase):
def test_validators_fail(self):
field = SimpleArrayField(forms.RegexField("[a-e]{2}"))
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Item 1 in the array did not validate: Enter a valid value."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean("a,bc,de")
- self.assertEqual(
- cm.exception.messages[0],
- "Item 1 in the array did not validate: Enter a valid value.",
- )
def test_delimiter(self):
field = SimpleArrayField(forms.CharField(), delimiter="|")
@@ -1227,21 +1215,15 @@ class TestSimpleFormField(PostgreSQLSimpleTestCase):
def test_max_length(self):
field = SimpleArrayField(forms.CharField(), max_length=2)
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "List contains 3 items, it should contain no more than 2."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean("a,b,c")
- self.assertEqual(
- cm.exception.messages[0],
- "List contains 3 items, it should contain no more than 2.",
- )
def test_min_length(self):
field = SimpleArrayField(forms.CharField(), min_length=4)
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "List contains 3 items, it should contain no fewer than 4."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean("a,b,c")
- self.assertEqual(
- cm.exception.messages[0],
- "List contains 3 items, it should contain no fewer than 4.",
- )
def test_min_length_singular(self):
field = SimpleArrayField(forms.IntegerField(), min_length=2)
@@ -1252,9 +1234,9 @@ class TestSimpleFormField(PostgreSQLSimpleTestCase):
def test_required(self):
field = SimpleArrayField(forms.CharField(), required=True)
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "This field is required."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean("")
- self.assertEqual(cm.exception.messages[0], "This field is required.")
def test_model_field_formfield(self):
model_field = ArrayField(models.CharField(max_length=27))
diff --git a/tests/postgres_tests/test_ranges.py b/tests/postgres_tests/test_ranges.py
index f1c104e83b..7e21d2704f 100644
--- a/tests/postgres_tests/test_ranges.py
+++ b/tests/postgres_tests/test_ranges.py
@@ -842,21 +842,21 @@ class TestFormField(PostgreSQLSimpleTestCase):
def test_integer_invalid_lower(self):
field = pg_forms.IntegerRangeField()
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Enter a whole number."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["a", "2"])
- self.assertEqual(cm.exception.messages[0], "Enter a whole number.")
def test_integer_invalid_upper(self):
field = pg_forms.IntegerRangeField()
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Enter a whole number."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["1", "b"])
- self.assertEqual(cm.exception.messages[0], "Enter a whole number.")
def test_integer_required(self):
field = pg_forms.IntegerRangeField(required=True)
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "This field is required."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["", ""])
- self.assertEqual(cm.exception.messages[0], "This field is required.")
value = field.clean([1, ""])
self.assertEqual(value, NumericRange(1, None))
@@ -884,21 +884,21 @@ class TestFormField(PostgreSQLSimpleTestCase):
def test_decimal_invalid_lower(self):
field = pg_forms.DecimalRangeField()
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Enter a number."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["a", "3.1415926"])
- self.assertEqual(cm.exception.messages[0], "Enter a number.")
def test_decimal_invalid_upper(self):
field = pg_forms.DecimalRangeField()
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Enter a number."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["1.61803399", "b"])
- self.assertEqual(cm.exception.messages[0], "Enter a number.")
def test_decimal_required(self):
field = pg_forms.DecimalRangeField(required=True)
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "This field is required."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["", ""])
- self.assertEqual(cm.exception.messages[0], "This field is required.")
value = field.clean(["1.61803399", ""])
self.assertEqual(value, NumericRange(Decimal("1.61803399"), None))
@@ -926,21 +926,21 @@ class TestFormField(PostgreSQLSimpleTestCase):
def test_date_invalid_lower(self):
field = pg_forms.DateRangeField()
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Enter a valid date."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["a", "2013-04-09"])
- self.assertEqual(cm.exception.messages[0], "Enter a valid date.")
def test_date_invalid_upper(self):
field = pg_forms.DateRangeField()
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Enter a valid date."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["2013-04-09", "b"])
- self.assertEqual(cm.exception.messages[0], "Enter a valid date.")
def test_date_required(self):
field = pg_forms.DateRangeField(required=True)
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "This field is required."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["", ""])
- self.assertEqual(cm.exception.messages[0], "This field is required.")
value = field.clean(["1976-04-16", ""])
self.assertEqual(value, DateRange(datetime.date(1976, 4, 16), None))
@@ -986,21 +986,21 @@ class TestFormField(PostgreSQLSimpleTestCase):
def test_datetime_invalid_lower(self):
field = pg_forms.DateTimeRangeField()
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Enter a valid date/time."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["45", "2013-04-09 11:45"])
- self.assertEqual(cm.exception.messages[0], "Enter a valid date/time.")
def test_datetime_invalid_upper(self):
field = pg_forms.DateTimeRangeField()
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "Enter a valid date/time."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["2013-04-09 11:45", "sweet pickles"])
- self.assertEqual(cm.exception.messages[0], "Enter a valid date/time.")
def test_datetime_required(self):
field = pg_forms.DateTimeRangeField(required=True)
- with self.assertRaises(exceptions.ValidationError) as cm:
+ msg = "This field is required."
+ with self.assertRaisesMessage(exceptions.ValidationError, msg):
field.clean(["", ""])
- self.assertEqual(cm.exception.messages[0], "This field is required.")
value = field.clean(["2013-04-09 11:45", ""])
self.assertEqual(
value, DateTimeTZRange(datetime.datetime(2013, 4, 9, 11, 45), None)