From 99c38ea7c632b0d8a2ed58e5afb93b6912a45d29 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Mon, 14 Sep 2020 07:10:56 +0200 Subject: [PATCH 1/2] fix: field string values are correctly escaped in DataFrame serialization --- .../client/write/dataframe_serializer.py | 7 +++-- tests/test_WriteApiDataFrame.py | 28 ++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/influxdb_client/client/write/dataframe_serializer.py b/influxdb_client/client/write/dataframe_serializer.py index e32bb952..952b5052 100644 --- a/influxdb_client/client/write/dataframe_serializer.py +++ b/influxdb_client/client/write/dataframe_serializer.py @@ -8,7 +8,7 @@ from functools import reduce from itertools import chain -from influxdb_client.client.write.point import _ESCAPE_KEY, _ESCAPE_MEASUREMENT +from influxdb_client.client.write.point import _ESCAPE_KEY, _ESCAPE_STRING, _ESCAPE_MEASUREMENT def _replace(data_frame): @@ -80,7 +80,7 @@ def data_frame_to_list_of_points(data_frame, point_settings, **kwargs): elif issubclass(value.type, (np.float, np.bool_)): fields.append(f"{key_format}={{p[{index + 1}]}}") else: - fields.append(f"{key_format}=\"{{str(p[{index + 1}]).translate(_ESCAPE_KEY)}}\"") + fields.append(f"{key_format}=\"{{str(p[{index + 1}]).translate(_ESCAPE_STRING)}}\"") tags.sort(key=lambda x: x['key']) tags = ','.join(map(lambda y: y['value'], tags)) @@ -88,7 +88,8 @@ def data_frame_to_list_of_points(data_frame, point_settings, **kwargs): fmt = ('{measurement_name}', f'{"," if tags else ""}', tags, ' ', ','.join(fields), ' {p[0].value}') f = eval("lambda p: f'{}'".format(''.join(fmt)), - {'measurement_name': measurement_name, '_ESCAPE_KEY': _ESCAPE_KEY, 'keys': keys}) + {'measurement_name': measurement_name, '_ESCAPE_KEY': _ESCAPE_KEY, '_ESCAPE_STRING': _ESCAPE_STRING, + 'keys': keys}) for k, v in dict(data_frame.dtypes).items(): if k in data_frame_tag_columns: diff --git a/tests/test_WriteApiDataFrame.py b/tests/test_WriteApiDataFrame.py index f5dcf5eb..7b190462 100644 --- a/tests/test_WriteApiDataFrame.py +++ b/tests/test_WriteApiDataFrame.py @@ -88,6 +88,9 @@ def test_write_num_py(self): pass + +class DataSerializerTest(unittest.TestCase): + def test_write_nan(self): from influxdb_client.extras import pd, np @@ -129,8 +132,6 @@ def test_write_tag_nan(self): now + timedelta(minutes=60), now + timedelta(minutes=90)], columns=["tag", "actual_kw_price", "forecast_kw_price"]) - write_api = self.client.write_api(write_options=SYNCHRONOUS, point_settings=PointSettings()) - points = data_frame_to_list_of_points(data_frame=data_frame, point_settings=PointSettings(), data_frame_measurement_name='measurement', @@ -146,8 +147,6 @@ def test_write_tag_nan(self): self.assertEqual("measurement,tag=tag actual_kw_price=3.138664,forecast_kw_price=20.755026 1586050200000000000", points[3]) - write_api.__del__() - def test_escaping_measurement(self): from influxdb_client.extras import pd, np @@ -214,3 +213,24 @@ def test_tags_order(self): self.assertEqual(1, len(points)) self.assertEqual("h2o,a=a,b=b,c=c level=2i 1586048400000000000", points[0]) + + def test_escape_text_value(self): + from influxdb_client.extras import pd, np + + now = pd.Timestamp('2020-04-05 00:00+00:00') + an_hour_ago = now - timedelta(hours=1) + + test = [{'a': an_hour_ago, 'b': 'hello world', 'c': 1, 'd': 'foo bar'}, + {'a': now, 'b': 'goodbye cruel world', 'c': 2, 'd': 'bar foo'}] + + data_frame = pd.DataFrame(test) + data_frame = data_frame.set_index('a') + + points = data_frame_to_list_of_points(data_frame=data_frame, + point_settings=PointSettings(), + data_frame_measurement_name='test', + data_frame_tag_columns=['d']) + + self.assertEqual(2, len(points)) + self.assertEqual("test,d=foo\\ bar b=\"hello world\",c=1i 1586041200000000000", points[0]) + self.assertEqual("test,d=bar\\ foo b=\"goodbye cruel world\",c=2i 1586044800000000000", points[1]) From 20b484c11cd5f3c86bd8e070099976d11cae4880 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Mon, 14 Sep 2020 07:14:41 +0200 Subject: [PATCH 2/2] docs: CHANGELOG.md updated --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bba19fb4..9ab41e79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ ### API 1. [#151](https://github.com/influxdata/influxdb-client-python/pull/151): Default port changed from 9999 -> 8086 +### Bug Fixes +1. [#154](https://github.com/influxdata/influxdb-client-python/pull/154): Fixed escaping string fields in DataFrame serialization + ## 1.10.0 [2020-08-14] ### Features