Skip to content

Commit

Permalink
Fix default tags for write batching, added new test. (#56)
Browse files Browse the repository at this point in the history
* Fix default tags for write batching, added new test
  • Loading branch information
rolincova authored Feb 6, 2020
1 parent b4df6bf commit 0b80ca4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
### CI
1. [#54](https://github.com/influxdata/influxdb-client-python/pull/54): Add Python 3.7 and 3.8 to CI builds

### Bugs
1. [#56](https://github.com/influxdata/influxdb-client-python/pull/56): Fix default tags for write batching, added new test

## 1.3.0 [2020-01-17]

### Features
Expand Down
6 changes: 3 additions & 3 deletions influxdb_client/client/write_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ def write(self, bucket: str, org: str = None,
if org is None:
org = self._influxdb_client.org

if self._write_options.write_type is WriteType.batching:
return self._write_batching(bucket, org, record, write_precision)

if self._point_settings.defaultTags and record:
for key, val in self._point_settings.defaultTags.items():
if isinstance(record, dict):
Expand All @@ -200,6 +197,9 @@ def write(self, bucket: str, org: str = None,
elif isinstance(r, Point):
r.tag(key, val)

if self._write_options.write_type is WriteType.batching:
return self._write_batching(bucket, org, record, write_precision)

final_string = self._serialize(record, write_precision)

_async_req = True if self._write_options.write_type == WriteType.asynchronous else False
Expand Down
35 changes: 32 additions & 3 deletions tests/test_WriteApiBatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import influxdb_client
from influxdb_client import WritePrecision, InfluxDBClient
from influxdb_client.client.write.point import Point
from influxdb_client.client.write_api import WriteOptions, WriteApi
from influxdb_client.client.write_api import WriteOptions, WriteApi, PointSettings
from tests.base_test import BaseTest


Expand All @@ -33,8 +33,8 @@ def setUp(self) -> None:

self.influxdb_client = InfluxDBClient(url=conf.host, token="my-token")

write_options = WriteOptions(batch_size=2, flush_interval=5_000, retry_interval=3_000)
self._write_client = WriteApi(influxdb_client=self.influxdb_client, write_options=write_options)
self.write_options = WriteOptions(batch_size=2, flush_interval=5_000, retry_interval=3_000)
self._write_client = WriteApi(influxdb_client=self.influxdb_client, write_options=self.write_options)

def tearDown(self) -> None:
self._write_client.__del__()
Expand Down Expand Up @@ -336,6 +336,35 @@ def test_del(self):
self.assertEqual(1, len(_requests))
self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=1.0 1", _requests[0].parsed_body)

def test_default_tags(self):
self._write_client.__del__()

self.id_tag = "132-987-655"
self.customer_tag = "California Miner"

self._write_client = WriteApi(influxdb_client=self.influxdb_client,
write_options=WriteOptions(batch_size=1),
point_settings=PointSettings(**{"id": self.id_tag,
"customer": self.customer_tag}))

httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/write", status=204)

_point1 = {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
"time": "2009-11-10T22:00:00Z", "fields": {"water_level": 1.0}}

_point_list = [_point1]

self._write_client.write("my-bucket", "my-org", _point_list)

time.sleep(1)

requests = httpretty.httpretty.latest_requests
self.assertEqual(1, len(requests))

request = str(requests[0].body)
self.assertNotEquals(-1, request.find('customer=California\\\\ Miner'))
self.assertNotEquals(-1, request.find('id=132-987-655'))


if __name__ == '__main__':
unittest.main()

0 comments on commit 0b80ca4

Please sign in to comment.