diff options
Diffstat (limited to 'tests/postgres_tests')
| -rw-r--r-- | tests/postgres_tests/test_array.py | 46 | ||||
| -rw-r--r-- | tests/postgres_tests/test_ranges.py | 48 |
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) |
