more pytest style assert (#4719)

This commit is contained in:
Asif Saifuddin Auvi 2016-11-30 15:48:33 +06:00 committed by Tom Christie
parent 4c41f2bfea
commit e03d88ced7
2 changed files with 29 additions and 25 deletions

View File

@ -60,11 +60,11 @@ class TestNestedValidationError(TestCase):
} }
}) })
self.assertEqual(serializers.as_serializer_error(e), { assert serializers.as_serializer_error(e) == {
'nested': { 'nested': {
'field': ['error'], 'field': ['error'],
} }
}) }
class TestPreSaveValidationExclusionsSerializer(TestCase): class TestPreSaveValidationExclusionsSerializer(TestCase):
@ -75,20 +75,20 @@ class TestPreSaveValidationExclusionsSerializer(TestCase):
# We've set `required=False` on the serializer, but the model # We've set `required=False` on the serializer, but the model
# does not have `blank=True`, so this serializer should not validate. # does not have `blank=True`, so this serializer should not validate.
serializer = ShouldValidateModelSerializer(data={'renamed': ''}) serializer = ShouldValidateModelSerializer(data={'renamed': ''})
self.assertEqual(serializer.is_valid(), False) assert serializer.is_valid() is False
self.assertIn('renamed', serializer.errors) assert 'renamed' in serializer.errors
self.assertNotIn('should_validate_field', serializer.errors) assert 'should_validate_field' not in serializer.errors
class TestCustomValidationMethods(TestCase): class TestCustomValidationMethods(TestCase):
def test_custom_validation_method_is_executed(self): def test_custom_validation_method_is_executed(self):
serializer = ShouldValidateModelSerializer(data={'renamed': 'fo'}) serializer = ShouldValidateModelSerializer(data={'renamed': 'fo'})
self.assertFalse(serializer.is_valid()) assert not serializer.is_valid()
self.assertIn('renamed', serializer.errors) assert 'renamed' in serializer.errors
def test_custom_validation_method_passing(self): def test_custom_validation_method_passing(self):
serializer = ShouldValidateModelSerializer(data={'renamed': 'foo'}) serializer = ShouldValidateModelSerializer(data={'renamed': 'foo'})
self.assertTrue(serializer.is_valid()) assert serializer.is_valid()
class ValidationSerializer(serializers.Serializer): class ValidationSerializer(serializers.Serializer):
@ -108,12 +108,12 @@ class TestAvoidValidation(TestCase):
""" """
def test_serializer_errors_has_only_invalid_data_error(self): def test_serializer_errors_has_only_invalid_data_error(self):
serializer = ValidationSerializer(data='invalid data') serializer = ValidationSerializer(data='invalid data')
self.assertFalse(serializer.is_valid()) assert not serializer.is_valid()
self.assertDictEqual(serializer.errors, { assert serializer.errors == {
'non_field_errors': [ 'non_field_errors': [
'Invalid data. Expected a dictionary, but got %s.' % type('').__name__ 'Invalid data. Expected a dictionary, but got %s.' % type('').__name__
] ]
}) }
# regression tests for issue: 1493 # regression tests for issue: 1493
@ -137,27 +137,31 @@ class TestMaxValueValidatorValidation(TestCase):
def test_max_value_validation_serializer_success(self): def test_max_value_validation_serializer_success(self):
serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 99}) serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 99})
self.assertTrue(serializer.is_valid()) assert serializer.is_valid()
def test_max_value_validation_serializer_fails(self): def test_max_value_validation_serializer_fails(self):
serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 101}) serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 101})
self.assertFalse(serializer.is_valid()) assert not serializer.is_valid()
self.assertDictEqual({'number_value': ['Ensure this value is less than or equal to 100.']}, serializer.errors) assert serializer.errors == {
'number_value': [
'Ensure this value is less than or equal to 100.'
]
}
def test_max_value_validation_success(self): def test_max_value_validation_success(self):
obj = ValidationMaxValueValidatorModel.objects.create(number_value=100) obj = ValidationMaxValueValidatorModel.objects.create(number_value=100)
request = factory.patch('/{0}'.format(obj.pk), {'number_value': 98}, format='json') request = factory.patch('/{0}'.format(obj.pk), {'number_value': 98}, format='json')
view = UpdateMaxValueValidationModel().as_view() view = UpdateMaxValueValidationModel().as_view()
response = view(request, pk=obj.pk).render() 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): def test_max_value_validation_fail(self):
obj = ValidationMaxValueValidatorModel.objects.create(number_value=100) obj = ValidationMaxValueValidatorModel.objects.create(number_value=100)
request = factory.patch('/{0}'.format(obj.pk), {'number_value': 101}, format='json') request = factory.patch('/{0}'.format(obj.pk), {'number_value': 101}, format='json')
view = UpdateMaxValueValidationModel().as_view() view = UpdateMaxValueValidationModel().as_view()
response = view(request, pk=obj.pk).render() response = view(request, pk=obj.pk).render()
self.assertEqual(response.content, b'{"number_value":["Ensure this value is less than or equal to 100."]}') assert 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.status_code == status.HTTP_400_BAD_REQUEST
# regression tests for issue: 1533 # regression tests for issue: 1533

View File

@ -54,16 +54,16 @@ class TestValidationErrorWithFullDetails(TestCase):
request = factory.get('/', content_type='application/json') request = factory.get('/', content_type='application/json')
response = view(request) response = view(request)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) assert response.status_code == status.HTTP_400_BAD_REQUEST
self.assertEqual(response.data, self.expected_response_data) assert response.data == self.expected_response_data
def test_function_based_view_exception_handler(self): def test_function_based_view_exception_handler(self):
view = error_view view = error_view
request = factory.get('/', content_type='application/json') request = factory.get('/', content_type='application/json')
response = view(request) response = view(request)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) assert response.status_code == status.HTTP_400_BAD_REQUEST
self.assertEqual(response.data, self.expected_response_data) assert response.data == self.expected_response_data
class TestValidationErrorWithCodes(TestCase): class TestValidationErrorWithCodes(TestCase):
@ -89,13 +89,13 @@ class TestValidationErrorWithCodes(TestCase):
request = factory.get('/', content_type='application/json') request = factory.get('/', content_type='application/json')
response = view(request) response = view(request)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) assert response.status_code == status.HTTP_400_BAD_REQUEST
self.assertEqual(response.data, self.expected_response_data) assert response.data == self.expected_response_data
def test_function_based_view_exception_handler(self): def test_function_based_view_exception_handler(self):
view = error_view view = error_view
request = factory.get('/', content_type='application/json') request = factory.get('/', content_type='application/json')
response = view(request) response = view(request)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) assert response.status_code == status.HTTP_400_BAD_REQUEST
self.assertEqual(response.data, self.expected_response_data) assert response.data == self.expected_response_data