10
10
from tests .base_test import BaseTest
11
11
12
12
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
-
27
13
class PointTest (BaseTest ):
28
14
29
15
def test_MeasurementEscape (self ):
@@ -124,47 +110,47 @@ def test_TimeSpanFormatting(self):
124
110
point = Point .measurement ("h2o" ) \
125
111
.tag ("location" , "europe" ) \
126
112
.field ("level" , 2 ) \
127
- .time (TimeSpan . FromDays ( 1 ), WritePrecision .NS )
113
+ .time (timedelta ( days = 1 ), WritePrecision .NS )
128
114
129
115
self .assertEqual ("h2o,location=europe level=2i 86400000000000" , point .to_line_protocol ())
130
116
131
117
point = Point .measurement ("h2o" ) \
132
118
.tag ("location" , "europe" ) \
133
119
.field ("level" , 2 ) \
134
- .time (TimeSpan . FromHours ( 356 ), WritePrecision .US )
120
+ .time (timedelta ( hours = 356 ), WritePrecision .US )
135
121
136
122
self .assertEqual ("h2o,location=europe level=2i 1281600000000" , point .to_line_protocol ())
137
123
138
124
point = Point .measurement ("h2o" ) \
139
125
.tag ("location" , "europe" ) \
140
126
.field ("level" , 2 ) \
141
- .time (TimeSpan . FromSeconds ( 156 ), WritePrecision .MS )
127
+ .time (timedelta ( seconds = 156 ), WritePrecision .MS )
142
128
143
129
self .assertEqual ("h2o,location=europe level=2i 156000" , point .to_line_protocol ())
144
130
145
131
point = Point .measurement ("h2o" ) \
146
132
.tag ("location" , "europe" ) \
147
133
.field ("level" , 2 ) \
148
- .time (TimeSpan . FromSeconds ( 123 ), WritePrecision .S )
134
+ .time (timedelta ( seconds = 123 ), WritePrecision .S )
149
135
150
136
self .assertEqual ("h2o,location=europe level=2i 123" , point .to_line_protocol ())
151
137
152
138
def test_DateTimeFormatting (self ):
153
- dateTime = datetime (2015 , 10 , 15 , 8 , 20 , 15 )
139
+ date_time = datetime (2015 , 10 , 15 , 8 , 20 , 15 )
154
140
155
141
point = Point .measurement ("h2o" ) \
156
142
.tag ("location" , "europe" ) \
157
143
.field ("level" , 2 ) \
158
- .time (dateTime , WritePrecision .MS )
144
+ .time (date_time , WritePrecision .MS )
159
145
160
146
self .assertEqual ("h2o,location=europe level=2i 1444897215000" , point .to_line_protocol ())
161
147
162
- dateTime = datetime (2015 , 10 , 15 , 8 , 20 , 15 , 750 , UTC )
148
+ date_time = datetime (2015 , 10 , 15 , 8 , 20 , 15 , 750 , UTC )
163
149
164
150
point = Point .measurement ("h2o" ) \
165
151
.tag ("location" , "europe" ) \
166
152
.field ("level" , False ) \
167
- .time (dateTime , WritePrecision .S )
153
+ .time (date_time , WritePrecision .S )
168
154
169
155
self .assertEqual ("h2o,location=europe level=false 1444897215" , point .to_line_protocol ())
170
156
@@ -173,16 +159,16 @@ def test_DateTimeFormatting(self):
173
159
.field ("level" , True ) \
174
160
.time (datetime .now (UTC ), WritePrecision .S )
175
161
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 )
178
164
179
165
point = Point .measurement ("h2o" ) \
180
166
.tag ("location" , "europe" ) \
181
167
.field ("level" , True ) \
182
168
.time (datetime .now (UTC ), WritePrecision .NS )
183
169
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 )
186
172
187
173
def test_DateTimeUtc (self ):
188
174
date_time = datetime (2015 , 10 , 15 , 8 , 20 , 15 )
@@ -297,12 +283,31 @@ def test_timezone(self):
297
283
eastern = berlin .astimezone (timezone ('US/Eastern' ))
298
284
299
285
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 ())
306
311
307
312
308
313
if __name__ == '__main__' :
0 commit comments