File: test_response_deprecations.py

package info (click to toggle)
python-django 1.8.18-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 41,628 kB
  • sloc: python: 189,488; xml: 695; makefile: 194; sh: 169; sql: 11
file content (42 lines) | stat: -rw-r--r-- 1,557 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
import warnings

from django.http import HttpRequest
from django.template import Template
from django.template.response import TemplateResponse
from django.test import SimpleTestCase


class MyTemplateResponse(TemplateResponse):
    def resolve_template(self, template_name):
        return Template(template_name)


class DeprecationTests(SimpleTestCase):

    def test_template_response(self):
        msg = (
            "TemplateResponse's template argument cannot be a "
            "django.template.Template anymore. It may be a backend-specific "
            "template like those created by get_template()."
        )
        with warnings.catch_warnings(record=True) as warns:
            warnings.simplefilter('always')
            response = TemplateResponse(HttpRequest(), Template('foo'))
        response.render()
        self.assertEqual(response.content, b'foo')
        self.assertEqual(len(warns), 1)
        self.assertEqual(str(warns[0].message), msg)

    def test_custom_template_response(self):
        response = MyTemplateResponse(HttpRequest(), 'baz')
        msg = (
            "MyTemplateResponse.resolve_template() must return a "
            "backend-specific template like those created by get_template(), "
            "not a Template."
        )
        with warnings.catch_warnings(record=True) as warns:
            warnings.simplefilter('always')
            response.render()
        self.assertEqual(response.content, b'baz')
        self.assertEqual(len(warns), 1)
        self.assertEqual(str(warns[0].message), msg)