From e03d88ced7f2b22f96f50c2a885264147b242b4a Mon Sep 17 00:00:00 2001 From: Asif Saifuddin Auvi Date: Wed, 30 Nov 2016 15:48:33 +0600 Subject: [PATCH] more pytest style assert (#4719) --- tests/test_validation.py | 38 +++++++++++++++++++--------------- tests/test_validation_error.py | 16 +++++++------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/tests/test_validation.py b/tests/test_validation.py index bc950dd22..8ff4aaf38 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -60,11 +60,11 @@ class TestNestedValidationError(TestCase): } }) - self.assertEqual(serializers.as_serializer_error(e), { + assert serializers.as_serializer_error(e) == { 'nested': { 'field': ['error'], } - }) + } class TestPreSaveValidationExclusionsSerializer(TestCase): @@ -75,20 +75,20 @@ class TestPreSaveValidationExclusionsSerializer(TestCase): # We've set `required=False` on the serializer, but the model # does not have `blank=True`, so this serializer should not validate. serializer = ShouldValidateModelSerializer(data={'renamed': ''}) - self.assertEqual(serializer.is_valid(), False) - self.assertIn('renamed', serializer.errors) - self.assertNotIn('should_validate_field', serializer.errors) + assert serializer.is_valid() is False + assert 'renamed' in serializer.errors + assert 'should_validate_field' not in serializer.errors class TestCustomValidationMethods(TestCase): def test_custom_validation_method_is_executed(self): serializer = ShouldValidateModelSerializer(data={'renamed': 'fo'}) - self.assertFalse(serializer.is_valid()) - self.assertIn('renamed', serializer.errors) + assert not serializer.is_valid() + assert 'renamed' in serializer.errors def test_custom_validation_method_passing(self): serializer = ShouldValidateModelSerializer(data={'renamed': 'foo'}) - self.assertTrue(serializer.is_valid()) + assert serializer.is_valid() class ValidationSerializer(serializers.Serializer): @@ -108,12 +108,12 @@ class TestAvoidValidation(TestCase): """ def test_serializer_errors_has_only_invalid_data_error(self): serializer = ValidationSerializer(data='invalid data') - self.assertFalse(serializer.is_valid()) - self.assertDictEqual(serializer.errors, { + assert not serializer.is_valid() + assert serializer.errors == { 'non_field_errors': [ 'Invalid data. Expected a dictionary, but got %s.' % type('').__name__ ] - }) + } # regression tests for issue: 1493 @@ -137,27 +137,31 @@ class TestMaxValueValidatorValidation(TestCase): def test_max_value_validation_serializer_success(self): serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 99}) - self.assertTrue(serializer.is_valid()) + assert serializer.is_valid() def test_max_value_validation_serializer_fails(self): serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 101}) - self.assertFalse(serializer.is_valid()) - self.assertDictEqual({'number_value': ['Ensure this value is less than or equal to 100.']}, serializer.errors) + assert not serializer.is_valid() + assert serializer.errors == { + 'number_value': [ + 'Ensure this value is less than or equal to 100.' + ] + } def test_max_value_validation_success(self): obj = ValidationMaxValueValidatorModel.objects.create(number_value=100) request = factory.patch('/{0}'.format(obj.pk), {'number_value': 98}, format='json') view = UpdateMaxValueValidationModel().as_view() response = view(request, pk=obj.pk).render() - self.assertEqual(response.status_code, status.HTTP_200_OK) + assert response.status_code == status.HTTP_200_OK def test_max_value_validation_fail(self): obj = ValidationMaxValueValidatorModel.objects.create(number_value=100) request = factory.patch('/{0}'.format(obj.pk), {'number_value': 101}, format='json') view = UpdateMaxValueValidationModel().as_view() response = view(request, pk=obj.pk).render() - self.assertEqual(response.content, b'{"number_value":["Ensure this value is less than or equal to 100."]}') - self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + assert response.content == b'{"number_value":["Ensure this value is less than or equal to 100."]}' + assert response.status_code == status.HTTP_400_BAD_REQUEST # regression tests for issue: 1533 diff --git a/tests/test_validation_error.py b/tests/test_validation_error.py index 8e371a349..562fe37e6 100644 --- a/tests/test_validation_error.py +++ b/tests/test_validation_error.py @@ -54,16 +54,16 @@ class TestValidationErrorWithFullDetails(TestCase): request = factory.get('/', content_type='application/json') response = view(request) - self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - self.assertEqual(response.data, self.expected_response_data) + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert response.data == self.expected_response_data def test_function_based_view_exception_handler(self): view = error_view request = factory.get('/', content_type='application/json') response = view(request) - self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - self.assertEqual(response.data, self.expected_response_data) + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert response.data == self.expected_response_data class TestValidationErrorWithCodes(TestCase): @@ -89,13 +89,13 @@ class TestValidationErrorWithCodes(TestCase): request = factory.get('/', content_type='application/json') response = view(request) - self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - self.assertEqual(response.data, self.expected_response_data) + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert response.data == self.expected_response_data def test_function_based_view_exception_handler(self): view = error_view request = factory.get('/', content_type='application/json') response = view(request) - self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - self.assertEqual(response.data, self.expected_response_data) + assert response.status_code == status.HTTP_400_BAD_REQUEST + assert response.data == self.expected_response_data