10000 Merge branch 'master' of github.com:influxdb/influxdb-python into HEAD · kespindler/influxdb-python@d633692 · GitHub
[go: up one dir, main page]

Skip to content

Commit d633692

Browse files
author
aviau
committed
Merge branch 'master' of github.com:influxdb/influxdb-python into HEAD
2 parents 269549e + 99b7b9e commit d633692

File tree

3 files changed

+94
-39
lines changed

3 files changed

+94
-39
lines changed

influxdb/client.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Python client for InfluxDB
44
"""
5-
from collections import OrderedDict
5+
66
from functools import wraps
77
import json
88
import socket
@@ -180,27 +180,6 @@ def from_DSN(dsn, **kwargs):
180180

181181
return InfluxDBClient(**init_args)
182182

183-
#
184-
# By default we keep the "order" of the json responses:
185-
# more clearly: any dict contained in the json response will have
186-
# its key-value items order kept as in the raw answer, thanks to
187-
# `collections.OrderedDict`.
188-
# if one doesn't care in that, then it can simply change its client
189-
# instance 'keep_json_response_order' attribute value (to a falsy one).
190-
# This will then eventually help for performance considerations.
191-
_keep_json_response_order = False
192-
# NB: For "group by" query type :
193-
# This setting is actually necessary in order to have a consistent and
194-
# reproducible rsp format if you "group by" on more than 1 tag.
195-
196-
@property
197-
def keep_json_response_order(self):
198-
return self._keep_json_response_order
199-
200-
@keep_json_response_order.setter
201-
def keep_json_response_order(self, new_value):
202-
self._keep_json_response_order = new_value
203-
204183
def switch_database(self, database):
205184
"""
206185
switch_database()
@@ -309,10 +288,7 @@ def query(self,
309288
expected_response_code=expected_response_code
310289
)
311290

312-
json_kw = {}
313-
if self.keep_json_response_order:
314-
json_kw.update(object_pairs_hook=OrderedDict)
315-
data = response.json(**json_kw)
291+
data = response.json()
316292

317293
return ResultSet(data)
318294

influxdb/influxdb08/client.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ def list_chunks(l, n):
291291
yield l[i:i + n]
292292

293293
batch_size = kwargs.get('batch_size')
294-
if batch_size:
294+
if batch_size and batch_size > 0:
295295
for item in data:
296296
name = item.get('name')
297297
columns = item.get('columns')
298-
point_list = item.get('points')
298+
point_list = item.get('points', [])
299299

300300
for batch in list_chunks(point_list, batch_size):
301301
item = [{
@@ -306,10 +306,10 @@ def list_chunks(l, n):
306306
self._write_points(
307307
data=item,
308308
time_precision=time_precision)
309-
310-
return True
311-
312-
return self._write_points(data=data, time_precision=time_precision)
309+
return True
310+
else:
311+
return self._write_points(data=data,
312+
time_precision=time_precision)
313313

314314
def write_points_with_precision(self, data, time_precision='s'):
315315
"""
@@ -448,7 +448,13 @@ def _query(self, query, time_precision='s', chunked=False):
448448
)
449449

450450
if chunked:
451-
return list(chunked_json.loads(response.content.decode()))
451+
decoded = {}
452+
try:
453+
decoded = chunked_json.loads(response.content.decode())
454+
except UnicodeDecodeError:
455+
decoded = chunked_json.loads(response.content.decode('utf-8'))
456+
finally:
457+
return list(decoded)
452458
else:
453459
return response.json()
454460

tests/influxdb/influxdb08/client_test.py

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
from influxdb.influxdb08 import InfluxDBClient
1818
from influxdb.influxdb08.client import session
1919

20+
import sys
21+
if sys.version < '3':
22+
import codecs
23+
24+
def u(x):
25+
return codecs.unicode_escape_decode(x)[0]
26+
else:
27+
def u(x):
28+
return x
29+
2030

2131
def _build_response_object(status_code=200, content=""):
2232
resp = requests.Response()
@@ -186,12 +196,46 @@ def test_write_points_string(self):
186196
)
187197

188198
def test_write_points_batch(self):
189-
with _mocked_session('post', 200, self.dummy_points):
190-
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
191-
assert cli.write_points(
192-
data=self.dummy_points,
193-
batch_size=2
194-
) is True
199+
with requests_mock.Mocker() as m:
200+
m.register_uri(requests_mock.POST,
201+
"http://localhost:8086/db/db/series")
202+
cli = InfluxDBClient('localhost', 8086,
203+
'username', 'password', 'db')
204+
cli.write_points(data=self.dummy_points, batch_size=2)
205+
self.assertEqual(1, m.call_count)
206+
207+
def test_write_points_batch_invalid_size(self):
208+
with requests_mock.Mocker() as m:
209+
m.register_uri(requests_mock.POST,
210+
"http://localhost:8086/db/db/series")
211+
cli = InfluxDBClient('localhost', 8086,
212+
'username', 'password', 'db')
213+
cli.write_points(data=self.dummy_points, batch_size=-2)
214+
self.assertEqual(1, m.call_count)
215+
216+
def test_write_points_batch_multiple_series(self):
217+
dummy_points = [
218+
{"points": [["1", 1, 1.0], ["2", 2, 2.0], ["3", 3, 3.0],
219+
["4", 4, 4.0], ["5", 5, 5.0]],
220+
"name": "foo",
221+
"columns": ["val1", "val2", "val3"]},
222+
{"points": [["1", 1, 1.0], ["2", 2, 2.0], ["3", 3, 3.0],
223+
["4", 4, 4.0], ["5", 5, 5.0], ["6", 6, 6.0],
224+
["7", 7, 7.0], ["8", 8, 8.0]],
225+
"name": "bar",
226+
"columns": ["val1", "val2", "val3"]},
227+
]
228+
expected_last_body = [{'points': [['7', 7, 7.0], ['8', 8, 8.0]],
229+
'name': 'bar',
230+
'columns': ['val1', 'val2', 'val3']}]
231+
with requests_mock.Mocker() as m:
232+
m.register_uri(requests_mock.POST,
233+
"http://localhost:8086/db/db/series")
234+
cli = InfluxDBClient('localhost', 8086,
235+
'username', 'password', 'db')
236+
cli.write_points(data=dummy_points, batch_size=3)
237+
self.assertEqual(m.call_count, 5)
238+
self.assertEqual(expected_last_body, m.request_history[4].json())
195239

196240
def test_write_points_udp(self):
197241
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -332,6 +376,35 @@ def test_query_chunked(self):
332376
[example_object, example_object]
333377
)
334378

379+
def test_query_chunked_unicode(self):
380+
cli = InfluxDBClient(database='db')
381+
example_object = {
382+
'points': [
383+
[1415206212980, 10001, u('unicode-\xcf\x89')],
384+
[1415197271586, 10001, u('more-unicode-\xcf\x90')]
385+
],
386+
'name': 'foo',
387+
'columns': [
388+
'time',
389+
'sequence_number',
390+
'val'
391+
]
392+
}
393+
example_response = \
394+
json.dumps(example_object) + json.dumps(example_object)
395+
396+
with requests_mock.Mocker() as m:
397+
m.register_uri(
398+
requests_mock.GET,
399+
"http://localhost:8086/db/db/series",
400+
text=example_response
401+
)
402+
403+
self.assertListEqual(
404+
cli.query('select * from foo', chunked=True),
405+
[example_object, example_object]
406+
)
407+
335408
@raises(Exception)
336409
def test_query_fail(self):
337410
with _mocked_session('get', 401):

0 commit comments

Comments
 (0)
0