unicode_literals ftw
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
from functools import wraps
|
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
|
# parts of URL to be omitted
|
||||||
SKIP_IN_PATH = (None, '', [], ())
|
SKIP_IN_PATH = (None, '', [], ())
|
||||||
@@ -13,7 +15,7 @@ def _escape(value):
|
|||||||
|
|
||||||
# make sequences into comma-separated stings
|
# make sequences into comma-separated stings
|
||||||
if isinstance(value, (list, tuple)):
|
if isinstance(value, (list, tuple)):
|
||||||
value = u(',').join(value)
|
value = ','.join(value)
|
||||||
|
|
||||||
# dates and datetimes into isoformat
|
# dates and datetimes into isoformat
|
||||||
elif isinstance(value, (date, datetime)):
|
elif isinstance(value, (date, datetime)):
|
||||||
|
|||||||
@@ -3,12 +3,10 @@ import sys
|
|||||||
PY2 = sys.version_info[0] == 2
|
PY2 = sys.version_info[0] == 2
|
||||||
|
|
||||||
if PY2:
|
if PY2:
|
||||||
u = lambda s: s.decode('utf-8')
|
|
||||||
string_types = basestring,
|
string_types = basestring,
|
||||||
from urllib import quote_plus, urlencode
|
from urllib import quote_plus, urlencode
|
||||||
from itertools import imap as map
|
from itertools import imap as map
|
||||||
else:
|
else:
|
||||||
u = str
|
|
||||||
string_types = str, bytes
|
string_types = str, bytes
|
||||||
from urllib.parse import quote_plus, urlencode
|
from urllib.parse import quote_plus, urlencode
|
||||||
map = map
|
map = map
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from mock import patch
|
from mock import patch
|
||||||
|
|
||||||
from elasticsearch.client import _normalize_hosts, Elasticsearch
|
from elasticsearch.client import _normalize_hosts, Elasticsearch
|
||||||
from elasticsearch.compat import u
|
|
||||||
|
|
||||||
from ..test_cases import TestCase, ElasticsearchTestCase
|
from ..test_cases import TestCase, ElasticsearchTestCase
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ class TestNormalizeHosts(TestCase):
|
|||||||
def test_strings_are_parsed_for_port(self):
|
def test_strings_are_parsed_for_port(self):
|
||||||
self.assertEquals(
|
self.assertEquals(
|
||||||
[{"host": "elasticsearch.org", "port": 42}, {"host": "user:[email protected]"}],
|
[{"host": "elasticsearch.org", "port": 42}, {"host": "user:[email protected]"}],
|
||||||
_normalize_hosts(["elasticsearch.org:42", u("user:[email protected]")])
|
_normalize_hosts(["elasticsearch.org:42", "user:[email protected]"])
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_dicts_are_left_unchanged(self):
|
def test_dicts_are_left_unchanged(self):
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from elasticsearch.client.utils import _make_path
|
from elasticsearch.client.utils import _make_path
|
||||||
from elasticsearch.compat import PY2, u
|
from elasticsearch.compat import PY2
|
||||||
|
|
||||||
from ..test_cases import TestCase, SkipTest
|
from ..test_cases import TestCase, SkipTest
|
||||||
|
|
||||||
class TestMakePath(TestCase):
|
class TestMakePath(TestCase):
|
||||||
def test_handles_unicode(self):
|
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))
|
self.assertEquals('/some-index/type/%E4%B8%AD%E6%96%87', _make_path('some-index', 'type', id))
|
||||||
|
|
||||||
def test_handles_utf_encoded_string(self):
|
def test_handles_utf_encoded_string(self):
|
||||||
if not PY2:
|
if not PY2:
|
||||||
raise SkipTest('Only relevant for 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))
|
self.assertEquals('/some-index/type/%E4%B8%AD%E6%96%87', _make_path('some-index', 'type', id))
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
# -*- coding: utf-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.transport import ADDRESS_RE
|
||||||
from elasticsearch.compat import u
|
|
||||||
|
|
||||||
from . import ElasticsearchTestCase
|
from . import ElasticsearchTestCase
|
||||||
from ..test_cases import SkipTest
|
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"])
|
self.assertEquals({"answer": 42}, self.mc_client.get("test_index", doc_type="test_type", id=1)["_source"])
|
||||||
|
|
||||||
def test_unicode(self):
|
def test_unicode(self):
|
||||||
self.mc_client.index("test_index", "test_type", {"answer": u("你好")}, id=u("你好"))
|
self.mc_client.index("test_index", "test_type", {"answer": "你好"}, id="你好")
|
||||||
self.assertEquals({"answer": u("你好")}, self.mc_client.get("test_index", doc_type="test_type", id=u("你好"))["_source"])
|
self.assertEquals({"answer": "你好"}, self.mc_client.get("test_index", doc_type="test_type", id="你好")["_source"])
|
||||||
|
|
||||||
def test_missing(self):
|
def test_missing(self):
|
||||||
self.assertRaises(NotFoundError, self.mc_client.get, "test_index", doc_type="test_type", id=42)
|
self.assertRaises(NotFoundError, self.mc_client.get, "test_index", doc_type="test_type", id=42)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from elasticsearch.transport import Transport
|
from elasticsearch.transport import Transport
|
||||||
from elasticsearch.connection import Connection
|
from elasticsearch.connection import Connection
|
||||||
from elasticsearch.exceptions import ConnectionError
|
from elasticsearch.exceptions import ConnectionError
|
||||||
from elasticsearch.compat import u
|
|
||||||
|
|
||||||
from .test_cases import TestCase
|
from .test_cases import TestCase
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ class TestTransport(TestCase):
|
|||||||
def test_body_gets_encoded_into_bytes(self):
|
def test_body_gets_encoded_into_bytes(self):
|
||||||
t = Transport([{}], connection_class=DummyConnection)
|
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(1, len(t.get_connection().calls))
|
||||||
self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd'), t.get_connection().calls[0][0])
|
self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd'), t.get_connection().calls[0][0])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user