|
8 | 8 | import warnings
|
9 | 9 |
|
10 | 10 | import mock
|
11 |
| -from datetime import datetime |
| 11 | +from datetime import datetime, timedelta |
12 | 12 | from unittest.mock import patch
|
13 | 13 | from influxdb import SeriesHelper, InfluxDBClient
|
14 | 14 | from requests.exceptions import ConnectionError
|
@@ -209,6 +209,64 @@ def testSeveralSeriesNames(self, current_timestamp):
|
209 | 209 | [],
|
210 | 210 | 'Resetting helper did not empty datapoints.')
|
211 | 211 |
|
| 212 | + @patch('influxdb.helper.SeriesHelper._current_timestamp') |
| 213 | + def testSeriesWithoutTimeField(self, current_timestamp): |
| 214 | + """ |
| 215 | + Tests that time is optional on a series without a time field. |
| 216 | + """ |
| 217 | + current_date = datetime.today() |
| 218 | + yesterday = current_date - timedelta(days=1) |
| 219 | + current_timestamp.return_value = yesterday |
| 220 | + TestSeriesHelper.MySeriesHelper( |
| 221 | + server_name='us.east-1', other_tag='ello', |
| 222 | + some_stat=159, time=current_date |
| 223 | + ) |
| 224 | + TestSeriesHelper.MySeriesHelper( |
| 225 | + server_name='us.east-1', other_tag='ello', |
| 226 | + some_stat=158, |
| 227 | + ) |
| 228 | + point1, point2 = TestSeriesHelper.MySeriesHelper._json_body_() |
| 229 | + self.assertTrue('time' in point1 and 'time' in point2) |
| 230 | + self.assertEqual(point1['time'], current_date) |
| 231 | + self.assertEqual(point2['time'], yesterday) |
| 232 | + TestSeriesHelper.MySeriesHelper._reset_() |
| 233 | + self.assertEqual( |
| 234 | + TestSeriesHelper.MySeriesHelper._json_body_(), |
| 235 | + [], |
| 236 | + 'Resetting helper did not empty datapoints.') |
| 237 | + |
| 238 | + @patch('influxdb.helper.SeriesHelper._current_timestamp') |
| 239 | + def testSeriesWithTimeField(self, current_timestamp): |
| 240 | + """ |
| 241 | + Test that time is optional on a series with a time field. |
| 242 | + """ |
| 243 | + current_date = datetime.today() |
| 244 | + yesterday = current_date - timedelta(days=1) |
| 245 | + current_timestamp.return_value = yesterday |
| 246 | + |
| 247 | + class MyTimeFieldSeriesHelper(SeriesHelper): |
| 248 | + |
| 249 | + class Meta: |
| 250 | + client = TestSeriesHelper.client |
| 251 | + series_name = 'events.stats.{server_name}' |
| 252 | + fields = ['some_stat', 'time'] |
| 253 | + tags = ['server_name', 'other_tag'] |
| 254 | + bulk_size = 5 |
| 255 | + autocommit = True |
| 256 | + |
| 257 | + MyTimeFieldSeriesHelper( |
| 258 | + server_name='us.east-1', other_tag='ello', |
| 259 | + some_stat=159, time=current_date |
| 260 | + ) |
| 261 | + MyTimeFieldSeriesHelper( |
| 262 | + server_name='us.east-1', other_tag='ello', |
| 263 | + some_stat=158, |
| 264 | + ) |
| 265 | + point1, point2 = MyTimeFieldSeriesHelper._json_body_() |
| 266 | + self.assertTrue('time' in point1 and 'time' in point2) |
| 267 | + self.assertEqual(point1['time'], current_date) |
| 268 | + self.assertEqual(point2['time'], yesterday) |
| 269 | + |
212 | 270 | def testInvalidHelpers(self):
|
213 | 271 | '''
|
214 | 272 | Tests errors in invalid helpers.
|
|
0 commit comments