def test_update_wrong_method(self): import json request = { 'method': 'GET', 'query': {}, 'body': '', } commands = [ self.command_ddoc_add(), [ 'ddoc', '_design/test_update', ['updates', 'inc'], [None, request] ], ] input = StringIO(b'\n'.join(json.dumps(c).encode('utf-8') for c in commands)) output = StringIO() view.run(input=input, output=output) results = [ json.loads(l.decode('utf-8')) for l in output.getvalue().strip().split(b'\n') ] self.assertEqual(len(results), 2) self.assertEqual(results[1][2]['status'], 405)
def test_rereduce(self): input = StringIO(b'["rereduce", ' b'["def fun(keys, values, rereduce): return sum(values)"], ' b'[1, 2, 3]]\n') output = StringIO() view.run(input=input, output=output) self.assertEqual(output.getvalue(), b'[true, [6]]\n')
def test_i18n(self): input = StringIO(b'["add_fun", "def fun(doc): yield doc[\\"test\\"], doc"]\n' b'["map_doc", {"test": "b\xc3\xa5r"}]\n') output = StringIO() view.run(input=input, output=output) self.assertEqual(output.getvalue(), b'true\n' b'[[["b\xc3\xa5r", {"test": "b\xc3\xa5r"}]]]\n')
def test_map_doc(self): input = StringIO(b'["add_fun", "def fun(doc): yield None, doc"]\n' b'["map_doc", {"foo": "bar"}]\n') output = StringIO() view.run(input=input, output=output) self.assertEqual(output.getvalue(), b'true\n' b'[[[null, {"foo": "bar"}]]]\n')
def test_reduce_with_logging(self): input = StringIO(b'["reduce", ' b'["def fun(keys, values): log(\'Summing %r\' % (values,)); return sum(values)"], ' b'[[null, 1], [null, 2], [null, 3]]]\n') output = StringIO() view.run(input=input, output=output) self.assertEqual(output.getvalue(), b'{"log": "Summing (1, 2, 3)"}\n' b'[true, [6]]\n')
def test_map_doc_with_logging_json(self): fun = b'def fun(doc): log([1, 2, 3]); yield None, doc' input = StringIO(b'["add_fun", "' + fun + b'"]\n' b'["map_doc", {"foo": "bar"}]\n') output = StringIO() view.run(input=input, output=output) self.assertEqual(output.getvalue(), b'true\n' b'{"log": "[1, 2, 3]"}\n' b'[[[null, {"foo": "bar"}]]]\n')
def test_unicode_content(self): buf = StringIO() envelope = multipart.write_multipart(buf, boundary='==123456789==') envelope.add('text/plain', u'Iñtërnâtiônàlizætiøn') envelope.close() self.assertEqual(u'''Content-Type: multipart/mixed; boundary="==123456789==" --==123456789== Content-Length: 27 Content-MD5: 5eYoIG5zsa5ps3/Gl2Kh4Q== Content-Type: text/plain;charset=utf-8 Iñtërnâtiônàlizætiøn --==123456789==-- '''.encode('utf-8'), buf.getvalue().replace(b'\r\n', b'\n'))
def test_unicode_content(self): buf = StringIO() envelope = multipart.write_multipart(buf, boundary='==123456789==') envelope.add('text/plain', u'Iñtërnâtiônàlizætiøn') envelope.close() self.assertEqual( u'''Content-Type: multipart/mixed; boundary="==123456789==" --==123456789== Content-Length: 27 Content-MD5: 5eYoIG5zsa5ps3/Gl2Kh4Q== Content-Type: text/plain;charset=utf-8 Iñtërnâtiônàlizætiøn --==123456789==-- '''.encode('utf-8'), buf.getvalue().replace(b'\r\n', b'\n'))
def test_handle_credentials(self): # Similar to issue 194 # Fixing: AttributeError: 'Resource' object has no attribute 'http' try: dump.dump_db(self.db.resource.url, 'foo', 'bar', output=StringIO()) except Unauthorized: # This is ok, since we provided dummy credentials. pass
def test_unicode_headers_charset(self): # http://code.google.com/p/couchdb-python/issues/detail?id=179 buf = StringIO() envelope = multipart.write_multipart(buf, boundary='==123456789==') envelope.add('application/json;charset=utf-8', '{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09",' ' "_id": "文档"}', headers={'Content-ID': u"文档"}) self.assertEqual(u'''Content-Type: multipart/mixed; boundary="==123456789==" --==123456789== Content-ID: =?utf-8?b?5paH5qGj?= Content-Length: 63 Content-MD5: Cpw3iC3xPua8YzKeWLzwvw== Content-Type: application/json;charset=utf-8 {"_rev": "3-bc27b6930ca514527d8954c7c43e6a09", "_id": "文档"} '''.encode('utf-8'), buf.getvalue().replace(b'\r\n', b'\n'))
def test_unicode_headers(self): # http://code.google.com/p/couchdb-python/issues/detail?id=179 buf = StringIO() envelope = multipart.write_multipart(buf, boundary='==123456789==') envelope.add('application/json', '{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09",' ' "_id": "文档"}', headers={'Content-ID': u"文档"}) self.assertEqual( u'''Content-Type: multipart/mixed; boundary="==123456789==" --==123456789== Content-ID: =?utf-8?b?5paH5qGj?= Content-Length: 63 Content-MD5: Cpw3iC3xPua8YzKeWLzwvw== Content-Type: application/json;charset=utf-8 {"_rev": "3-bc27b6930ca514527d8954c7c43e6a09", "_id": "文档"} '''.encode('utf-8'), buf.getvalue().replace(b'\r\n', b'\n'))
def test_unicode_content(self): buf = StringIO() envelope = multipart.write_multipart(buf, boundary="==123456789==") envelope.add("text/plain", u"Iñtërnâtiônàlizætiøn") envelope.close() self.assertEqual( u"""Content-Type: multipart/mixed; boundary="==123456789==" --==123456789== Content-Length: 27 Content-MD5: 5eYoIG5zsa5ps3/Gl2Kh4Q== Content-Type: text/plain;charset=utf-8 Iñtërnâtiônàlizætiøn --==123456789==-- """.encode( "utf-8" ), buf.getvalue().replace(b"\r\n", b"\n"), )
def test_update(self): import json request = { 'method': 'POST', 'query': {}, 'body': '', } commands = [ self.command_ddoc_add(), [ 'ddoc', '_design/test_update', ['updates', 'inc'], [None, request] ], [ 'ddoc', '_design/test_update', ['updates', 'inc'], [{'field': 41, 'other_field': 'x'}, request] ], ] input = StringIO(b'\n'.join(json.dumps(c).encode('utf-8') for c in commands)) output = StringIO() view.run(input=input, output=output) results = [ json.loads(l.decode('utf-8')) for l in output.getvalue().strip().split(b'\n') ] self.assertEqual(len(results), 3) self.assertEqual(results[0], True) self.assertEqual(results[1], ['up', None, {'body': '.'}]) self.assertEqual( results[2], ['up', {'field': 42, 'other_field': 'x'}, {'body': '.'}])
def test_unicode_headers(self): # http://code.google.com/p/couchdb-python/issues/detail?id=179 dump = u'''Content-Type: multipart/mixed; boundary="==123456789==" --==123456789== Content-ID: =?utf-8?b?5paH5qGj?= Content-Length: 63 Content-MD5: Cpw3iC3xPua8YzKeWLzwvw== Content-Type: application/json {"_rev": "3-bc27b6930ca514527d8954c7c43e6a09", "_id": "文档"} ''' parts = multipart.read_multipart(StringIO(dump.encode('utf-8'))) for headers, is_multipart, payload in parts: self.assertEqual(headers['content-id'], u'文档') break
def test_flat(self): text = b'''\ Content-Type: multipart/mixed; boundary="===============1946781859==" --===============1946781859== Content-Type: application/json Content-ID: bar ETag: "1-4229094393" { "_id": "bar", "_rev": "1-4229094393" } --===============1946781859== Content-Type: application/json Content-ID: foo ETag: "1-2182689334" { "_id": "foo", "_rev": "1-2182689334", "something": "cool" } --===============1946781859==-- ''' num = 0 parts = multipart.read_multipart(StringIO(text)) for headers, is_multipart, payload in parts: self.assertEqual(is_multipart, False) self.assertEqual('application/json', headers['content-type']) if num == 0: self.assertEqual('bar', headers['content-id']) self.assertEqual('"1-4229094393"', headers['etag']) self.assertEqual( b'{\n "_id": "bar",\n ' b'"_rev": "1-4229094393"\n}', payload) elif num == 1: self.assertEqual('foo', headers['content-id']) self.assertEqual('"1-2182689334"', headers['etag']) self.assertEqual( b'{\n "_id": "foo",\n "_rev": "1-2182689334",' b'\n "something": "cool"\n}', payload) num += 1 self.assertEqual(num, 2)
def test_handle_credentials(self): # Issue 194: couchdb-load attribute error: 'Resource' object has no attribute 'http' # http://code.google.com/p/couchdb-python/issues/detail?id=194 load.load_db(StringIO(b''), self.db.resource.url, 'foo', 'bar')
def _get_attachment(self, _id): attachment = self._db.get_attachment(_id, 'value') if isinstance(attachment, couchdb.http.ResponseBody): attachment = StringIO(attachment.read()) return attachment
def test_nested(self): text = b'''\ Content-Type: multipart/mixed; boundary="===============1946781859==" --===============1946781859== Content-Type: application/json Content-ID: bar ETag: "1-4229094393" { "_id": "bar", "_rev": "1-4229094393" } --===============1946781859== Content-Type: multipart/mixed; boundary="===============0909101126==" Content-ID: foo ETag: "1-919589747" --===============0909101126== Content-Type: application/json { "_id": "foo", "_rev": "1-919589747", "something": "cool" } --===============0909101126== Content-Type: text/plain Content-ID: mail.txt Hello, friends. How are you doing? Regards, Chris --===============0909101126==-- --===============1946781859== Content-Type: application/json Content-ID: baz ETag: "1-3482142493" { "_id": "baz", "_rev": "1-3482142493" } --===============1946781859==-- ''' num = 0 parts = multipart.read_multipart(StringIO(text)) for headers, is_multipart, payload in parts: if num == 0: self.assertEqual(is_multipart, False) self.assertEqual('application/json', headers['content-type']) self.assertEqual('bar', headers['content-id']) self.assertEqual('"1-4229094393"', headers['etag']) self.assertEqual( b'{\n "_id": "bar", \n ' b'"_rev": "1-4229094393"\n}', payload) elif num == 1: self.assertEqual(is_multipart, True) self.assertEqual('foo', headers['content-id']) self.assertEqual('"1-919589747"', headers['etag']) partnum = 0 for headers, is_multipart, payload in payload: self.assertEqual(is_multipart, False) if partnum == 0: self.assertEqual('application/json', headers['content-type']) self.assertEqual( b'{\n "_id": "foo", \n "_rev": ' b'"1-919589747", \n "something": ' b'"cool"\n}', payload) elif partnum == 1: self.assertEqual('text/plain', headers['content-type']) self.assertEqual('mail.txt', headers['content-id']) self.assertEqual( b'Hello, friends.\nHow are you doing?' b'\n\nRegards, Chris', payload) partnum += 1 elif num == 2: self.assertEqual(is_multipart, False) self.assertEqual('application/json', headers['content-type']) self.assertEqual('baz', headers['content-id']) self.assertEqual('"1-3482142493"', headers['etag']) self.assertEqual( b'{\n "_id": "baz", \n ' b'"_rev": "1-3482142493"\n}', payload) num += 1 self.assertEqual(num, 3)
def test_unicode_content_ascii(self): buf = StringIO() envelope = multipart.write_multipart(buf, boundary='==123456789==') self.assertRaises(UnicodeEncodeError, envelope.add, 'text/plain;charset=ascii', u'Iñtërnâtiônàlizætiøn')
def test_reset(self): input = StringIO(b'["reset"]\n') output = StringIO() view.run(input=input, output=output) self.assertEqual(output.getvalue(), b'true\n')
def test_add_fun(self): input = StringIO(b'["add_fun", "def fun(doc): yield None, doc"]\n') output = StringIO() view.run(input=input, output=output) self.assertEqual(output.getvalue(), b'true\n')