Skip to content

Commit 0ded779

Browse files
authored
fix: field string values are correctly escaped in DataFrame serialization (#154)
1 parent 0224ad7 commit 0ded779

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
### API
77
1. [#151](https://github.com/influxdata/influxdb-client-python/pull/151): Default port changed from 9999 -> 8086
88

9+
### Bug Fixes
10+
1. [#154](https://github.com/influxdata/influxdb-client-python/pull/154): Fixed escaping string fields in DataFrame serialization
11+
912
## 1.10.0 [2020-08-14]
1013

1114
### Features

influxdb_client/client/write/dataframe_serializer.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from functools import reduce
99
from itertools import chain
1010

11-
from influxdb_client.client.write.point import _ESCAPE_KEY, _ESCAPE_MEASUREMENT
11+
from influxdb_client.client.write.point import _ESCAPE_KEY, _ESCAPE_STRING, _ESCAPE_MEASUREMENT
1212

1313

1414
def _replace(data_frame):
@@ -80,15 +80,16 @@ def data_frame_to_list_of_points(data_frame, point_settings, **kwargs):
8080
elif issubclass(value.type, (np.float, np.bool_)):
8181
fields.append(f"{key_format}={{p[{index + 1}]}}")
8282
else:
83-
fields.append(f"{key_format}=\"{{str(p[{index + 1}]).translate(_ESCAPE_KEY)}}\"")
83+
fields.append(f"{key_format}=\"{{str(p[{index + 1}]).translate(_ESCAPE_STRING)}}\"")
8484

8585
tags.sort(key=lambda x: x['key'])
8686
tags = ','.join(map(lambda y: y['value'], tags))
8787

8888
fmt = ('{measurement_name}', f'{"," if tags else ""}', tags,
8989
' ', ','.join(fields), ' {p[0].value}')
9090
f = eval("lambda p: f'{}'".format(''.join(fmt)),
91-
{'measurement_name': measurement_name, '_ESCAPE_KEY': _ESCAPE_KEY, 'keys': keys})
91+
{'measurement_name': measurement_name, '_ESCAPE_KEY': _ESCAPE_KEY, '_ESCAPE_STRING': _ESCAPE_STRING,
92+
'keys': keys})
9293

9394
for k, v in dict(data_frame.dtypes).items():
9495
if k in data_frame_tag_columns:

tests/test_WriteApiDataFrame.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def test_write_num_py(self):
8888

8989
pass
9090

91+
92+
class DataSerializerTest(unittest.TestCase):
93+
9194
def test_write_nan(self):
9295
from influxdb_client.extras import pd, np
9396

@@ -129,8 +132,6 @@ def test_write_tag_nan(self):
129132
now + timedelta(minutes=60), now + timedelta(minutes=90)],
130133
columns=["tag", "actual_kw_price", "forecast_kw_price"])
131134

132-
write_api = self.client.write_api(write_options=SYNCHRONOUS, point_settings=PointSettings())
133-
134135
points = data_frame_to_list_of_points(data_frame=data_frame,
135136
point_settings=PointSettings(),
136137
data_frame_measurement_name='measurement',
@@ -146,8 +147,6 @@ def test_write_tag_nan(self):
146147
self.assertEqual("measurement,tag=tag actual_kw_price=3.138664,forecast_kw_price=20.755026 1586050200000000000",
147148
points[3])
148149

149-
write_api.__del__()
150-
151150
def test_escaping_measurement(self):
152151
from influxdb_client.extras import pd, np
153152

@@ -214,3 +213,24 @@ def test_tags_order(self):
214213

215214
self.assertEqual(1, len(points))
216215
self.assertEqual("h2o,a=a,b=b,c=c level=2i 1586048400000000000", points[0])
216+
217+
def test_escape_text_value(self):
218+
from influxdb_client.extras import pd, np
219+
220+
now = pd.Timestamp('2020-04-05 00:00+00:00')
221+
an_hour_ago = now - timedelta(hours=1)
222+
223+
test = [{'a': an_hour_ago, 'b': 'hello world', 'c': 1, 'd': 'foo bar'},
224+
{'a': now, 'b': 'goodbye cruel world', 'c': 2, 'd': 'bar foo'}]
225+
226+
data_frame = pd.DataFrame(test)
227+
data_frame = data_frame.set_index('a')
228+
229+
points = data_frame_to_list_of_points(data_frame=data_frame,
230+
point_settings=PointSettings(),
231+
data_frame_measurement_name='test',
232+
data_frame_tag_columns=['d'])
233+
234+
self.assertEqual(2, len(points))
235+
self.assertEqual("test,d=foo\\ bar b=\"hello world\",c=1i 1586041200000000000", points[0])
236+
self.assertEqual("test,d=bar\\ foo b=\"goodbye cruel world\",c=2i 1586044800000000000", points[1])

0 commit comments

Comments
 (0)