Use JsonResponse when it makes sense

This commit is contained in:
David Baumgold
2013-06-27 12:21:42 -04:00
parent 090d0d4464
commit ef81556cc5
7 changed files with 59 additions and 49 deletions

View File

@@ -2,6 +2,7 @@ from django.http import HttpResponse
from util.json_request import JsonResponse
import json
import unittest
import mock
class JsonResponseTestCase(unittest.TestCase):
@@ -33,10 +34,29 @@ class JsonResponseTestCase(unittest.TestCase):
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp["content-type"], "application/json")
def test_set_status(self):
def test_set_status_kwarg(self):
obj = {"error": "resource not found"}
resp = JsonResponse(obj, status=404)
compare = json.loads(resp.content)
self.assertEqual(obj, compare)
self.assertEqual(resp.status_code, 404)
self.assertEqual(resp["content-type"], "application/json")
def test_set_status_arg(self):
obj = {"error": "resource not found"}
resp = JsonResponse(obj, 404)
compare = json.loads(resp.content)
self.assertEqual(obj, compare)
self.assertEqual(resp.status_code, 404)
self.assertEqual(resp["content-type"], "application/json")
def test_encoder(self):
obj = [1, 2, 3]
encoder = object()
with mock.patch.object(json, "dumps", return_value="[1,2,3]") as dumps:
resp = JsonResponse(obj, encoder=encoder)
self.assertEqual(resp.status_code, 200)
compare = json.loads(resp.content)
self.assertEqual(obj, compare)
kwargs = dumps.call_args[1]
self.assertIs(kwargs["cls"], encoder)