1212import influxdb_client
1313from influxdb_client import WritePrecision , InfluxDBClient
1414from influxdb_client .client .write .point import Point
15- from influxdb_client .client .write_api import WriteOptions , WriteApi
15+ from influxdb_client .client .write_api import WriteOptions , WriteApi , PointSettings
1616from tests .base_test import BaseTest
1717
1818
@@ -33,8 +33,8 @@ def setUp(self) -> None:
3333
3434 self .influxdb_client = InfluxDBClient (url = conf .host , token = "my-token" )
3535
36- write_options = WriteOptions (batch_size = 2 , flush_interval = 5_000 , retry_interval = 3_000 )
37- self ._write_client = WriteApi (influxdb_client = self .influxdb_client , write_options = write_options )
36+ self . write_options = WriteOptions (batch_size = 2 , flush_interval = 5_000 , retry_interval = 3_000 )
37+ self ._write_client = WriteApi (influxdb_client = self .influxdb_client , write_options = self . write_options )
3838
3939 def tearDown (self ) -> None :
4040 self ._write_client .__del__ ()
@@ -336,6 +336,35 @@ def test_del(self):
336336 self .assertEqual (1 , len (_requests ))
337337 self .assertEqual ("h2o_feet,location=coyote_creek level\\ water_level=1.0 1" , _requests [0 ].parsed_body )
338338
339+ def test_default_tags (self ):
340+ self ._write_client .__del__ ()
341+
342+ self .id_tag = "132-987-655"
343+ self .customer_tag = "California Miner"
344+
345+ self ._write_client = WriteApi (influxdb_client = self .influxdb_client ,
346+ write_options = WriteOptions (batch_size = 1 ),
347+ point_settings = PointSettings (** {"id" : self .id_tag ,
348+ "customer" : self .customer_tag }))
349+
350+ httpretty .register_uri (httpretty .POST , uri = "http://localhost/api/v2/write" , status = 204 )
351+
352+ _point1 = {"measurement" : "h2o_feet" , "tags" : {"location" : "coyote_creek" },
353+ "time" : "2009-11-10T22:00:00Z" , "fields" : {"water_level" : 1.0 }}
354+
355+ _point_list = [_point1 ]
356+
357+ self ._write_client .write ("my-bucket" , "my-org" , _point_list )
358+
359+ time .sleep (1 )
360+
361+ requests = httpretty .httpretty .latest_requests
362+ self .assertEqual (1 , len (requests ))
363+
364+ request = str (requests [0 ].body )
365+ self .assertNotEquals (- 1 , request .find ('customer=California\\ \\ Miner' ))
366+ self .assertNotEquals (- 1 , request .find ('id=132-987-655' ))
367+
339368
340369if __name__ == '__main__' :
341370 unittest .main ()
0 commit comments