Skip to content

Commit 7e092ad

Browse files
committed
Added unit tests for tag columns. All tests working.
1 parent 30e4e87 commit 7e092ad

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

influxdb/_dataframe_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ def _convert_dataframe_to_lines(self,
232232
field_columns = list(field_columns) if list(field_columns) else []
233233
tag_columns = list(tag_columns) if list(tag_columns) else []
234234

235+
# If field columns but no tag columns, assume rest of columns are tags
236+
if field_columns and (not tag_columns):
237+
tag_columns = list(column_series[~column_series.isin(
238+
field_columns)])
239+
235240
# Assume that all columns not listed as tag columns are field columns
236241
if not field_columns:
237242
field_columns = list(column_series[~column_series.isin(

influxdb/tests/dataframe_client_test.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,135 @@ def test_write_points_from_dataframe_in_batches(self):
6969
cli = DataFrameClient(database='db')
7070
self.assertTrue(cli.write_points(dataframe, "foo", batch_size=1))
7171

72+
def test_write_points_with_tag_columns(self):
73+
now = pd.Timestamp('1970-01-01 00:00+00:00')
74+
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0],
75+
['red', 0, "2", 2, 2.0]],
76+
index=[now, now + timedelta(hours=1)],
77+
columns=["tag_one", "tag_two", "column_one",
78+
"column_two", "column_three"])
79+
expected = (
80+
b"foo,tag_one=blue,tag_two=1 "
81+
b"column_one=\"1\",column_two=1i,column_three=1.0 "
82+
b"0\n"
83+
b"foo,tag_one=red,tag_two=0 "
84+
b"column_one=\"2\",column_two=2i,column_three=2.0 "
85+
b"3600000000000\n"
86+
)
87+
88+
with requests_mock.Mocker() as m:
89+
m.register_uri(requests_mock.POST,
90+
"http://localhost:8086/write",
91+
status_code=204)
92+
93+
cli = DataFrameClient(database='db')
94+
95+
cli.write_points(dataframe, 'foo',
96+
tag_columns=['tag_one', 'tag_two'])
97+
self.assertEqual(m.last_request.body, expected)
98+
99+
cli.write_points(dataframe, 'foo',
100+
tag_columns=['tag_one', 'tag_two'], tags=None)
101+
self.assertEqual(m.last_request.body, expected)
102+
103+
def test_write_points_with_tag_columns_and_global_tags(self):
104+
now = pd.Timestamp('1970-01-01 00:00+00:00')
105+
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0],
106+
['red', 0, "2", 2, 2.0]],
107+
index=[now, now + timedelta(hours=1)],
108+
columns=["tag_one", "tag_two", "column_one",
109+
"column_two", "column_three"])
110+
expected = (
111+
b"foo,tag_one=blue,tag_two=1,global_tag=value "
112+
b"column_one=\"1\",column_two=1i,column_three=1.0 "
113+
b"0\n"
114+
b"foo,tag_one=red,tag_two=0,global_tag=value "
115+
b"column_one=\"2\",column_two=2i,column_three=2.0 "
116+
b"3600000000000\n"
117+
)
118+
119+
with requests_mock.Mocker() as m:
120+
m.register_uri(requests_mock.POST,
121+
"http://localhost:8086/write",
122+
status_code=204)
123+
124+
cli = DataFrameClient(database='db')
125+
126+
cli.write_points(dataframe, 'foo',
127+
tag_columns=['tag_one', 'tag_two'],
128+
tags={'global_tag': 'value'})
129+
self.assertEqual(m.last_request.body, expected)
130+
131+
def test_write_points_with_tag_columns_and_defaults(self):
132+
now = pd.Timestamp('1970-01-01 00:00+00:00')
133+
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0, 'hot'],
134+
['red', 0, "2", 2, 2.0, 'cold']],
135+
index=[now, now + timedelta(hours=1)],
136+
columns=["tag_one", "tag_two", "column_one",
137+
"column_two", "column_three",
138+
"tag_three"])
139+
expected_tags_and_fields = (
140+
b"foo,tag_one=blue "
141+
b"column_one=\"1\",column_two=1i "
142+
b"0\n"
143+
b"foo,tag_one=red "
144+
b"column_one=\"2\",column_two=2i "
145+
b"3600000000000\n"
146+
)
147+
148+
expected_tags_no_fields = (
149+
b"foo,tag_one=blue,tag_two=1 "
150+
b"column_one=\"1\",column_two=1i,column_three=1.0,"
151+
b"tag_three=\"hot\" 0\n"
152+
b"foo,tag_one=red,tag_two=0 "
153+
b"column_one=\"2\",column_two=2i,column_three=2.0,"
154+
b"tag_three=\"cold\" 3600000000000\n"
155+
)
156+
157+
expected_fields_no_tags = (
158+
b"foo,tag_one=blue,tag_two=1,tag_three=hot "
159+
b"column_one=\"1\",column_two=1i,column_three=1.0 "
160+
b"0\n"
161+
b"foo,tag_one=red,tag_two=0,tag_three=cold "
162+
b"column_one=\"2\",column_two=2i,column_three=2.0 "
163+
b"3600000000000\n"
164+
)
165+
166+
expected_no_tags_no_fields = (
167+
b"foo "
168+
b"tag_one=\"blue\",tag_two=1i,column_one=\"1\","
169+
b"column_two=1i,column_three=1.0,tag_three=\"hot\" "
170+
b"0\n"
171+
b"foo "
172+
b"tag_one=\"red\",tag_two=0i,column_one=\"2\","
173+
b"column_two=2i,column_three=2.0,tag_three=\"cold\" "
174+
b"3600000000000\n"
175+
)
176+
177+
with requests_mock.Mocker() as m:
178+
m.register_uri(requests_mock.POST,
179+
"http://localhost:8086/write",
180+
status_code=204)
181+
182+
cli = DataFrameClient(database='db')
183+
184+
cli.write_points(dataframe, 'foo',
185+
field_columns=['column_one', 'column_two'],
186+
tag_columns=['tag_one'])
187+
self.assertEqual(m.last_request.body, expected_tags_and_fields)
188+
189+
cli.write_points(dataframe, 'foo',
190+
tag_columns=['tag_one', 'tag_two'])
191+
self.assertEqual(m.last_request.body, expected_tags_no_fields)
192+
193+
cli.write_points(dataframe, 'foo',
194+
field_columns=['column_one', 'column_two',
195+
'column_three'])
196+
self.assertEqual(m.last_request.body, expected_fields_no_tags)
197+
198+
cli.write_points(dataframe, 'foo')
199+
self.assertEqual(m.last_request.body, expected_no_tags_no_fields)
200+
72201
def test_write_points_from_dataframe_with_numeric_column_names(self):
73202
now = pd.Timestamp('1970-01-01 00:00+00:00')
74203
# df with numeric column names

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy