Merge lp:~jderose/microfiber/ensure_ascii into lp:microfiber

Proposed by Jason Gerard DeRose
Status: Merged
Merged at revision: 124
Proposed branch: lp:~jderose/microfiber/ensure_ascii
Merge into: lp:microfiber
Diff against target: 135 lines (+56/-7)
2 files modified
microfiber.py (+7/-3)
test_microfiber.py (+49/-4)
To merge this branch: bzr merge lp:~jderose/microfiber/ensure_ascii
Reviewer Review Type Date Requested Status
David Jordan Approve
Review via email: mp+117379@code.launchpad.net

Description of the change

* Adds non-ascii values in _json_body() test

* Fixes _json_body() so it uses ensure_ascii=False

* Adds non-ascii values in _queryiter() test

* Fixes _queryiter() so it uses ensure_ascii=False

* Fixes Database.dump() so it uses ensure_ascii=False, even though it's not an official part of the API yet, doesn't have tests yet

To post a comment you must log in.
Revision history for this message
David Jordan (dmj726) wrote :

Good catch!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'microfiber.py'
2--- microfiber.py 2012-07-26 09:23:56 +0000
3+++ microfiber.py 2012-07-31 06:38:23 +0000
4@@ -133,7 +133,11 @@
5 return None
6 if isinstance(obj, (bytes, BufferedReader)):
7 return obj
8- return json.dumps(obj, sort_keys=True, separators=(',',':')).encode('utf-8')
9+ return json.dumps(obj,
10+ ensure_ascii=False,
11+ sort_keys=True,
12+ separators=(',',':'),
13+ ).encode('utf-8')
14
15
16 def _queryiter(options):
17@@ -146,7 +150,7 @@
18 for key in sorted(options):
19 value = options[key]
20 if key in ('key', 'startkey', 'endkey') or not isinstance(value, str):
21- value = json.dumps(value)
22+ value = json.dumps(value, ensure_ascii=False)
23 yield (key, value)
24
25
26@@ -834,7 +838,7 @@
27 rows = self.get('_all_docs')['rows']
28 iterable = iter_all_docs(rows, self, attachments)
29 docs = FakeList(len(rows), iterable)
30- json.dump({'docs': docs}, fp, sort_keys=True, indent=4, separators=(',', ': '))
31+ json.dump({'docs': docs}, fp, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))
32
33 def load(self, fp):
34 return self.post(fp, '_bulk_docs')
35
36=== modified file 'test_microfiber.py'
37--- test_microfiber.py 2012-07-26 09:23:56 +0000
38+++ test_microfiber.py 2012-07-31 06:38:23 +0000
39@@ -120,14 +120,31 @@
40 self.assertIsInstance(b, bytes)
41 self.assertEqual(len(b) * 8, 80)
42
43+ def test_json(self):
44+ """
45+ Test our assumptions about json.dumps().
46+ """
47+ tm = '™'
48+ self.assertEqual(json.dumps(tm), '"\\u2122"')
49+ self.assertEqual(json.dumps(tm, ensure_ascii=False), '"™"')
50+
51 def test_json_body(self):
52 doc = {
53 '_id': 'foo',
54 'bar': 'baz',
55 'hello': 'world',
56+ 'name': 'Jon Åslund',
57 }
58- json_str = json.dumps(doc, sort_keys=True, separators=(',',':'))
59- json_str2 = json.dumps(json_str, sort_keys=True, separators=(',',':'))
60+ json_str = json.dumps(doc,
61+ ensure_ascii=False,
62+ sort_keys=True,
63+ separators=(',',':'),
64+ )
65+ json_str2 = json.dumps(json_str,
66+ ensure_ascii=False,
67+ sort_keys=True,
68+ separators=(',',':'),
69+ )
70 json_bytes = json_str.encode('utf-8')
71
72 # Test with obj=None
73@@ -141,11 +158,15 @@
74 microfiber._json_body(json_str),
75 json_str2.encode('utf-8')
76 )
77-
78+
79 # Test other stuff that should get JSON encoded:
80 self.assertEqual(microfiber._json_body(True), b'true')
81 self.assertEqual(microfiber._json_body(False), b'false')
82 self.assertEqual(microfiber._json_body('hello'), b'"hello"')
83+ self.assertEqual(
84+ microfiber._json_body('*safe solvent™'),
85+ b'"*safe solvent\xe2\x84\xa2"'
86+ )
87 self.assertEqual(microfiber._json_body(18), b'18')
88 self.assertEqual(microfiber._json_body(17.9), b'17.9')
89 self.assertEqual(microfiber._json_body({}), b'{}')
90@@ -153,7 +174,7 @@
91 microfiber._json_body(['one', True, 3]),
92 b'["one",true,3]'
93 )
94-
95+
96 # Test when obj in an open file
97 d = tempfile.mkdtemp()
98 try:
99@@ -219,6 +240,21 @@
100 ]
101 )
102
103+ # Test key, startkey, endkey with non-ascii values
104+ options = dict(
105+ key='Hanna Sköld',
106+ startkey='Matias Särs',
107+ endkey='Paweł Moll',
108+ )
109+ self.assertEqual(
110+ list(microfiber._queryiter(options)),
111+ [
112+ ('endkey', '"Paweł Moll"'),
113+ ('key', '"Hanna Sköld"'),
114+ ('startkey', '"Matias Särs"'),
115+ ]
116+ )
117+
118 def test_oauth_base_string(self):
119 f = microfiber._oauth_base_string
120
121@@ -1216,6 +1252,15 @@
122 self.assertEqual(inst.delete(), {'ok': True})
123 self.assertRaises(NotFound, inst.get)
124
125+ def test_non_ascii(self):
126+ inst = self.klass(self.db, self.env)
127+ self.assertTrue(inst.ensure())
128+ _id = test_id()
129+ name = '*safe solvent™'
130+ doc = {'_id': _id, 'name': name}
131+ inst.save(doc)
132+ self.assertEqual(inst.get(_id)['name'], name)
133+
134 def test_save(self):
135 inst = self.klass(self.db, self.env)
136

Subscribers

People subscribed via source and target branches