Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default tags for write batching, added new test. #56

Merged
merged 2 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()