1010from tests .base_test import BaseTest
1111
1212
13- class TimeSpan (object ):
14- @classmethod
15- def FromDays (cls , param ):
16- return timedelta (days = param )
17-
18- @classmethod
19- def FromHours (cls , param ):
20- return timedelta (hours = param )
21-
22- @classmethod
23- def FromSeconds (cls , param ):
24- return timedelta (seconds = param )
25-
26-
2713class PointTest (BaseTest ):
2814
2915 def test_MeasurementEscape (self ):
@@ -124,47 +110,47 @@ def test_TimeSpanFormatting(self):
124110 point = Point .measurement ("h2o" ) \
125111 .tag ("location" , "europe" ) \
126112 .field ("level" , 2 ) \
127- .time (TimeSpan . FromDays ( 1 ), WritePrecision .NS )
113+ .time (timedelta ( days = 1 ), WritePrecision .NS )
128114
129115 self .assertEqual ("h2o,location=europe level=2i 86400000000000" , point .to_line_protocol ())
130116
131117 point = Point .measurement ("h2o" ) \
132118 .tag ("location" , "europe" ) \
133119 .field ("level" , 2 ) \
134- .time (TimeSpan . FromHours ( 356 ), WritePrecision .US )
120+ .time (timedelta ( hours = 356 ), WritePrecision .US )
135121
136122 self .assertEqual ("h2o,location=europe level=2i 1281600000000" , point .to_line_protocol ())
137123
138124 point = Point .measurement ("h2o" ) \
139125 .tag ("location" , "europe" ) \
140126 .field ("level" , 2 ) \
141- .time (TimeSpan . FromSeconds ( 156 ), WritePrecision .MS )
127+ .time (timedelta ( seconds = 156 ), WritePrecision .MS )
142128
143129 self .assertEqual ("h2o,location=europe level=2i 156000" , point .to_line_protocol ())
144130
145131 point = Point .measurement ("h2o" ) \
146132 .tag ("location" , "europe" ) \
147133 .field ("level" , 2 ) \
148- .time (TimeSpan . FromSeconds ( 123 ), WritePrecision .S )
134+ .time (timedelta ( seconds = 123 ), WritePrecision .S )
149135
150136 self .assertEqual ("h2o,location=europe level=2i 123" , point .to_line_protocol ())
151137
152138 def test_DateTimeFormatting (self ):
153- dateTime = datetime (2015 , 10 , 15 , 8 , 20 , 15 )
139+ date_time = datetime (2015 , 10 , 15 , 8 , 20 , 15 )
154140
155141 point = Point .measurement ("h2o" ) \
156142 .tag ("location" , "europe" ) \
157143 .field ("level" , 2 ) \
158- .time (dateTime , WritePrecision .MS )
144+ .time (date_time , WritePrecision .MS )
159145
160146 self .assertEqual ("h2o,location=europe level=2i 1444897215000" , point .to_line_protocol ())
161147
162- dateTime = datetime (2015 , 10 , 15 , 8 , 20 , 15 , 750 , UTC )
148+ date_time = datetime (2015 , 10 , 15 , 8 , 20 , 15 , 750 , UTC )
163149
164150 point = Point .measurement ("h2o" ) \
165151 .tag ("location" , "europe" ) \
166152 .field ("level" , False ) \
167- .time (dateTime , WritePrecision .S )
153+ .time (date_time , WritePrecision .S )
168154
169155 self .assertEqual ("h2o,location=europe level=false 1444897215" , point .to_line_protocol ())
170156
@@ -173,16 +159,16 @@ def test_DateTimeFormatting(self):
173159 .field ("level" , True ) \
174160 .time (datetime .now (UTC ), WritePrecision .S )
175161
176- lineProtocol = point .to_line_protocol ()
177- self .assertTrue ("." not in lineProtocol )
162+ line_protocol = point .to_line_protocol ()
163+ self .assertTrue ("." not in line_protocol )
178164
179165 point = Point .measurement ("h2o" ) \
180166 .tag ("location" , "europe" ) \
181167 .field ("level" , True ) \
182168 .time (datetime .now (UTC ), WritePrecision .NS )
183169
184- lineProtocol = point .to_line_protocol ()
185- self .assertTrue ("." not in lineProtocol )
170+ line_protocol = point .to_line_protocol ()
171+ self .assertTrue ("." not in line_protocol )
186172
187173 def test_DateTimeUtc (self ):
188174 date_time = datetime (2015 , 10 , 15 , 8 , 20 , 15 )
@@ -297,12 +283,31 @@ def test_timezone(self):
297283 eastern = berlin .astimezone (timezone ('US/Eastern' ))
298284
299285 self .assertEqual ("h2o val=1i 0" , Point .measurement ("h2o" ).field ("val" , 1 ).time (0 ).to_line_protocol ())
300- self .assertEqual ("h2o val=1i 1257894000123456000" , Point .measurement ("h2o" ).field ("val" , 1 ).time ("2009-11-10T23:00:00.123456Z" ).to_line_protocol ())
301- self .assertEqual ("h2o val=1i 1257894000123456000" , Point .measurement ("h2o" ).field ("val" , 1 ).time (utc ).to_line_protocol ())
302- self .assertEqual ("h2o val=1i 1257894000123456000" , Point .measurement ("h2o" ).field ("val" , 1 ).time (dt ).to_line_protocol ())
303- self .assertEqual ("h2o val=1i 1257894000123456000" , Point .measurement ("h2o" ).field ("val" , 1 ).time (1257894000123456000 , write_precision = WritePrecision .NS ).to_line_protocol ())
304- self .assertEqual ("h2o val=1i 1257890400123456000" , Point .measurement ("h2o" ).field ("val" , 1 ).time (eastern ).to_line_protocol ())
305- self .assertEqual ("h2o val=1i 1257890400123456000" , Point .measurement ("h2o" ).field ("val" , 1 ).time (berlin ).to_line_protocol ())
286+ self .assertEqual ("h2o val=1i 1257894000123456000" , Point .measurement ("h2o" ).field ("val" , 1 ).time (
287+ "2009-11-10T23:00:00.123456Z" ).to_line_protocol ())
288+ self .assertEqual ("h2o val=1i 1257894000123456000" ,
289+ Point .measurement ("h2o" ).field ("val" , 1 ).time (utc ).to_line_protocol ())
290+ self .assertEqual ("h2o val=1i 1257894000123456000" ,
291+ Point .measurement ("h2o" ).field ("val" , 1 ).time (dt ).to_line_protocol ())
292+ self .assertEqual ("h2o val=1i 1257894000123456000" ,
293+ Point .measurement ("h2o" ).field ("val" , 1 ).time (1257894000123456000 ,
294+ write_precision = WritePrecision .NS ).to_line_protocol ())
295+ self .assertEqual ("h2o val=1i 1257890400123456000" ,
296+ Point .measurement ("h2o" ).field ("val" , 1 ).time (eastern ).to_line_protocol ())
297+ self .assertEqual ("h2o val=1i 1257890400123456000" ,
298+ Point .measurement ("h2o" ).field ("val" , 1 ).time (berlin ).to_line_protocol ())
299+
300+ def test_from_dict_without_timestamp (self ):
301+ json = {"measurement" : "my-org" , "tags" : {"tag1" : "tag1" , "tag2" : "tag2" }, "fields" : {'field1' : 1 , "field2" : 2 }}
302+
303+ point = Point .from_dict (json )
304+ self .assertEqual ("my-org,tag1=tag1,tag2=tag2 field1=1i,field2=2i" , point .to_line_protocol ())
305+
306+ def test_from_dict_without_tags (self ):
307+ json = {"measurement" : "my-org" , "fields" : {'field1' : 1 , "field2" : 2 }}
308+
309+ point = Point .from_dict (json )
310+ self .assertEqual ("my-org field1=1i,field2=2i" , point .to_line_protocol ())
306311
307312
308313if __name__ == '__main__' :
0 commit comments