11# -*- coding: utf-8 -*-
22
3- import datetime
4- import pytz
53import sys
64if sys .version_info < (2 , 7 ):
75 import unittest2 as unittest
108import warnings
119
1210import mock
11+ from datetime import datetime , timedelta
1312from influxdb import SeriesHelper , InfluxDBClient
1413from requests .exceptions import ConnectionError
1514
@@ -40,17 +39,13 @@ class Meta:
4039
4140 TestSeriesHelper .MySeriesHelper = MySeriesHelper
4241
43- class MySeriesTimeHelper (SeriesHelper ):
44-
45- class Meta :
46- client = TestSeriesHelper .client
47- series_name = 'events.stats.{server_name}'
48- fields = ['time' , 'some_stat' ]
49- tags = ['server_name' , 'other_tag' ]
50- bulk_size = 5
51- autocommit = True
52-
53- TestSeriesHelper .MySeriesTimeHelper = MySeriesTimeHelper
42+ def tearDown (self ):
43+ super (TestSeriesHelper , self ).tearDown ()
44+ TestSeriesHelper .MySeriesHelper ._reset_ ()
45+ self .assertEqual (
46+ TestSeriesHelper .MySeriesHelper ._json_body_ (),
47+ [],
48+ 'Resetting helper did not empty datapoints.' )
5449
5550 def test_auto_commit (self ):
5651 """
@@ -76,24 +71,20 @@ class Meta:
7671 AutoCommitTest (server_name = 'us.east-1' , some_stat = 3443 , other_tag = 'gg' )
7772 self .assertTrue (fake_write_points .called )
7873
79- def testSingleSeriesName (self ):
74+ @mock .patch ('influxdb.helper.SeriesHelper._current_timestamp' )
75+ def testSingleSeriesName (self , current_timestamp ):
8076 """
8177 Tests JSON conversion when there is only one series name.
8278 """
83- dt = datetime .datetime (2016 , 1 , 2 , 3 , 4 , 5 , 678912 )
84- ts1 = dt
85- ts2 = "2016-10-11T01:02:03.123456789-04:00"
86- ts3 = 1234567890123456789
87- ts4 = pytz .timezone ("Europe/Berlin" ).localize (dt )
88-
89- TestSeriesHelper .MySeriesTimeHelper (
90- time = ts1 , server_name = 'us.east-1' , other_tag = 'ello' , some_stat = 159 )
91- TestSeriesHelper .MySeriesTimeHelper (
92- time = ts2 , server_name = 'us.east-1' , other_tag = 'ello' , some_stat = 158 )
93- TestSeriesHelper .MySeriesTimeHelper (
94- time = ts3 , server_name = 'us.east-1' , other_tag = 'ello' , some_stat = 157 )
95- TestSeriesHelper .MySeriesTimeHelper (
96- time = ts4 , server_name = 'us.east-1' , other_tag = 'ello' , some_stat = 156 )
79+ current_timestamp .return_value = current_date = datetime .today ()
80+ TestSeriesHelper .MySeriesHelper (
81+ server_name = 'us.east-1' , other_tag = 'ello' , some_stat = 159 )
82+ TestSeriesHelper .MySeriesHelper (
83+ server_name = 'us.east-1' , other_tag = 'ello' , some_stat = 158 )
84+ TestSeriesHelper .MySeriesHelper (
85+ server_name = 'us.east-1' , other_tag = 'ello' , some_stat = 157 )
86+ TestSeriesHelper .MySeriesHelper (
87+ server_name = 'us.east-1' , other_tag = 'ello' , some_stat = 156 )
9788 expectation = [
9889 {
9990 "measurement" : "events.stats.us.east-1" ,
@@ -104,7 +95,7 @@ def testSingleSeriesName(self):
10495 "fields" : {
10596 "some_stat" : 159
10697 },
107- "time" : "2016-01-02T03:04:05.678912+00:00" ,
98+ "time" : current_date ,
10899 },
109100 {
110101 "measurement" : "events.stats.us.east-1" ,
@@ -115,7 +106,7 @@ def testSingleSeriesName(self):
115106 "fields" : {
116107 "some_stat" : 158
117108 },
118- "time" : "2016-10-11T01:02:03.123456789-04:00" ,
109+ "time" : current_date ,
119110 },
120111 {
121112 "measurement" : "events.stats.us.east-1" ,
@@ -126,7 +117,7 @@ def testSingleSeriesName(self):
126117 "fields" : {
127118 "some_stat" : 157
128119 },
129- "time" : 1234567890123456789 ,
120+ "time" : current_date ,
130121 },
131122 {
132123 "measurement" : "events.stats.us.east-1" ,
@@ -137,25 +128,22 @@ def testSingleSeriesName(self):
137128 "fields" : {
138129 "some_stat" : 156
139130 },
140- "time" : "2016-01-02T03:04:05.678912+01:00" ,
131+ "time" : current_date ,
141132 }
142133 ]
143134
144- rcvd = TestSeriesHelper .MySeriesTimeHelper ._json_body_ ()
135+ rcvd = TestSeriesHelper .MySeriesHelper ._json_body_ ()
145136 self .assertTrue (all ([el in expectation for el in rcvd ]) and
146137 all ([el in rcvd for el in expectation ]),
147138 'Invalid JSON body of time series returned from '
148139 '_json_body_ for one series name: {0}.' .format (rcvd ))
149- TestSeriesHelper .MySeriesTimeHelper ._reset_ ()
150- self .assertEqual (
151- TestSeriesHelper .MySeriesTimeHelper ._json_body_ (),
152- [],
153- 'Resetting helper did not empty datapoints.' )
154140
155- def testSeveralSeriesNames (self ):
156- '''
141+ @mock .patch ('influxdb.helper.SeriesHelper._current_timestamp' )
142+ def testSeveralSeriesNames (self , current_timestamp ):
143+ """
157144 Tests JSON conversion when there are multiple series names.
158- '''
145+ """
146+ current_timestamp .return_value = current_date = datetime .today ()
159147 TestSeriesHelper .MySeriesHelper (
160148 server_name = 'us.east-1' , some_stat = 159 , other_tag = 'ello' )
161149 TestSeriesHelper .MySeriesHelper (
@@ -173,7 +161,8 @@ def testSeveralSeriesNames(self):
173161 'tags' : {
174162 'other_tag' : 'ello' ,
175163 'server_name' : 'lu.lux'
176- }
164+ },
165+ "time" : current_date ,
177166 },
178167 {
179168 'fields' : {
@@ -183,7 +172,8 @@ def testSeveralSeriesNames(self):
183172 'tags' : {
184173 'other_tag' : 'ello' ,
185174 'server_name' : 'uk.london'
186- }
175+ },
176+ "time" : current_date ,
187177 },
188178 {
189179 'fields' : {
@@ -193,7 +183,8 @@ def testSeveralSeriesNames(self):
193183 'tags' : {
194184 'other_tag' : 'ello' ,
195185 'server_name' : 'fr.paris-10'
196- }
186+ },
187+ "time" : current_date ,
197188 },
198189 {
199190 'fields' : {
@@ -203,25 +194,70 @@ def testSeveralSeriesNames(self):
203194 'tags' : {
204195 'other_tag' : 'ello' ,
205196 'server_name' : 'us.east-1'
206- }
197+ },
198+ "time" : current_date ,
207199 }
208200 ]
209201
210202 rcvd = TestSeriesHelper .MySeriesHelper ._json_body_ ()
211- for r in rcvd :
212- self .assertTrue (r .get ('time' ),
213- "No time field in received JSON body." )
214- del (r ["time" ])
215203 self .assertTrue (all ([el in expectation for el in rcvd ]) and
216204 all ([el in rcvd for el in expectation ]),
217205 'Invalid JSON body of time series returned from '
218206 '_json_body_ for several series names: {0}.'
219207 .format (rcvd ))
220- TestSeriesHelper .MySeriesHelper ._reset_ ()
221- self .assertEqual (
222- TestSeriesHelper .MySeriesHelper ._json_body_ (),
223- [],
224- 'Resetting helper did not empty datapoints.' )
208+
209+ @mock .patch ('influxdb.helper.SeriesHelper._current_timestamp' )
210+ def testSeriesWithoutTimeField (self , current_timestamp ):
211+ """
212+ Tests that time is optional on a series without a time field.
213+ """
214+ current_date = datetime .today ()
215+ yesterday = current_date - timedelta (days = 1 )
216+ current_timestamp .return_value = yesterday
217+ TestSeriesHelper .MySeriesHelper (
218+ server_name = 'us.east-1' , other_tag = 'ello' ,
219+ some_stat = 159 , time = current_date
220+ )
221+ TestSeriesHelper .MySeriesHelper (
222+ server_name = 'us.east-1' , other_tag = 'ello' ,
223+ some_stat = 158 ,
224+ )
225+ point1 , point2 = TestSeriesHelper .MySeriesHelper ._json_body_ ()
226+ self .assertTrue ('time' in point1 and 'time' in point2 )
227+ self .assertEqual (point1 ['time' ], current_date )
228+ self .assertEqual (point2 ['time' ], yesterday )
229+
230+ @mock .patch ('influxdb.helper.SeriesHelper._current_timestamp' )
231+ def testSeriesWithTimeField (self , current_timestamp ):
232+ """
233+ Test that time is optional on a series with a time field.
234+ """
235+ current_date = datetime .today ()
236+ yesterday = current_date - timedelta (days = 1 )
237+ current_timestamp .return_value = yesterday
238+
239+ class MyTimeFieldSeriesHelper (SeriesHelper ):
240+
241+ class Meta :
242+ client = TestSeriesHelper .client
243+ series_name = 'events.stats.{server_name}'
244+ fields = ['some_stat' , 'time' ]
245+ tags = ['server_name' , 'other_tag' ]
246+ bulk_size = 5
247+ autocommit = True
248+
249+ MyTimeFieldSeriesHelper (
250+ server_name = 'us.east-1' , other_tag = 'ello' ,
251+ some_stat = 159 , time = current_date
252+ )
253+ MyTimeFieldSeriesHelper (
254+ server_name = 'us.east-1' , other_tag = 'ello' ,
255+ some_stat = 158 ,
256+ )
257+ point1 , point2 = MyTimeFieldSeriesHelper ._json_body_ ()
258+ self .assertTrue ('time' in point1 and 'time' in point2 )
259+ self .assertEqual (point1 ['time' ], current_date )
260+ self .assertEqual (point2 ['time' ], yesterday )
225261
226262 def testInvalidHelpers (self ):
227263 '''
0 commit comments