test_json_request.py 4.53 KB
Newer Older
1 2 3 4
"""
Test for JsonResponse and JsonResponseBadRequest util classes.
"""

David Baumgold committed
5 6
import json
import unittest
7

8
import mock
9 10 11
from django.http import HttpResponse, HttpResponseBadRequest

from util.json_request import JsonResponse, JsonResponseBadRequest
David Baumgold committed
12 13 14


class JsonResponseTestCase(unittest.TestCase):
15 16 17
    """
    A set of tests to make sure that JsonResponse Class works correctly.
    """
David Baumgold committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    def test_empty(self):
        resp = JsonResponse()
        self.assertIsInstance(resp, HttpResponse)
        self.assertEqual(resp.content, "")
        self.assertEqual(resp.status_code, 204)
        self.assertEqual(resp["content-type"], "application/json")

    def test_empty_string(self):
        resp = JsonResponse("")
        self.assertIsInstance(resp, HttpResponse)
        self.assertEqual(resp.content, "")
        self.assertEqual(resp.status_code, 204)
        self.assertEqual(resp["content-type"], "application/json")

    def test_string(self):
        resp = JsonResponse("foo")
        self.assertEqual(resp.content, '"foo"')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp["content-type"], "application/json")

    def test_dict(self):
        obj = {"foo": "bar"}
        resp = JsonResponse(obj)
        compare = json.loads(resp.content)
        self.assertEqual(obj, compare)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp["content-type"], "application/json")

46
    def test_set_status_kwarg(self):
David Baumgold committed
47 48 49 50 51 52
        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")
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    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)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127


class JsonResponseBadRequestTestCase(unittest.TestCase):
    """
    A set of tests to make sure that the JsonResponseBadRequest wrapper class
    works as intended.
    """

    def test_empty(self):
        resp = JsonResponseBadRequest()
        self.assertIsInstance(resp, HttpResponseBadRequest)
        self.assertEqual(resp.content, "")
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp["content-type"], "application/json")

    def test_empty_string(self):
        resp = JsonResponseBadRequest("")
        self.assertIsInstance(resp, HttpResponse)
        self.assertEqual(resp.content, "")
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp["content-type"], "application/json")

    def test_dict(self):
        obj = {"foo": "bar"}
        resp = JsonResponseBadRequest(obj)
        compare = json.loads(resp.content)
        self.assertEqual(obj, compare)
        self.assertEqual(resp.status_code, 400)
        self.assertEqual(resp["content-type"], "application/json")

    def test_set_status_kwarg(self):
        obj = {"error": "resource not found"}
        resp = JsonResponseBadRequest(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 = JsonResponseBadRequest(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 = JsonResponseBadRequest(obj, encoder=encoder)
        self.assertEqual(resp.status_code, 400)
        compare = json.loads(resp.content)
        self.assertEqual(obj, compare)
        kwargs = dumps.call_args[1]
        self.assertIs(kwargs["cls"], encoder)