Run Black+Flake8 on project
This commit is contained in:
committed by
Seth Michael Larson
parent
3a89b7bd01
commit
210fae23d0
@@ -1,11 +1,6 @@
|
||||
from collections import defaultdict
|
||||
|
||||
try:
|
||||
# python 2.6
|
||||
from unittest2 import TestCase, SkipTest
|
||||
except ImportError:
|
||||
from unittest import TestCase, SkipTest
|
||||
|
||||
from unittest import TestCase
|
||||
from unittest import SkipTest # noqa: F401
|
||||
from elasticsearch import Elasticsearch
|
||||
|
||||
|
||||
|
||||
@@ -25,9 +25,7 @@ def gzip_decompress(data):
|
||||
|
||||
|
||||
class TestUrllib3Connection(TestCase):
|
||||
def _get_mock_connection(
|
||||
self, connection_params={}, response_body=b"{}"
|
||||
):
|
||||
def _get_mock_connection(self, connection_params={}, response_body=b"{}"):
|
||||
con = Urllib3HttpConnection(**connection_params)
|
||||
|
||||
def _dummy_urlopen(*args, **kwargs):
|
||||
@@ -66,7 +64,9 @@ class TestUrllib3Connection(TestCase):
|
||||
con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243"
|
||||
)
|
||||
self.assertEquals(con.port, 9243)
|
||||
self.assertEquals(con.hostname, "0fd50f62320ed6539f6cb48e1b68.example.cloud.com")
|
||||
self.assertEquals(
|
||||
con.hostname, "0fd50f62320ed6539f6cb48e1b68.example.cloud.com"
|
||||
)
|
||||
self.assertTrue(con.http_compress)
|
||||
|
||||
def test_api_key_auth(self):
|
||||
@@ -75,16 +75,24 @@ class TestUrllib3Connection(TestCase):
|
||||
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
|
||||
api_key=("elastic", "changeme1"),
|
||||
)
|
||||
self.assertEquals(con.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTE=")
|
||||
self.assertEquals(con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243")
|
||||
self.assertEquals(
|
||||
con.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTE="
|
||||
)
|
||||
self.assertEquals(
|
||||
con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243"
|
||||
)
|
||||
|
||||
# test with base64 encoded string
|
||||
con = Urllib3HttpConnection(
|
||||
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
|
||||
api_key="ZWxhc3RpYzpjaGFuZ2VtZTI=",
|
||||
)
|
||||
self.assertEquals(con.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTI=")
|
||||
self.assertEquals(con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243")
|
||||
self.assertEquals(
|
||||
con.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTI="
|
||||
)
|
||||
self.assertEquals(
|
||||
con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243"
|
||||
)
|
||||
|
||||
def test_no_http_compression(self):
|
||||
con = self._get_mock_connection()
|
||||
@@ -134,19 +142,22 @@ class TestUrllib3Connection(TestCase):
|
||||
|
||||
con = Urllib3HttpConnection(
|
||||
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
|
||||
http_compress=False
|
||||
http_compress=False,
|
||||
)
|
||||
self.assertEquals(con.http_compress, False)
|
||||
|
||||
con = Urllib3HttpConnection(
|
||||
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
|
||||
http_compress=True
|
||||
http_compress=True,
|
||||
)
|
||||
self.assertEquals(con.http_compress, True)
|
||||
|
||||
def test_default_user_agent(self):
|
||||
con = Urllib3HttpConnection()
|
||||
self.assertEquals(con._get_default_user_agent(), "elasticsearch-py/%s (Python %s)" % (__versionstr__, python_version()))
|
||||
self.assertEquals(
|
||||
con._get_default_user_agent(),
|
||||
"elasticsearch-py/%s (Python %s)" % (__versionstr__, python_version()),
|
||||
)
|
||||
|
||||
def test_timeout_set(self):
|
||||
con = Urllib3HttpConnection(timeout=42)
|
||||
@@ -254,13 +265,13 @@ class TestUrllib3Connection(TestCase):
|
||||
@patch("elasticsearch.connection.base.logger")
|
||||
def test_uncompressed_body_logged(self, logger):
|
||||
con = self._get_mock_connection(connection_params={"http_compress": True})
|
||||
con.perform_request("GET", "/", body=b"{\"example\": \"body\"}")
|
||||
con.perform_request("GET", "/", body=b'{"example": "body"}')
|
||||
|
||||
self.assertEquals(2, logger.debug.call_count)
|
||||
req, resp = logger.debug.call_args_list
|
||||
print(req, resp)
|
||||
self.assertEquals('> {"example": "body"}', req[0][0] % req[0][1:])
|
||||
self.assertEquals('< {}', resp[0][0] % resp[0][1:])
|
||||
self.assertEquals("< {}", resp[0][0] % resp[0][1:])
|
||||
|
||||
|
||||
class TestRequestsConnection(TestCase):
|
||||
@@ -315,7 +326,9 @@ class TestRequestsConnection(TestCase):
|
||||
con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243"
|
||||
)
|
||||
self.assertEquals(con.port, 9243)
|
||||
self.assertEquals(con.hostname, "0fd50f62320ed6539f6cb48e1b68.example.cloud.com")
|
||||
self.assertEquals(
|
||||
con.hostname, "0fd50f62320ed6539f6cb48e1b68.example.cloud.com"
|
||||
)
|
||||
self.assertTrue(con.http_compress)
|
||||
|
||||
def test_api_key_auth(self):
|
||||
@@ -324,16 +337,24 @@ class TestRequestsConnection(TestCase):
|
||||
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
|
||||
api_key=("elastic", "changeme1"),
|
||||
)
|
||||
self.assertEquals(con.session.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTE=")
|
||||
self.assertEquals(con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243")
|
||||
self.assertEquals(
|
||||
con.session.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTE="
|
||||
)
|
||||
self.assertEquals(
|
||||
con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243"
|
||||
)
|
||||
|
||||
# test with base64 encoded string
|
||||
con = RequestsHttpConnection(
|
||||
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
|
||||
api_key="ZWxhc3RpYzpjaGFuZ2VtZTI=",
|
||||
)
|
||||
self.assertEquals(con.session.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTI=")
|
||||
self.assertEquals(con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243")
|
||||
self.assertEquals(
|
||||
con.session.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTI="
|
||||
)
|
||||
self.assertEquals(
|
||||
con.host, "https://0fd50f62320ed6539f6cb48e1b68.example.cloud.com:9243"
|
||||
)
|
||||
|
||||
def test_no_http_compression(self):
|
||||
con = self._get_mock_connection()
|
||||
@@ -348,9 +369,7 @@ class TestRequestsConnection(TestCase):
|
||||
self.assertNotIn("accept-encoding", req.headers)
|
||||
|
||||
def test_http_compression(self):
|
||||
con = self._get_mock_connection(
|
||||
{"http_compress": True},
|
||||
)
|
||||
con = self._get_mock_connection({"http_compress": True},)
|
||||
|
||||
self.assertTrue(con.http_compress)
|
||||
|
||||
@@ -380,13 +399,13 @@ class TestRequestsConnection(TestCase):
|
||||
|
||||
con = RequestsHttpConnection(
|
||||
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
|
||||
http_compress=False
|
||||
http_compress=False,
|
||||
)
|
||||
self.assertEquals(con.http_compress, False)
|
||||
|
||||
con = RequestsHttpConnection(
|
||||
cloud_id="foobar:ZXhhbXBsZS5jbG91ZC5jb20kMGZkNTBmNjIzMjBlZDY1MzlmNmNiNDhlMWI2OCRhYzUzOTVhODgz\nNDU2NmM5ZjE1Y2Q4ZTQ5MGE=\n",
|
||||
http_compress=True
|
||||
http_compress=True,
|
||||
)
|
||||
self.assertEquals(con.http_compress, True)
|
||||
|
||||
@@ -442,10 +461,15 @@ class TestRequestsConnection(TestCase):
|
||||
|
||||
def test_custom_headers(self):
|
||||
con = self._get_mock_connection()
|
||||
req = self._get_request(con, "GET", "/", headers={
|
||||
"content-type": "application/x-ndjson",
|
||||
"user-agent": "custom-agent/1.2.3",
|
||||
})
|
||||
req = self._get_request(
|
||||
con,
|
||||
"GET",
|
||||
"/",
|
||||
headers={
|
||||
"content-type": "application/x-ndjson",
|
||||
"user-agent": "custom-agent/1.2.3",
|
||||
},
|
||||
)
|
||||
self.assertEquals(req.headers["content-type"], "application/x-ndjson")
|
||||
self.assertEquals(req.headers["user-agent"], "custom-agent/1.2.3")
|
||||
|
||||
@@ -506,7 +530,7 @@ class TestRequestsConnection(TestCase):
|
||||
self.assertEquals(1, logger.warning.call_count)
|
||||
self.assertTrue(
|
||||
re.match(
|
||||
"^GET http://localhost:9200/\?param=42 \[status:500 request:0.[0-9]{3}s\]",
|
||||
r"^GET http://localhost:9200/\?param=42 \[status:500 request:0.[0-9]{3}s\]",
|
||||
logger.warning.call_args[0][0] % logger.warning.call_args[0][1:],
|
||||
)
|
||||
)
|
||||
@@ -532,7 +556,7 @@ class TestRequestsConnection(TestCase):
|
||||
self.assertEquals(1, tracer.debug.call_count)
|
||||
self.assertTrue(
|
||||
re.match(
|
||||
'#\[200\] \(0.[0-9]{3}s\)\n#\{\n# "answer": "that\\\\u0027s it!"\n#\}',
|
||||
r'#\[200\] \(0.[0-9]{3}s\)\n#{\n# "answer": "that\\u0027s it!"\n#}',
|
||||
tracer.debug.call_args[0][0] % tracer.debug.call_args[0][1:],
|
||||
)
|
||||
)
|
||||
@@ -541,7 +565,7 @@ class TestRequestsConnection(TestCase):
|
||||
self.assertEquals(1, logger.info.call_count)
|
||||
self.assertTrue(
|
||||
re.match(
|
||||
"GET http://localhost:9200/\?param=42 \[status:200 request:0.[0-9]{3}s\]",
|
||||
r"GET http://localhost:9200/\?param=42 \[status:200 request:0.[0-9]{3}s\]",
|
||||
logger.info.call_args[0][0] % logger.info.call_args[0][1:],
|
||||
)
|
||||
)
|
||||
@@ -554,12 +578,12 @@ class TestRequestsConnection(TestCase):
|
||||
@patch("elasticsearch.connection.base.logger")
|
||||
def test_uncompressed_body_logged(self, logger):
|
||||
con = self._get_mock_connection(connection_params={"http_compress": True})
|
||||
con.perform_request("GET", "/", body=b"{\"example\": \"body\"}")
|
||||
con.perform_request("GET", "/", body=b'{"example": "body"}')
|
||||
|
||||
self.assertEquals(2, logger.debug.call_count)
|
||||
req, resp = logger.debug.call_args_list
|
||||
self.assertEquals('> {"example": "body"}', req[0][0] % req[0][1:])
|
||||
self.assertEquals('< {}', resp[0][0] % resp[0][1:])
|
||||
self.assertEquals("< {}", resp[0][0] % resp[0][1:])
|
||||
|
||||
def test_defaults(self):
|
||||
con = self._get_mock_connection()
|
||||
|
||||
@@ -77,6 +77,6 @@ class TestDeserializer(TestCase):
|
||||
self.assertRaises(SerializationError, self.de.loads, "{}", "text/html")
|
||||
|
||||
def test_raises_improperly_configured_when_default_mimetype_cannot_be_deserialized(
|
||||
self
|
||||
self,
|
||||
):
|
||||
self.assertRaises(ImproperlyConfigured, Deserializer, {})
|
||||
|
||||
@@ -24,7 +24,13 @@ PARAMS_RENAMES = {"type": "doc_type", "from": "from_"}
|
||||
CATCH_CODES = {"missing": 404, "conflict": 409, "unauthorized": 401}
|
||||
|
||||
# test features we have implemented
|
||||
IMPLEMENTED_FEATURES = {"gtelte", "stash_in_path", "headers", "catch_unauthorized", "default_shards"}
|
||||
IMPLEMENTED_FEATURES = {
|
||||
"gtelte",
|
||||
"stash_in_path",
|
||||
"headers",
|
||||
"catch_unauthorized",
|
||||
"default_shards",
|
||||
}
|
||||
|
||||
# broken YAML tests on some releases
|
||||
SKIP_TESTS = {
|
||||
@@ -35,7 +41,7 @@ SKIP_TESTS = {
|
||||
"TestScripts20GetScriptContext",
|
||||
"TestScripts25GetScriptLanguages",
|
||||
# Disallowing expensive queries is 7.7+
|
||||
"TestSearch320DisallowQueries"
|
||||
"TestSearch320DisallowQueries",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import time
|
||||
from elasticsearch.transport import Transport, get_host_info
|
||||
from elasticsearch.connection import Connection
|
||||
from elasticsearch.connection_pool import DummyConnectionPool
|
||||
from elasticsearch.exceptions import ConnectionError, ImproperlyConfigured
|
||||
from elasticsearch.exceptions import ConnectionError
|
||||
|
||||
from .test_cases import TestCase
|
||||
|
||||
@@ -107,11 +107,7 @@ class TestTransport(TestCase):
|
||||
self.assertEquals(1, len(t.get_connection().calls))
|
||||
self.assertEquals(("GET", "/", {}, None), t.get_connection().calls[0][0])
|
||||
self.assertEquals(
|
||||
{
|
||||
"timeout": 42,
|
||||
"ignore": (),
|
||||
"headers": None,
|
||||
},
|
||||
{"timeout": 42, "ignore": (), "headers": None},
|
||||
t.get_connection().calls[0][1],
|
||||
)
|
||||
|
||||
@@ -121,7 +117,10 @@ class TestTransport(TestCase):
|
||||
t.perform_request("GET", "/", headers={"user-agent": "my-custom-value/1.2.3"})
|
||||
self.assertEquals(1, len(t.get_connection().calls))
|
||||
self.assertEquals(
|
||||
{"timeout": None, "ignore": (), "headers": {"user-agent": "my-custom-value/1.2.3"}
|
||||
{
|
||||
"timeout": None,
|
||||
"ignore": (),
|
||||
"headers": {"user-agent": "my-custom-value/1.2.3"},
|
||||
},
|
||||
t.get_connection().calls[0][1],
|
||||
)
|
||||
@@ -323,5 +322,7 @@ class TestTransport(TestCase):
|
||||
)
|
||||
t.sniff_hosts()
|
||||
# Ensure we parsed out the fqdn and port from the fqdn/ip:port string.
|
||||
self.assertEqual(t.connection_pool.connection_opts[0][1],
|
||||
{'host': 'somehost.tld', 'port': 123})
|
||||
self.assertEqual(
|
||||
t.connection_pool.connection_opts[0][1],
|
||||
{"host": "somehost.tld", "port": 123},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user