diff --git a/CHANGELOG.md b/CHANGELOG.md index a2861f2f..d30a3183 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/influxdb_client/client/write_api.py b/influxdb_client/client/write_api.py index 3d621cee..6dfd6a6e 100644 --- a/influxdb_client/client/write_api.py +++ b/influxdb_client/client/write_api.py @@ -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): @@ -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 diff --git a/tests/test_WriteApiBatching.py b/tests/test_WriteApiBatching.py index c5674125..5ed7e84b 100644 --- a/tests/test_WriteApiBatching.py +++ b/tests/test_WriteApiBatching.py @@ -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 @@ -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__() @@ -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()