unicode_literals ftw

This commit is contained in:
Honza Král
2014-05-29 01:16:17 +02:00
parent b1d36926c1
commit 756a6aee25
6 changed files with 18 additions and 15 deletions
+4 -2
View File
@@ -1,6 +1,8 @@
from __future__ import unicode_literals
from datetime import date, datetime
from functools import wraps
from ..compat import string_types, quote_plus, u
from ..compat import string_types, quote_plus
# parts of URL to be omitted
SKIP_IN_PATH = (None, '', [], ())
@@ -13,7 +15,7 @@ def _escape(value):
# make sequences into comma-separated stings
if isinstance(value, (list, tuple)):
value = u(',').join(value)
value = ','.join(value)
# dates and datetimes into isoformat
elif isinstance(value, (date, datetime)):
-2
View File
@@ -3,12 +3,10 @@ import sys
PY2 = sys.version_info[0] == 2
if PY2:
u = lambda s: s.decode('utf-8')
string_types = basestring,
from urllib import quote_plus, urlencode
from itertools import imap as map
else:
u = str
string_types = str, bytes
from urllib.parse import quote_plus, urlencode
map = map
+3 -2
View File
@@ -1,7 +1,8 @@
from __future__ import unicode_literals
from mock import patch
from elasticsearch.client import _normalize_hosts, Elasticsearch
from elasticsearch.compat import u
from ..test_cases import TestCase, ElasticsearchTestCase
@@ -15,7 +16,7 @@ class TestNormalizeHosts(TestCase):
def test_strings_are_parsed_for_port(self):
self.assertEquals(
[{"host": "elasticsearch.org", "port": 42}, {"host": "user:secret@elasticsearch.com"}],
_normalize_hosts(["elasticsearch.org:42", u("user:secret@elasticsearch.com")])
_normalize_hosts(["elasticsearch.org:42", "user:secret@elasticsearch.com"])
)
def test_dicts_are_left_unchanged(self):
+5 -3
View File
@@ -1,17 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from elasticsearch.client.utils import _make_path
from elasticsearch.compat import PY2, u
from elasticsearch.compat import PY2
from ..test_cases import TestCase, SkipTest
class TestMakePath(TestCase):
def test_handles_unicode(self):
id = u("中文")
id = "中文"
self.assertEquals('/some-index/type/%E4%B8%AD%E6%96%87', _make_path('some-index', 'type', id))
def test_handles_utf_encoded_string(self):
if not PY2:
raise SkipTest('Only relevant for py2')
id = u("中文").encode('utf-8')
id = "中文".encode('utf-8')
self.assertEquals('/some-index/type/%E4%B8%AD%E6%96%87', _make_path('some-index', 'type', id))
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
from elasticsearch import Elasticsearch, MemcachedConnection, NotFoundError
from __future__ import unicode_literals
from elasticsearch import Elasticsearch, MemcachedConnection, NotFoundError
from elasticsearch.transport import ADDRESS_RE
from elasticsearch.compat import u
from . import ElasticsearchTestCase
from ..test_cases import SkipTest
@@ -35,8 +35,8 @@ class TestMemcachedConnection(ElasticsearchTestCase):
self.assertEquals({"answer": 42}, self.mc_client.get("test_index", doc_type="test_type", id=1)["_source"])
def test_unicode(self):
self.mc_client.index("test_index", "test_type", {"answer": u("你好")}, id=u("你好"))
self.assertEquals({"answer": u("你好")}, self.mc_client.get("test_index", doc_type="test_type", id=u("你好"))["_source"])
self.mc_client.index("test_index", "test_type", {"answer": "你好"}, id="你好")
self.assertEquals({"answer": "你好"}, self.mc_client.get("test_index", doc_type="test_type", id="你好")["_source"])
def test_missing(self):
self.assertRaises(NotFoundError, self.mc_client.get, "test_index", doc_type="test_type", id=42)
+2 -2
View File
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import time
from elasticsearch.transport import Transport
from elasticsearch.connection import Connection
from elasticsearch.exceptions import ConnectionError
from elasticsearch.compat import u
from .test_cases import TestCase
@@ -55,7 +55,7 @@ class TestTransport(TestCase):
def test_body_gets_encoded_into_bytes(self):
t = Transport([{}], connection_class=DummyConnection)
t.perform_request('GET', '/', body=u('你好'))
t.perform_request('GET', '/', body='你好')
self.assertEquals(1, len(t.get_connection().calls))
self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd'), t.get_connection().calls[0][0])