2
2
import unittest
3
3
from datetime import timedelta
4
4
5
- from influxdb_client import InfluxDBClient , WriteOptions , WriteApi
5
+ from influxdb_client import InfluxDBClient , WriteOptions , WriteApi , WritePrecision
6
6
from influxdb_client .client .write .dataframe_serializer import data_frame_to_list_of_points
7
7
from influxdb_client .client .write_api import SYNCHRONOUS , PointSettings
8
8
from tests .base_test import BaseTest
@@ -56,8 +56,8 @@ class DataSerializerTest(unittest.TestCase):
56
56
def test_convert_data_frame (self ):
57
57
from influxdb_client .extras import pd , np
58
58
59
- num_rows = 1500000
60
- col_data = {
59
+ num_rows = 1500000
60
+ col_data = {
61
61
'time' : np .arange (0 , num_rows , 1 , dtype = int ),
62
62
'col1' : np .random .choice (['test_a' , 'test_b' , 'test_c' ], size = (num_rows ,)),
63
63
}
@@ -69,8 +69,8 @@ def test_convert_data_frame(self):
69
69
70
70
start = time .time ()
71
71
data_frame_to_list_of_points (data_frame , PointSettings (),
72
- data_frame_measurement_name = 'h2o_feet' ,
73
- data_frame_tag_columns = ['location' ])
72
+ data_frame_measurement_name = 'h2o_feet' ,
73
+ data_frame_tag_columns = ['location' ])
74
74
75
75
print ("Time elapsed: " , (time .time () - start ))
76
76
@@ -291,7 +291,7 @@ def test_with_default_tags(self):
291
291
self .assertEqual ("h2o,t1=a2,t2=every,t3=c2 value=2i 1586052000000000000" , points [1 ])
292
292
293
293
# Check that the data frame hasn't been changed (an earlier version did change it)
294
- self .assertEqual (True , (data_frame == original_data ).all (axis = None ), f'data changed; old:\n { original_data } \n new:\n { data_frame } ' )
294
+ self .assertEqual (True , (data_frame == original_data ).all (axis = None ), f'data changed; old:\n { original_data } \n new:\n { data_frame } ' )
295
295
296
296
# Check that the default tags won't override actual column data.
297
297
# This is arguably incorrect behavior, but it's how it works currently.
@@ -304,7 +304,7 @@ def test_with_default_tags(self):
304
304
self .assertEqual ("h2o,t1=a1,t3=c1 value=1i 1586048400000000000" , points [0 ])
305
305
self .assertEqual ("h2o,t1=a2,t3=c2 value=2i 1586052000000000000" , points [1 ])
306
306
307
- self .assertEqual (True , (data_frame == original_data ).all (axis = None ), f'data changed; old:\n { original_data } \n new:\n { data_frame } ' )
307
+ self .assertEqual (True , (data_frame == original_data ).all (axis = None ), f'data changed; old:\n { original_data } \n new:\n { data_frame } ' )
308
308
309
309
def test_with_period_index (self ):
310
310
from influxdb_client .extras import pd
@@ -333,3 +333,22 @@ def test_write_num_py_floats(self):
333
333
self .assertEqual (1 , len (points ))
334
334
self .assertEqual ("h2o level=15.5 1586044800000000000" , points [0 ], msg = f'Current type: { np_float_type } ' )
335
335
336
+ def test_write_precision (self ):
337
+ from influxdb_client .extras import pd
338
+ now = pd .Timestamp ('2020-04-05 00:00+00:00' )
339
+ precisions = [
340
+ (WritePrecision .NS , 1586044800000000000 ),
341
+ (WritePrecision .US , 1586044800000000 ),
342
+ (WritePrecision .MS , 1586044800000 ),
343
+ (WritePrecision .S , 1586044800 ),
344
+ (None , 1586044800000000000 )
345
+ ]
346
+
347
+ for precision in precisions :
348
+ data_frame = pd .DataFrame ([15 ], index = [now ], columns = ['level' ])
349
+ points = data_frame_to_list_of_points (data_frame = data_frame ,
350
+ data_frame_measurement_name = 'h2o' ,
351
+ point_settings = PointSettings (),
352
+ precision = precision [0 ])
353
+ self .assertEqual (1 , len (points ))
354
+ self .assertEqual (f"h2o level=15i { precision [1 ]} " , points [0 ])
0 commit comments