← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~jml/testtools/json_content into lp:testtools

 

Jonathan Lange has proposed merging lp:~jml/testtools/json_content into lp:testtools.

Requested reviews:
  testtools committers (testtools-committers)
Related bugs:
  Bug #1024284 in testtools: "JSON content convenience helper thing"
  https://bugs.launchpad.net/testtools/+bug/1024284

For more details, see:
https://code.launchpad.net/~jml/testtools/json_content/+merge/114905

Add a JSON content helper.
-- 
https://code.launchpad.net/~jml/testtools/json_content/+merge/114905
Your team testtools developers is subscribed to branch lp:testtools.
=== modified file 'testtools/content.py'
--- testtools/content.py	2012-02-04 16:44:10 +0000
+++ testtools/content.py	2012-07-13 16:23:28 +0000
@@ -12,13 +12,14 @@
     ]
 
 import codecs
+import json
 import os
 import sys
 import traceback
 
 from testtools import try_import
 from testtools.compat import _b, _format_exc_info, str_is_unicode, _u
-from testtools.content_type import ContentType, UTF8_TEXT
+from testtools.content_type import ContentType, JSON, UTF8_TEXT
 
 
 functools = try_import('functools')
@@ -189,6 +190,11 @@
         return length
 
 
+def json_content(data):
+    """Create a JSON `Content` object from JSON-encodeable data."""
+    return Content(JSON, lambda: [json.dumps(data)])
+
+
 def text_content(text):
     """Create a `Content` object from some text.
 
@@ -197,7 +203,6 @@
     return Content(UTF8_TEXT, lambda: [text.encode('utf8')])
 
 
-
 def maybe_wrap(wrapper, func):
     """Merge metadata for func into wrapper if functools is present."""
     if functools is not None:

=== modified file 'testtools/content_type.py'
--- testtools/content_type.py	2011-06-30 17:01:10 +0000
+++ testtools/content_type.py	2012-07-13 16:23:28 +0000
@@ -1,4 +1,4 @@
-# Copyright (c) 2009-2011 testtools developers. See LICENSE for details.
+# Copyright (c) 2009-2012 testtools developers. See LICENSE for details.
 
 """ContentType - a MIME Content Type."""
 
@@ -36,4 +36,6 @@
         return "%s/%s%s" % (self.type, self.subtype, params)
 
 
+JSON = ContentType('application', 'json', {'charset': 'utf8'})
+
 UTF8_TEXT = ContentType('text', 'plain', {'charset': 'utf8'})

=== modified file 'testtools/tests/test_content.py'
--- testtools/tests/test_content.py	2012-02-04 16:44:10 +0000
+++ testtools/tests/test_content.py	2012-07-13 16:23:28 +0000
@@ -1,5 +1,6 @@
 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
 
+import json
 import os
 import tempfile
 import unittest
@@ -15,6 +16,8 @@
     Content,
     content_from_file,
     content_from_stream,
+    JSON,
+    json_content,
     TracebackContent,
     text_content,
     )
@@ -150,6 +153,11 @@
         expected = Content(UTF8_TEXT, lambda: [data.encode('utf8')])
         self.assertEqual(expected, text_content(data))
 
+    def test_json_content(self):
+        data = {'foo': 'bar'}
+        expected = Content(JSON, lambda: [json.dumps(data)])
+        self.assertEqual(expected, json_content(data))
+
 
 class TestTracebackContent(TestCase):
 

=== modified file 'testtools/tests/test_content_type.py'
--- testtools/tests/test_content_type.py	2011-06-30 17:01:10 +0000
+++ testtools/tests/test_content_type.py	2012-07-13 16:23:28 +0000
@@ -1,8 +1,12 @@
-# Copyright (c) 2008 testtools developers. See LICENSE for details.
+# Copyright (c) 2008, 2012 testtools developers. See LICENSE for details.
 
 from testtools import TestCase
 from testtools.matchers import Equals, MatchesException, Raises
-from testtools.content_type import ContentType, UTF8_TEXT
+from testtools.content_type import (
+    ContentType,
+    JSON,
+    UTF8_TEXT,
+    )
 
 
 class TestContentType(TestCase):
@@ -50,6 +54,12 @@
         self.assertThat(UTF8_TEXT.subtype, Equals('plain'))
         self.assertThat(UTF8_TEXT.parameters, Equals({'charset': 'utf8'}))
 
+    def test_json_content(self):
+        # The JSON content type represents UTF-8 encoded application/json.
+        self.assertThat(JSON.type, Equals('application'))
+        self.assertThat(JSON.subtype, Equals('json'))
+        self.assertThat(JSON.parameters, Equals({'charset': 'utf8'}))
+
 
 def test_suite():
     from unittest import TestLoader


Follow ups