Fixed tests for python 3.4

This commit is contained in:
Xavier Ordoquy 2014-05-16 01:20:40 +02:00
parent b370fb40b6
commit a704d5a206

View File

@ -1,5 +1,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import sys
import copy import copy
from django.test import TestCase from django.test import TestCase
from rest_framework import status from rest_framework import status
@ -11,6 +12,11 @@ from rest_framework.views import APIView
factory = APIRequestFactory() factory = APIRequestFactory()
if sys.version_info[:2] >= (3, 4):
JSON_ERROR = 'JSON parse error - Expecting value:'
else:
JSON_ERROR = 'JSON parse error - No JSON object could be decoded'
class BasicView(APIView): class BasicView(APIView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -48,7 +54,7 @@ def sanitise_json_error(error_dict):
of json. of json.
""" """
ret = copy.copy(error_dict) ret = copy.copy(error_dict)
chop = len('JSON parse error - No JSON object could be decoded') chop = len(JSON_ERROR)
ret['detail'] = ret['detail'][:chop] ret['detail'] = ret['detail'][:chop]
return ret return ret
@ -61,7 +67,7 @@ class ClassBasedViewIntegrationTests(TestCase):
request = factory.post('/', 'f00bar', content_type='application/json') request = factory.post('/', 'f00bar', content_type='application/json')
response = self.view(request) response = self.view(request)
expected = { expected = {
'detail': 'JSON parse error - No JSON object could be decoded' 'detail': JSON_ERROR
} }
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(sanitise_json_error(response.data), expected) self.assertEqual(sanitise_json_error(response.data), expected)
@ -76,7 +82,7 @@ class ClassBasedViewIntegrationTests(TestCase):
request = factory.post('/', form_data) request = factory.post('/', form_data)
response = self.view(request) response = self.view(request)
expected = { expected = {
'detail': 'JSON parse error - No JSON object could be decoded' 'detail': JSON_ERROR
} }
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(sanitise_json_error(response.data), expected) self.assertEqual(sanitise_json_error(response.data), expected)
@ -90,7 +96,7 @@ class FunctionBasedViewIntegrationTests(TestCase):
request = factory.post('/', 'f00bar', content_type='application/json') request = factory.post('/', 'f00bar', content_type='application/json')
response = self.view(request) response = self.view(request)
expected = { expected = {
'detail': 'JSON parse error - No JSON object could be decoded' 'detail': JSON_ERROR
} }
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(sanitise_json_error(response.data), expected) self.assertEqual(sanitise_json_error(response.data), expected)
@ -105,7 +111,7 @@ class FunctionBasedViewIntegrationTests(TestCase):
request = factory.post('/', form_data) request = factory.post('/', form_data)
response = self.view(request) response = self.view(request)
expected = { expected = {
'detail': 'JSON parse error - No JSON object could be decoded' 'detail': JSON_ERROR
} }
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(sanitise_json_error(response.data), expected) self.assertEqual(sanitise_json_error(response.data), expected)