When I raise a validation error in my serializer the output is prefixed with a 'b' (which I believe is a byte type) and every way I've tried to remove it has failed.
My serializer class: class AppInputTypeSerializer(serializers.Serializer): #value = serializers.CharField(required=True, max_length = 100, min_length = 1)
def validate_value(self, data):
length = len(data['value'])
if length < 1 or length > 100:
message = "We are sorry, your reply should be between 1 and 100 characters. Please try again."
data['error'] = message
raise serializers.ValidationError(message)
return data
my view: class PresentationLayerView(generics.GenericAPIView): permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
body = request.data
cardType = body['cardType']
if cardType == "TERMS_CONDITIONS":
serializer = AppTandCTypeSerializer(body)
elif cardType == "CHOICE":
serializer = AppChoiceTypeSerializer(body)
elif cardType == "TEXT" or cardType == "INPUT":
serializer = AppTextTypeSerializer(body)
serializer.validate_value(body)
#print(response.text)
return Response({}, status=status.HTTP_200_OK,)
Test_views:
class StartAssessmentViewTests(APITestCase): # Return validation error if user input > 100 characters for an input cardType
def test_input_type(self):
#response = self.client.get(
# reverse("app-start-assessment")
#)
response = self.client.post(reverse("app-start-assessment"),
json.dumps({"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":4,
"value": "This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":8,
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT"}),
content_type='application/json')
self.assertEqual(
response.content,
{
"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":"4",
"value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":"8",
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT",
"error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."
}, response.json,
)
Output is an assert failure because of the prefix below:
b'{"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment","step":"4","value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100","optionId":"8","path":"/assessments/assessment-id/dialog/next","cardType":"INPUT","error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."}'
Instead of this:
{"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment","step":"4","value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100","optionId":"8","path":"/assessments/assessment-id/dialog/next","cardType":"INPUT","error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."}
Please help. Thanks
CodePudding user response:
Output is an assert failure because of the prefix below.
No, that is not the reason. The reason is because you are comparing a binary string with a dictionary. But even if it was a simple string, that would fail because a string is not a dictionary, even if these look the same.
response.content
is a binary string, not a dictionary since it should eventually result in a binary stream send as HTTP response.
It is better to JSON decode the object, and then check if it is equivalent with the dictionary. You can do this with response.json()
:
self.assertEqual(
{
"message":"Please type in the symptom that is troubling you, only one symptom at a time. Reply back to go to the previous question or abort to end the assessment",
"step":"4",
"value":"This is some rubbish text to see what happens when a user submits an input with a length that is greater than 100",
"optionId":"8",
"path":"/assessments/assessment-id/dialog/next",
"cardType":"INPUT",
"error":"We are sorry, your reply should be between 1 and 100 characters. Please try again."
},
response.json()
)