22import unittest
33from datetime import timedelta
44
5- from influxdb_client import InfluxDBClient , WriteOptions , WriteApi
5+ from influxdb_client import InfluxDBClient , WriteOptions , WriteApi , WritePrecision
66from influxdb_client .client .write .dataframe_serializer import data_frame_to_list_of_points
77from influxdb_client .client .write_api import SYNCHRONOUS , PointSettings
88from tests .base_test import BaseTest
@@ -56,8 +56,8 @@ class DataSerializerTest(unittest.TestCase):
5656 def test_convert_data_frame (self ):
5757 from influxdb_client .extras import pd , np
5858
59- num_rows = 1500000
60- col_data = {
59+ num_rows = 1500000
60+ col_data = {
6161 'time' : np .arange (0 , num_rows , 1 , dtype = int ),
6262 'col1' : np .random .choice (['test_a' , 'test_b' , 'test_c' ], size = (num_rows ,)),
6363 }
@@ -69,8 +69,8 @@ def test_convert_data_frame(self):
6969
7070 start = time .time ()
7171 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' ])
7474
7575 print ("Time elapsed: " , (time .time () - start ))
7676
@@ -291,7 +291,7 @@ def test_with_default_tags(self):
291291 self .assertEqual ("h2o,t1=a2,t2=every,t3=c2 value=2i 1586052000000000000" , points [1 ])
292292
293293 # 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 } ' )
295295
296296 # Check that the default tags won't override actual column data.
297297 # This is arguably incorrect behavior, but it's how it works currently.
@@ -304,7 +304,7 @@ def test_with_default_tags(self):
304304 self .assertEqual ("h2o,t1=a1,t3=c1 value=1i 1586048400000000000" , points [0 ])
305305 self .assertEqual ("h2o,t1=a2,t3=c2 value=2i 1586052000000000000" , points [1 ])
306306
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 } ' )
308308
309309 def test_with_period_index (self ):
310310 from influxdb_client .extras import pd
@@ -333,3 +333,22 @@ def test_write_num_py_floats(self):
333333 self .assertEqual (1 , len (points ))
334334 self .assertEqual ("h2o level=15.5 1586044800000000000" , points [0 ], msg = f'Current type: { np_float_type } ' )
335335
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