10000 Fix batch writing for influxdb08 client. · krishnazure/influxdb-python@a74224d · GitHub
[go: up one dir, main page]

Skip to content

Commit a74224d

Browse files
committed
Fix batch writing for influxdb08 client.
This fixes issue influxdata#102
1 parent b58aeed commit a74224d

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

influxdb/influxdb08/client.py

Lines changed: 6 additions & 6 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
"""

tests/influxdb/influxdb08/client_test.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,46 @@ def test_write_points_string(self):
186186
)
187187

188188
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
189+
with requests_mock.Mocker() as m:
190+
m.register_uri(requests_mock.POST,
191+
"http://localhost:8086/db/db/series")
192+
cli = InfluxDBClient('localhost', 8086,
193+
'username', 'password', 'db')
194+
cli.write_points(data=self.dummy_points, batch_size=2)
195+
self.assertEqual(1, m.call_count)
196+
197+
def test_write_points_batch_invalid_size(self):
198+
with requests_mock.Mocker() as m:
199+
m.register_uri(requests_mock.POST,
200+
"http://localhost:8086/db/db/series")
201+
cli = InfluxDBClient('localhost', 8086,
202+
'username', 'password', 'db')
203+
cli.write_points(data=self.dummy_points, batch_size=-2)
204+
self.assertEqual(1, m.call_count)
205+
206+
def test_write_points_batch_multiple_series(self):
207+
dummy_points = [
208+
{"points": [["1", 1, 1.0], ["2", 2, 2.0], ["3", 3, 3.0],
209+
["4", 4, 4.0], ["5", 5, 5.0]],
210+
"name": "foo",
211+
"columns": ["val1", "val2", "val3"]},
212+
{"points": [["1", 1, 1.0], ["2", 2, 2.0], ["3", 3, 3.0],
213+
["4", 4, A374 4.0], ["5", 5, 5.0], ["6", 6, 6.0],
214+
["7", 7, 7.0], ["8", 8, 8.0]],
215+
"name": "bar",
216+
"columns": ["val1", "val2", "val3"]},
217+
]
218+
expected_last_body = [{'points': [['7', 7, 7.0], ['8', 8, 8.0]],
219+
'name': 'bar',
220+
'columns': ['val1', 'val2', 'val3']}]
221+
with requests_mock.Mocker() as m:
222+
m.register_uri(requests_mock.POST,
223+
"http://localhost:8086/db/db/series")
224+
cli = InfluxDBClient('localhost', 8086,
225+
'username', 'password', 'db')
226+
cli.write_points(data=dummy_points, batch_size=3)
227+
self.assertEqual(m.call_count, 5)
228+
self.assertEqual(expected_last_body, m.request_history[4].json())
195229

196230
def test_write_points_udp(self):
197231
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

0 commit comments

Comments
 (0)
0