Merge lp:~jml/testtools/json_content into lp:~testtools-committers/testtools/trunk

Proposed by Jonathan Lange
Status: Merged
Merged at revision: 265
Proposed branch: lp:~jml/testtools/json_content
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: 120 lines (+30/-5)
4 files modified
testtools/content.py (+7/-2)
testtools/content_type.py (+3/-1)
testtools/tests/test_content.py (+8/-0)
testtools/tests/test_content_type.py (+12/-2)
To merge this branch: bzr merge lp:~jml/testtools/json_content
Reviewer Review Type Date Requested Status
Jonathan Lange Needs Fixing
Martin Packman Approve
Review via email: mp+114905@code.launchpad.net

Commit message

Add a JSON content helper.

Description of the change

Add a JSON content helper.

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

Code looks good.

Why specify charset=utf-8? The usual form omits that, json is utf-8 only.

Note for self, testtools is 2.6 or later from 0.9.15, json is new in 2.6 stdlib.

review: Approve
Revision history for this message
Jonathan Lange (jml) wrote :

Thanks for the prompt review Martin!

I never want to release a version of testtools that drops support for a version of Python without announcing it first in the previous release. As such, I'll change this patch so that it's only a conditional import.

Will drop the charset=utf-8. I just cargo-culted that.

Thanks again,
jml

review: Needs Fixing
Revision history for this message
Robert Collins (lifeless) wrote :

FWIW NEWS etc already declares we're dropping compat. As of the last
release. I think you're covered.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'testtools/content.py'
2--- testtools/content.py 2012-02-04 16:44:10 +0000
3+++ testtools/content.py 2012-07-13 16:23:28 +0000
4@@ -12,13 +12,14 @@
5 ]
6
7 import codecs
8+import json
9 import os
10 import sys
11 import traceback
12
13 from testtools import try_import
14 from testtools.compat import _b, _format_exc_info, str_is_unicode, _u
15-from testtools.content_type import ContentType, UTF8_TEXT
16+from testtools.content_type import ContentType, JSON, UTF8_TEXT
17
18
19 functools = try_import('functools')
20@@ -189,6 +190,11 @@
21 return length
22
23
24+def json_content(data):
25+ """Create a JSON `Content` object from JSON-encodeable data."""
26+ return Content(JSON, lambda: [json.dumps(data)])
27+
28+
29 def text_content(text):
30 """Create a `Content` object from some text.
31
32@@ -197,7 +203,6 @@
33 return Content(UTF8_TEXT, lambda: [text.encode('utf8')])
34
35
36-
37 def maybe_wrap(wrapper, func):
38 """Merge metadata for func into wrapper if functools is present."""
39 if functools is not None:
40
41=== modified file 'testtools/content_type.py'
42--- testtools/content_type.py 2011-06-30 17:01:10 +0000
43+++ testtools/content_type.py 2012-07-13 16:23:28 +0000
44@@ -1,4 +1,4 @@
45-# Copyright (c) 2009-2011 testtools developers. See LICENSE for details.
46+# Copyright (c) 2009-2012 testtools developers. See LICENSE for details.
47
48 """ContentType - a MIME Content Type."""
49
50@@ -36,4 +36,6 @@
51 return "%s/%s%s" % (self.type, self.subtype, params)
52
53
54+JSON = ContentType('application', 'json', {'charset': 'utf8'})
55+
56 UTF8_TEXT = ContentType('text', 'plain', {'charset': 'utf8'})
57
58=== modified file 'testtools/tests/test_content.py'
59--- testtools/tests/test_content.py 2012-02-04 16:44:10 +0000
60+++ testtools/tests/test_content.py 2012-07-13 16:23:28 +0000
61@@ -1,5 +1,6 @@
62 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
63
64+import json
65 import os
66 import tempfile
67 import unittest
68@@ -15,6 +16,8 @@
69 Content,
70 content_from_file,
71 content_from_stream,
72+ JSON,
73+ json_content,
74 TracebackContent,
75 text_content,
76 )
77@@ -150,6 +153,11 @@
78 expected = Content(UTF8_TEXT, lambda: [data.encode('utf8')])
79 self.assertEqual(expected, text_content(data))
80
81+ def test_json_content(self):
82+ data = {'foo': 'bar'}
83+ expected = Content(JSON, lambda: [json.dumps(data)])
84+ self.assertEqual(expected, json_content(data))
85+
86
87 class TestTracebackContent(TestCase):
88
89
90=== modified file 'testtools/tests/test_content_type.py'
91--- testtools/tests/test_content_type.py 2011-06-30 17:01:10 +0000
92+++ testtools/tests/test_content_type.py 2012-07-13 16:23:28 +0000
93@@ -1,8 +1,12 @@
94-# Copyright (c) 2008 testtools developers. See LICENSE for details.
95+# Copyright (c) 2008, 2012 testtools developers. See LICENSE for details.
96
97 from testtools import TestCase
98 from testtools.matchers import Equals, MatchesException, Raises
99-from testtools.content_type import ContentType, UTF8_TEXT
100+from testtools.content_type import (
101+ ContentType,
102+ JSON,
103+ UTF8_TEXT,
104+ )
105
106
107 class TestContentType(TestCase):
108@@ -50,6 +54,12 @@
109 self.assertThat(UTF8_TEXT.subtype, Equals('plain'))
110 self.assertThat(UTF8_TEXT.parameters, Equals({'charset': 'utf8'}))
111
112+ def test_json_content(self):
113+ # The JSON content type represents UTF-8 encoded application/json.
114+ self.assertThat(JSON.type, Equals('application'))
115+ self.assertThat(JSON.subtype, Equals('json'))
116+ self.assertThat(JSON.parameters, Equals({'charset': 'utf8'}))
117+
118
119 def test_suite():
120 from unittest import TestLoader

Subscribers

People subscribed via source and target branches