From ccd94bb0f60c4054011a50b1347a5e19f4ef2981 Mon Sep 17 00:00:00 2001 From: TH Chen Date: Wed, 6 Dec 2017 13:42:17 -0500 Subject: [PATCH 1/2] Fixed: DataFrameClient should escape measurement names Issue #520 --- influxdb/_dataframe_client.py | 1 + influxdb/tests/dataframe_client_test.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/influxdb/_dataframe_client.py b/influxdb/_dataframe_client.py index d5b41d9f..86b582af 100644 --- a/influxdb/_dataframe_client.py +++ b/influxdb/_dataframe_client.py @@ -376,6 +376,7 @@ def format_line(line): del field_df # Generate line protocol string + measurement = _escape_tag(measurement) points = (measurement + tags + ' ' + fields + ' ' + time).tolist() return points diff --git a/influxdb/tests/dataframe_client_test.py b/influxdb/tests/dataframe_client_test.py index 269261d5..87e425ef 100644 --- a/influxdb/tests/dataframe_client_test.py +++ b/influxdb/tests/dataframe_client_test.py @@ -59,6 +59,28 @@ def test_write_points_from_dataframe(self): cli.write_points(dataframe, 'foo', tags=None) self.assertEqual(m.last_request.body, expected) + def test_dataframe_write_points_with_whitespace_measurement(self): + """write_points should escape white space in measurements""" + now = pd.Timestamp('1970-01-01 00:00+00:00') + dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], + index=[now, now + timedelta(hours=1)], + columns=["column_one", "column_two", + "column_three"]) + expected = ( + b"meas\\ with\\ space " + b"column_one=\"1\",column_two=1i,column_three=1.0 0\n" + b"meas\\ with\\ space " + b"column_one=\"2\",column_two=2i,column_three=2.0 " + b"3600000000000\n" + ) + with requests_mock.Mocker() as m: + m.register_uri(requests_mock.POST, + "http://localhost:8086/write", + status_code=204) + cli = DataFrameClient(database='db') + cli.write_points(dataframe, 'meas with space') + self.assertEqual(m.last_request.body, expected) + def test_write_points_from_dataframe_with_none(self): """Test write points from df in TestDataFrameClient object.""" now = pd.Timestamp('1970-01-01 00:00+00:00') From b11974ed8f5d5fffd47de40b5727522c0ae1ce28 Mon Sep 17 00:00:00 2001 From: TH Chen Date: Mon, 11 Dec 2017 20:11:26 -0500 Subject: [PATCH 2/2] Fix pep257 error --- influxdb/tests/dataframe_client_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb/tests/dataframe_client_test.py b/influxdb/tests/dataframe_client_test.py index 87e425ef..5a717f5c 100644 --- a/influxdb/tests/dataframe_client_test.py +++ b/influxdb/tests/dataframe_client_test.py @@ -60,7 +60,7 @@ def test_write_points_from_dataframe(self): self.assertEqual(m.last_request.body, expected) def test_dataframe_write_points_with_whitespace_measurement(self): - """write_points should escape white space in measurements""" + """write_points should escape white space in measurements.""" now = pd.Timestamp('1970-01-01 00:00+00:00') dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], index=[now, now + timedelta(hours=1)],