mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
more pytest style assert (#4719)
This commit is contained in:
parent
4c41f2bfea
commit
e03d88ced7
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user