| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Unit tests for misc module.""" |
| 3 |
|
| 4 |
from datetime import timedelta |
| 5 |
|
| 6 |
import copy |
| 7 |
import json |
| 8 |
import unittest |
| 9 |
import warnings |
| 10 |
|
| 11 |
import requests_mock |
| 12 |
|
| 13 |
from nose.tools import raises |
| 14 |
|
| 15 |
from influxdb.tests import skip_if_pypy, using_pypy |
| 16 |
|
| 17 |
from .client_test import _mocked_session |
| 18 |
|
| 19 |
if not using_pypy: |
| 20 |
import pandas as pd |
| 21 |
from pandas.util.testing import assert_frame_equal |
| 22 |
from influxdb.influxdb08 import DataFrameClient |
| 23 |
|
| 24 |
|
| 25 |
@skip_if_pypy |
| 26 |
class TestDataFrameClient(unittest.TestCase): |
| 27 |
"""Define the DataFramClient test object.""" |
| 28 |
|
| 29 |
def setUp(self): |
| 30 |
"""Set up an instance of TestDataFrameClient object.""" |
| 31 |
# By default, raise exceptions on warnings |
| 32 |
warnings.simplefilter('error', FutureWarning) |
| 33 |
|
| 34 |
def test_write_points_from_dataframe(self): |
| 35 |
"""Test write points from dataframe.""" |
| 36 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 37 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 38 |
index=[now, now + timedelta(hours=1)], |
| 39 |
columns=["column_one", "column_two", |
| 40 |
"column_three"]) |
| 41 |
points = [ |
| 42 |
{ |
| 43 |
"points": [ |
| 44 |
["1", 1, 1.0, 0], |
| 45 |
["2", 2, 2.0, 3600] |
| 46 |
], |
| 47 |
"name": "foo", |
| 48 |
"columns": ["column_one", "column_two", "column_three", "time"] |
| 49 |
} |
| 50 |
] |
| 51 |
|
| 52 |
with requests_mock.Mocker() as m: |
| 53 |
m.register_uri(requests_mock.POST, |
| 54 |
"http://localhost:8086/db/db/series") |
| 55 |
|
| 56 |
cli = DataFrameClient(database='db') |
| 57 |
cli.write_points({"foo": dataframe}) |
| 58 |
|
| 59 |
self.assertListEqual(json.loads(m.last_request.body), points) |
| 60 |
|
| 61 |
def test_write_points_from_dataframe_with_float_nan(self): |
| 62 |
"""Test write points from dataframe with NaN float.""" |
| 63 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 64 |
dataframe = pd.DataFrame(data=[[1, float("NaN"), 1.0], [2, 2, 2.0]], |
| 65 |
index=[now, now + timedelta(hours=1)], |
| 66 |
columns=["column_one", "column_two", |
| 67 |
"column_three"]) |
| 68 |
points = [ |
| 69 |
{ |
| 70 |
"points": [ |
| 71 |
[1, None, 1.0, 0], |
| 72 |
[2, 2, 2.0, 3600] |
| 73 |
], |
| 74 |
"name": "foo", |
| 75 |
"columns": ["column_one", "column_two", "column_three", "time"] |
| 76 |
} |
| 77 |
] |
| 78 |
|
| 79 |
with requests_mock.Mocker() as m: |
| 80 |
m.register_uri(requests_mock.POST, |
| 81 |
"http://localhost:8086/db/db/series") |
| 82 |
|
| 83 |
cli = DataFrameClient(database='db') |
| 84 |
cli.write_points({"foo": dataframe}) |
| 85 |
|
| 86 |
self.assertListEqual(json.loads(m.last_request.body), points) |
| 87 |
|
| 88 |
def test_write_points_from_dataframe_in_batches(self): |
| 89 |
"""Test write points from dataframe in batches.""" |
| 90 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 91 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 92 |
index=[now, now + timedelta(hours=1)], |
| 93 |
columns=["column_one", "column_two", |
| 94 |
"column_three"]) |
| 95 |
with requests_mock.Mocker() as m: |
| 96 |
m.register_uri(requests_mock.POST, |
| 97 |
"http://localhost:8086/db/db/series") |
| 98 |
|
| 99 |
cli = DataFrameClient(database='db') |
| 100 |
self.assertTrue(cli.write_points({"foo": dataframe}, batch_size=1)) |
| 101 |
|
| 102 |
def test_write_points_from_dataframe_with_numeric_column_names(self): |
| 103 |
"""Test write points from dataframe with numeric columns.""" |
| 104 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 105 |
# df with numeric column names |
| 106 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 107 |
index=[now, now + timedelta(hours=1)]) |
| 108 |
points = [ |
| 109 |
{ |
| 110 |
"points": [ |
| 111 |
["1", 1, 1.0, 0], |
| 112 |
["2", 2, 2.0, 3600] |
| 113 |
], |
| 114 |
"name": "foo", |
| 115 |
"columns": ['0', '1', '2', "time"] |
| 116 |
} |
| 117 |
] |
| 118 |
|
| 119 |
with requests_mock.Mocker() as m: |
| 120 |
m.register_uri(requests_mock.POST, |
| 121 |
"http://localhost:8086/db/db/series") |
| 122 |
|
| 123 |
cli = DataFrameClient(database='db') |
| 124 |
cli.write_points({"foo": dataframe}) |
| 125 |
|
| 126 |
self.assertListEqual(json.loads(m.last_request.body), points) |
| 127 |
|
| 128 |
def test_write_points_from_dataframe_with_period_index(self): |
| 129 |
"""Test write points from dataframe with period index.""" |
| 130 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 131 |
index=[pd.Period('1970-01-01'), |
| 132 |
pd.Period('1970-01-02')], |
| 133 |
columns=["column_one", "column_two", |
| 134 |
"column_three"]) |
| 135 |
points = [ |
| 136 |
{ |
| 137 |
"points": [ |
| 138 |
["1", 1, 1.0, 0], |
| 139 |
["2", 2, 2.0, 86400] |
| 140 |
], |
| 141 |
"name": "foo", |
| 142 |
"columns": ["column_one", "column_two", "column_three", "time"] |
| 143 |
} |
| 144 |
] |
| 145 |
|
| 146 |
with requests_mock.Mocker() as m: |
| 147 |
m.register_uri(requests_mock.POST, |
| 148 |
"http://localhost:8086/db/db/series") |
| 149 |
|
| 150 |
cli = DataFrameClient(database='db') |
| 151 |
cli.write_points({"foo": dataframe}) |
| 152 |
|
| 153 |
self.assertListEqual(json.loads(m.last_request.body), points) |
| 154 |
|
| 155 |
def test_write_points_from_dataframe_with_time_precision(self): |
| 156 |
"""Test write points from dataframe with time precision.""" |
| 157 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 158 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 159 |
index=[now, now + timedelta(hours=1)], |
| 160 |
columns=["column_one", "column_two", |
| 161 |
"column_three"]) |
| 162 |
points = [ |
| 163 |
{ |
| 164 |
"points": [ |
| 165 |
["1", 1, 1.0, 0], |
| 166 |
["2", 2, 2.0, 3600] |
| 167 |
], |
| 168 |
"name": "foo", |
| 169 |
"columns": ["column_one", "column_two", "column_three", "time"] |
| 170 |
} |
| 171 |
] |
| 172 |
|
| 173 |
points_ms = copy.deepcopy(points) |
| 174 |
points_ms[0]["points"][1][-1] = 3600 * 1000 |
| 175 |
|
| 176 |
points_us = copy.deepcopy(points) |
| 177 |
points_us[0]["points"][1][-1] = 3600 * 1000000 |
| 178 |
|
| 179 |
with requests_mock.Mocker() as m: |
| 180 |
m.register_uri(requests_mock.POST, |
| 181 |
"http://localhost:8086/db/db/series") |
| 182 |
|
| 183 |
cli = DataFrameClient(database='db') |
| 184 |
|
| 185 |
cli.write_points({"foo": dataframe}, time_precision='s') |
| 186 |
self.assertListEqual(json.loads(m.last_request.body), points) |
| 187 |
|
| 188 |
cli.write_points({"foo": dataframe}, time_precision='m') |
| 189 |
self.assertListEqual(json.loads(m.last_request.body), points_ms) |
| 190 |
|
| 191 |
cli.write_points({"foo": dataframe}, time_precision='u') |
| 192 |
self.assertListEqual(json.loads(m.last_request.body), points_us) |
| 193 |
|
| 194 |
@raises(TypeError) |
| 195 |
def test_write_points_from_dataframe_fails_without_time_index(self): |
| 196 |
"""Test write points from dataframe that fails without time index.""" |
| 197 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 198 |
columns=["column_one", "column_two", |
| 199 |
"column_three"]) |
| 200 |
|
| 201 |
with requests_mock.Mocker() as m: |
| 202 |
m.register_uri(requests_mock.POST, |
| 203 |
"http://localhost:8086/db/db/series") |
| 204 |
|
| 205 |
cli = DataFrameClient(database='db') |
| 206 |
cli.write_points({"foo": dataframe}) |
| 207 |
|
| 208 |
@raises(TypeError) |
| 209 |
def test_write_points_from_dataframe_fails_with_series(self): |
| 210 |
"""Test failed write points from dataframe with series.""" |
| 211 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 212 |
dataframe = pd.Series(data=[1.0, 2.0], |
| 213 |
index=[now, now + timedelta(hours=1)]) |
| 214 |
|
| 215 |
with requests_mock.Mocker() as m: |
| 216 |
m.register_uri(requests_mock.POST, |
| 217 |
"http://localhost:8086/db/db/series") |
| 218 |
|
| 219 |
cli = DataFrameClient(database='db') |
| 220 |
cli.write_points({"foo": dataframe}) |
| 221 |
|
| 222 |
def test_query_into_dataframe(self): |
| 223 |
"""Test query into a dataframe.""" |
| 224 |
data = [ |
| 225 |
{ |
| 226 |
"name": "foo", |
| 227 |
"columns": ["time", "sequence_number", "column_one"], |
| 228 |
"points": [ |
| 229 |
[3600, 16, 2], [3600, 15, 1], |
| 230 |
[0, 14, 2], [0, 13, 1] |
| 231 |
] |
| 232 |
} |
| 233 |
] |
| 234 |
# dataframe sorted ascending by time first, then sequence_number |
| 235 |
dataframe = pd.DataFrame(data=[[13, 1], [14, 2], [15, 1], [16, 2]], |
| 236 |
index=pd.to_datetime([0, 0, |
| 237 |
3600, 3600], |
| 238 |
unit='s', utc=True), |
| 239 |
columns=['sequence_number', 'column_one']) |
| 240 |
with _mocked_session('get', 200, data): |
| 241 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 242 |
result = cli.query('select column_one from foo;') |
| 243 |
assert_frame_equal(dataframe, result) |
| 244 |
|
| 245 |
def test_query_multiple_time_series(self): |
| 246 |
"""Test query for multiple time series.""" |
| 247 |
data = [ |
| 248 |
{ |
| 249 |
"name": "series1", |
| 250 |
"columns": ["time", "mean", "min", "max", "stddev"], |
| 251 |
"points": [[0, 323048, 323048, 323048, 0]] |
| 252 |
}, |
| 253 |
{ |
| 254 |
"name": "series2", |
| 255 |
"columns": ["time", "mean", "min", "max", "stddev"], |
| 256 |
"points": [[0, -2.8233, -2.8503, -2.7832, 0.0173]] |
| 257 |
}, |
| 258 |
{ |
| 259 |
"name": "series3", |
| 260 |
"columns": ["time", "mean", "min", "max", "stddev"], |
| 261 |
"points": [[0, -0.01220, -0.01220, -0.01220, 0]] |
| 262 |
} |
| 263 |
] |
| 264 |
dataframes = { |
| 265 |
'series1': pd.DataFrame(data=[[323048, 323048, 323048, 0]], |
| 266 |
index=pd.to_datetime([0], unit='s', |
| 267 |
utc=True), |
| 268 |
columns=['mean', 'min', 'max', 'stddev']), |
| 269 |
'series2': pd.DataFrame(data=[[-2.8233, -2.8503, -2.7832, 0.0173]], |
| 270 |
index=pd.to_datetime([0], unit='s', |
| 271 |
utc=True), |
| 272 |
columns=['mean', 'min', 'max', 'stddev']), |
| 273 |
'series3': pd.DataFrame(data=[[-0.01220, -0.01220, -0.01220, 0]], |
| 274 |
index=pd.to_datetime([0], unit='s', |
| 275 |
utc=True), |
| 276 |
columns=['mean', 'min', 'max', 'stddev']) |
| 277 |
} |
| 278 |
with _mocked_session('get', 200, data): |
| 279 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 280 |
result = cli.query("""select mean(value), min(value), max(value), |
| 281 |
stddev(value) from series1, series2, series3""") |
| 282 |
self.assertEqual(dataframes.keys(), result.keys()) |
| 283 |
for key in dataframes.keys(): |
| 284 |
assert_frame_equal(dataframes[key], result[key]) |
| 285 |
|
| 286 |
def test_query_with_empty_result(self): |
| 287 |
"""Test query with empty results.""" |
| 288 |
with _mocked_session('get', 200, []): |
| 289 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 290 |
result = cli.query('select column_one from foo;') |
| 291 |
self.assertEqual(result, []) |
| 292 |
|
| 293 |
def test_list_series(self): |
| 294 |
"""Test list of series for dataframe object.""" |
| 295 |
response = [ |
| 296 |
{ |
| 297 |
'columns': ['time', 'name'], |
| 298 |
'name': 'list_series_result', |
| 299 |
'points': [[0, 'seriesA'], [0, 'seriesB']] |
| 300 |
} |
| 301 |
] |
| 302 |
with _mocked_session('get', 200, response): |
| 303 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 304 |
series_list = cli.get_list_series() |
| 305 |
self.assertEqual(series_list, ['seriesA', 'seriesB']) |
| 306 |
|
| 307 |
def test_datetime_to_epoch(self): |
| 308 |
"""Test convert datetime to epoch.""" |
| 309 |
timestamp = pd.Timestamp('2013-01-01 00:00:00.000+00:00') |
| 310 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 311 |
|
| 312 |
self.assertEqual( |
| 313 |
cli._datetime_to_epoch(timestamp), |
| 314 |
1356998400.0 |
| 315 |
) |
| 316 |
self.assertEqual( |
| 317 |
cli._datetime_to_epoch(timestamp, time_precision='s'), |
| 318 |
1356998400.0 |
| 319 |
) |
| 320 |
self.assertEqual( |
| 321 |
cli._datetime_to_epoch(timestamp, time_precision='m'), |
| 322 |
1356998400000.0 |
| 323 |
) |
| 324 |
self.assertEqual( |
| 325 |
cli._datetime_to_epoch(timestamp, time_precision='ms'), |
| 326 |
1356998400000.0 |
| 327 |
) |
| 328 |
self.assertEqual( |
| 329 |
cli._datetime_to_epoch(timestamp, time_precision='u'), |
| 330 |
1356998400000000.0 |
| 331 |
) |