|
5 | 5 | from datetime import timedelta
|
6 | 6 |
|
7 | 7 | from influxdb_client import InfluxDBClient, WriteOptions, WriteApi
|
8 |
| -from influxdb_client.client.write_api import SYNCHRONOUS |
| 8 | +from influxdb_client.client.write.dataframe_serializer import data_frame_to_list_of_points |
| 9 | +from influxdb_client.client.write_api import SYNCHRONOUS, PointSettings |
9 | 10 | from tests.base_test import BaseTest
|
10 | 11 |
|
11 | 12 |
|
@@ -86,3 +87,130 @@ def test_write_num_py(self):
|
86 | 87 | self.assertEqual(result[0].records[1].get_value(), 200.0)
|
87 | 88 |
|
88 | 89 | pass
|
| 90 | + |
| 91 | + def test_write_nan(self): |
| 92 | + from influxdb_client.extras import pd, np |
| 93 | + |
| 94 | + now = pd.Timestamp('2020-04-05 00:00+00:00') |
| 95 | + |
| 96 | + data_frame = pd.DataFrame(data=[[3.1955, np.nan, 20.514305, np.nan], |
| 97 | + [5.7310, np.nan, 23.328710, np.nan], |
| 98 | + [np.nan, 3.138664, np.nan, 20.755026], |
| 99 | + [5.7310, 5.139563, 23.328710, 19.791240]], |
| 100 | + index=[now, now + timedelta(minutes=30), now + timedelta(minutes=60), |
| 101 | + now + timedelta(minutes=90)], |
| 102 | + columns=["actual_kw_price", "forecast_kw_price", "actual_general_use", |
| 103 | + "forecast_general_use"]) |
| 104 | + |
| 105 | + points = data_frame_to_list_of_points(data_frame=data_frame, point_settings=PointSettings(), |
| 106 | + data_frame_measurement_name='measurement') |
| 107 | + |
| 108 | + self.assertEqual(4, len(points)) |
| 109 | + self.assertEqual("measurement actual_kw_price=3.1955,actual_general_use=20.514305 1586044800000000000", |
| 110 | + points[0]) |
| 111 | + self.assertEqual("measurement actual_kw_price=5.731,actual_general_use=23.32871 1586046600000000000", |
| 112 | + points[1]) |
| 113 | + self.assertEqual("measurement forecast_kw_price=3.138664,forecast_general_use=20.755026 1586048400000000000", |
| 114 | + points[2]) |
| 115 | + self.assertEqual("measurement actual_kw_price=5.731,forecast_kw_price=5.139563,actual_general_use=23.32871," |
| 116 | + "forecast_general_use=19.79124 1586050200000000000", |
| 117 | + points[3]) |
| 118 | + |
| 119 | + def test_write_tag_nan(self): |
| 120 | + from influxdb_client.extras import pd, np |
| 121 | + |
| 122 | + now = pd.Timestamp('2020-04-05 00:00+00:00') |
| 123 | + |
| 124 | + data_frame = pd.DataFrame(data=[["", 3.1955, 20.514305], |
| 125 | + ['', 5.7310, 23.328710], |
| 126 | + [np.nan, 5.7310, 23.328710], |
| 127 | + ["tag", 3.138664, 20.755026]], |
| 128 | + index=[now, now + timedelta(minutes=30), |
| 129 | + now + timedelta(minutes=60), now + timedelta(minutes=90)], |
| 130 | + columns=["tag", "actual_kw_price", "forecast_kw_price"]) |
| 131 | + |
| 132 | + write_api = self.client.write_api(write_options=SYNCHRONOUS, point_settings=PointSettings()) |
| 133 | + |
| 134 | + points = data_frame_to_list_of_points(data_frame=data_frame, |
| 135 | + point_settings=PointSettings(), |
| 136 | + data_frame_measurement_name='measurement', |
| 137 | + data_frame_tag_columns={"tag"}) |
| 138 | + |
| 139 | + self.assertEqual(4, len(points)) |
| 140 | + self.assertEqual("measurement actual_kw_price=3.1955,forecast_kw_price=20.514305 1586044800000000000", |
| 141 | + points[0]) |
| 142 | + self.assertEqual("measurement actual_kw_price=5.731,forecast_kw_price=23.32871 1586046600000000000", |
| 143 | + points[1]) |
| 144 | + self.assertEqual("measurement actual_kw_price=5.731,forecast_kw_price=23.32871 1586048400000000000", |
| 145 | + points[2]) |
| 146 | + self.assertEqual("measurement,tag=tag actual_kw_price=3.138664,forecast_kw_price=20.755026 1586050200000000000", |
| 147 | + points[3]) |
| 148 | + |
| 149 | + write_api.__del__() |
| 150 | + |
| 151 | + def test_escaping_measurement(self): |
| 152 | + from influxdb_client.extras import pd, np |
| 153 | + |
| 154 | + now = pd.Timestamp('2020-04-05 00:00+00:00') |
| 155 | + |
| 156 | + data_frame = pd.DataFrame(data=[["coyote_creek", np.int64(100.5)], ["coyote_creek", np.int64(200)]], |
| 157 | + index=[now + timedelta(hours=1), now + timedelta(hours=2)], |
| 158 | + columns=["location", "water_level"]) |
| 159 | + |
| 160 | + points = data_frame_to_list_of_points(data_frame=data_frame, |
| 161 | + point_settings=PointSettings(), |
| 162 | + data_frame_measurement_name='measu rement', |
| 163 | + data_frame_tag_columns={"tag"}) |
| 164 | + |
| 165 | + self.assertEqual(2, len(points)) |
| 166 | + self.assertEqual("measu\\ rement location=\"coyote_creek\",water_level=100i 1586048400000000000", |
| 167 | + points[0]) |
| 168 | + self.assertEqual("measu\\ rement location=\"coyote_creek\",water_level=200i 1586052000000000000", |
| 169 | + points[1]) |
| 170 | + |
| 171 | + points = data_frame_to_list_of_points(data_frame=data_frame, |
| 172 | + point_settings=PointSettings(), |
| 173 | + data_frame_measurement_name='measu\nrement2', |
| 174 | + data_frame_tag_columns={"tag"}) |
| 175 | + |
| 176 | + self.assertEqual(2, len(points)) |
| 177 | + self.assertEqual("measu\\nrement2 location=\"coyote_creek\",water_level=100i 1586048400000000000", |
| 178 | + points[0]) |
| 179 | + self.assertEqual("measu\\nrement2 location=\"coyote_creek\",water_level=200i 1586052000000000000", |
| 180 | + points[1]) |
| 181 | + |
| 182 | + def test_tag_escaping_key_and_value(self): |
| 183 | + from influxdb_client.extras import pd, np |
| 184 | + |
| 185 | + now = pd.Timestamp('2020-04-05 00:00+00:00') |
| 186 | + |
| 187 | + data_frame = pd.DataFrame(data=[["carriage\nreturn", "new\nline", "t\tab", np.int64(2)], ], |
| 188 | + index=[now + timedelta(hours=1), ], |
| 189 | + columns=["carriage\rreturn", "new\nline", "t\tab", "l\ne\rv\tel"]) |
| 190 | + |
| 191 | + points = data_frame_to_list_of_points(data_frame=data_frame, |
| 192 | + point_settings=PointSettings(), |
| 193 | + data_frame_measurement_name='h\n2\ro\t_data', |
| 194 | + data_frame_tag_columns={"new\nline", "carriage\rreturn", "t\tab"}) |
| 195 | + |
| 196 | + self.assertEqual(1, len(points)) |
| 197 | + self.assertEqual( |
| 198 | + "h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab l\\ne\\rv\\tel=2i 1586048400000000000", |
| 199 | + points[0]) |
| 200 | + |
| 201 | + def test_tags_order(self): |
| 202 | + from influxdb_client.extras import pd, np |
| 203 | + |
| 204 | + now = pd.Timestamp('2020-04-05 00:00+00:00') |
| 205 | + |
| 206 | + data_frame = pd.DataFrame(data=[["c", "a", "b", np.int64(2)], ], |
| 207 | + index=[now + timedelta(hours=1), ], |
| 208 | + columns=["c", "a", "b", "level"]) |
| 209 | + |
| 210 | + points = data_frame_to_list_of_points(data_frame=data_frame, |
| 211 | + point_settings=PointSettings(), |
| 212 | + data_frame_measurement_name='h2o', |
| 213 | + data_frame_tag_columns={"c", "a", "b"}) |
| 214 | + |
| 215 | + self.assertEqual(1, len(points)) |
| 216 | + self.assertEqual("h2o,a=a,b=b,c=c level=2i 1586048400000000000", points[0]) |
0 commit comments