8000 Add tests for batched writing. · krishnazure/influxdb-python@540befe · GitHub
[go: up one dir, main page]

Skip to content

Commit 540befe

Browse files
committed
Add tests for batched writing.
1 parent c23f3ff commit 540befe

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

tests/influxdb/client_test.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,33 @@ def test_write_points_toplevel_attributes(self):
204204
json.loads(m.last_request.body)
205205
)
206206

207-
@unittest.skip('Not implemented for 0.9')
208207
def test_write_points_batch(self):
209-
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
210-
with _mocked_session(cli, 'post', 200, self.dummy_points):
211-
self.assertTrue(cli.write_points(data=self.dummy_points,
212-
batch_size=2))
208+
dummy_points = [
209+
{"name": "cpu_usage", "tags": {"unit": "percent"},
210+
"timestamp": "2009-11-10T23:00:00Z", "fields": {"value": 12.34}},
211+
{"name": "network", "tags": {"direction": "in"},
212+
"timestamp": "2009-11-10T23:00:00Z", "fields": {"value": 123.00}},
213+
{"name": "network", "tags": {"direction": "out"},
214+
"timestamp": "2009-11-10T23:00:00Z", "fields": {"value": 12.00}}
215+
]
216+
expected_last_body = {"tags": {"host": "server01",
217+
"region": "us-west"},
218+
"database": "db",
219+
"points": [{"name": "network",
220+
"tags": {"direction": "out"},
221+
"timestamp": "2009-11-10T23:00:00Z",
222+
"fields": {"value": 12.00}}]}
223+
with requests_mock.Mocker() as m:
224+
m.register_uri(requests_mock.POST,
225+
"http://localhost:8086/write")
226+
cli = InfluxDBClient(database='db')
227+
cli.write_points(points=dummy_points,
228+
database='db',
229+
tags={"host": "server01",
230+
"region": "us-west"},
231+
batch_size=2)
232+
self.assertEqual(m.call_count, 2)
233+
self.assertEqual(expected_last_body, m.last_request.json())
213234

214235
def test_write_points_udp(self):
215236
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

tests/influxdb/client_test_with_server.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,28 @@ def test_write_multiple_points_different_series(self):
451451
[[{'value': 33, 'time': '2009-11-10T23:01:35Z'}]]
452452
)
453453

454-
@unittest.skip('Not implemented for 0.9')
455454
def test_write_points_batch(self):
456-
self.cli.write_points(
457-
points=dummy_point * 3,
458-
batch_size=2
459-
)
455+
dummy_points = [
456+
{"name": "cpu_usage", "tags": {"unit": "percent"},
457+
"timestamp": "2009-11-10T23:00:00Z", "fields": {"value": 12.34}},
458+
{"name": "network", "tags": {"direction": "in"},
459+
"timestamp": "2009-11-10T23:00:00Z", "fields": {"value": 123.00}},
460+
{"name": "network", "tags": {"direction": "out"},
461+
"timestamp": "2009-11-10T23:00:00Z", "fields": {"value": 12.00}}
462+
]
463+
self.cli.write_points(points=dummy_points,
464+
tags={"host": "server01",
465+
"region": "us-west"},
466+
batch_size=2)
467+
time.sleep(5)
468+
net_in = self.cli.query("SELECT value FROM network "
469+
"WHERE direction='in'").raw['results'][0]
470+
net_out = self.cli.query("SELECT value FROM network "
471+
"WHERE direction='out'").raw['results'][0]
472+
cpu = self.cli.query("SELECT value FROM cpu_usage").raw['results'][0]
473+
self.assertIn(123, net_in['series'][0]['values'][0])
474+
self.assertIn(12, net_out['series'][0]['values'][0])
475+
self.assertIn(12.34, cpu['series'][0]['values'][0])
460476

461477
def test_write_points_with_precision(self):
462478
''' check that points written with an explicit precision have

0 commit comments

Comments
 (0)
0