| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Unit tests for misc module.""" |
| 3 |
|
| 4 |
from __future__ import absolute_import |
| 5 |
from __future__ import division |
| 6 |
from __future__ import print_function |
| 7 |
from __future__ import unicode_literals |
| 8 |
|
| 9 |
from datetime import timedelta |
| 10 |
|
| 11 |
import json |
| 12 |
import unittest |
| 13 |
import warnings |
| 14 |
import requests_mock |
| 15 |
|
| 16 |
from nose.tools import raises |
| 17 |
from influxdb.tests import skip_if_pypy, using_pypy |
| 18 |
|
| 19 |
from .client_test import _mocked_session |
| 20 |
|
| 21 |
if not using_pypy: |
| 22 |
import pandas as pd |
| 23 |
from pandas.util.testing import assert_frame_equal |
| 24 |
from influxdb import DataFrameClient |
| 25 |
import numpy as np |
| 26 |
|
| 27 |
|
| 28 |
@skip_if_pypy |
| 29 |
class TestDataFrameClient(unittest.TestCase): |
| 30 |
"""Set up a test DataFrameClient object.""" |
| 31 |
|
| 32 |
def setUp(self): |
| 33 |
"""Instantiate a TestDataFrameClient object.""" |
| 34 |
# By default, raise exceptions on warnings |
| 35 |
warnings.simplefilter('error', FutureWarning) |
| 36 |
|
| 37 |
def test_write_points_from_dataframe(self): |
| 38 |
"""Test write points from df in TestDataFrameClient object.""" |
| 39 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 40 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 41 |
index=[now, now + timedelta(hours=1)], |
| 42 |
columns=["column_one", "column_two", |
| 43 |
"column_three"]) |
| 44 |
expected = ( |
| 45 |
b"foo column_one=\"1\",column_two=1i,column_three=1.0 0\n" |
| 46 |
b"foo column_one=\"2\",column_two=2i,column_three=2.0 " |
| 47 |
b"3600000000000\n" |
| 48 |
) |
| 49 |
|
| 50 |
with requests_mock.Mocker() as m: |
| 51 |
m.register_uri(requests_mock.POST, |
| 52 |
"http://localhost:8086/write", |
| 53 |
status_code=204) |
| 54 |
|
| 55 |
cli = DataFrameClient(database='db') |
| 56 |
|
| 57 |
cli.write_points(dataframe, 'foo') |
| 58 |
self.assertEqual(m.last_request.body, expected) |
| 59 |
|
| 60 |
cli.write_points(dataframe, 'foo', tags=None) |
| 61 |
self.assertEqual(m.last_request.body, expected) |
| 62 |
|
| 63 |
def test_dataframe_write_points_with_whitespace_measurement(self): |
| 64 |
"""write_points should escape white space in measurements.""" |
| 65 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 66 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 67 |
index=[now, now + timedelta(hours=1)], |
| 68 |
columns=["column_one", "column_two", |
| 69 |
"column_three"]) |
| 70 |
expected = ( |
| 71 |
b"meas\\ with\\ space " |
| 72 |
b"column_one=\"1\",column_two=1i,column_three=1.0 0\n" |
| 73 |
b"meas\\ with\\ space " |
| 74 |
b"column_one=\"2\",column_two=2i,column_three=2.0 " |
| 75 |
b"3600000000000\n" |
| 76 |
) |
| 77 |
with requests_mock.Mocker() as m: |
| 78 |
m.register_uri(requests_mock.POST, |
| 79 |
"http://localhost:8086/write", |
| 80 |
status_code=204) |
| 81 |
cli = DataFrameClient(database='db') |
| 82 |
cli.write_points(dataframe, 'meas with space') |
| 83 |
self.assertEqual(m.last_request.body, expected) |
| 84 |
|
| 85 |
def test_dataframe_write_points_with_whitespace_in_column_names(self): |
| 86 |
"""write_points should escape white space in column names.""" |
| 87 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 88 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 89 |
index=[now, now + timedelta(hours=1)], |
| 90 |
columns=["column one", "column two", |
| 91 |
"column three"]) |
| 92 |
expected = ( |
| 93 |
b"foo column\\ one=\"1\",column\\ two=1i,column\\ three=1.0 0\n" |
| 94 |
b"foo column\\ one=\"2\",column\\ two=2i,column\\ three=2.0 " |
| 95 |
b"3600000000000\n" |
| 96 |
) |
| 97 |
with requests_mock.Mocker() as m: |
| 98 |
m.register_uri(requests_mock.POST, |
| 99 |
"http://localhost:8086/write", |
| 100 |
status_code=204) |
| 101 |
cli = DataFrameClient(database='db') |
| 102 |
cli.write_points(dataframe, 'foo') |
| 103 |
self.assertEqual(m.last_request.body, expected) |
| 104 |
|
| 105 |
def test_write_points_from_dataframe_with_none(self): |
| 106 |
"""Test write points from df in TestDataFrameClient object.""" |
| 107 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 108 |
dataframe = pd.DataFrame(data=[["1", None, 1.0], ["2", 2.0, 2.0]], |
| 109 |
index=[now, now + timedelta(hours=1)], |
| 110 |
columns=["column_one", "column_two", |
| 111 |
"column_three"]) |
| 112 |
expected = ( |
| 113 |
b"foo column_one=\"1\",column_three=1.0 0\n" |
| 114 |
b"foo column_one=\"2\",column_two=2.0,column_three=2.0 " |
| 115 |
b"3600000000000\n" |
| 116 |
) |
| 117 |
|
| 118 |
with requests_mock.Mocker() as m: |
| 119 |
m.register_uri(requests_mock.POST, |
| 120 |
"http://localhost:8086/write", |
| 121 |
status_code=204) |
| 122 |
|
| 123 |
cli = DataFrameClient(database='db') |
| 124 |
|
| 125 |
cli.write_points(dataframe, 'foo') |
| 126 |
self.assertEqual(m.last_request.body, expected) |
| 127 |
|
| 128 |
cli.write_points(dataframe, 'foo', tags=None) |
| 129 |
self.assertEqual(m.last_request.body, expected) |
| 130 |
|
| 131 |
def test_write_points_from_dataframe_with_line_of_none(self): |
| 132 |
"""Test write points from df in TestDataFrameClient object.""" |
| 133 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 134 |
dataframe = pd.DataFrame(data=[[None, None, None], ["2", 2.0, 2.0]], |
| 135 |
index=[now, now + timedelta(hours=1)], |
| 136 |
columns=["column_one", "column_two", |
| 137 |
"column_three"]) |
| 138 |
expected = ( |
| 139 |
b"foo column_one=\"2\",column_two=2.0,column_three=2.0 " |
| 140 |
b"3600000000000\n" |
| 141 |
) |
| 142 |
|
| 143 |
with requests_mock.Mocker() as m: |
| 144 |
m.register_uri(requests_mock.POST, |
| 145 |
"http://localhost:8086/write", |
| 146 |
status_code=204) |
| 147 |
|
| 148 |
cli = DataFrameClient(database='db') |
| 149 |
|
| 150 |
cli.write_points(dataframe, 'foo') |
| 151 |
self.assertEqual(m.last_request.body, expected) |
| 152 |
|
| 153 |
cli.write_points(dataframe, 'foo', tags=None) |
| 154 |
self.assertEqual(m.last_request.body, expected) |
| 155 |
|
| 156 |
def test_write_points_from_dataframe_with_all_none(self): |
| 157 |
"""Test write points from df in TestDataFrameClient object.""" |
| 158 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 159 |
dataframe = pd.DataFrame(data=[[None, None, None], [None, None, None]], |
| 160 |
index=[now, now + timedelta(hours=1)], |
| 161 |
columns=["column_one", "column_two", |
| 162 |
"column_three"]) |
| 163 |
expected = ( |
| 164 |
b"\n" |
| 165 |
) |
| 166 |
|
| 167 |
with requests_mock.Mocker() as m: |
| 168 |
m.register_uri(requests_mock.POST, |
| 169 |
"http://localhost:8086/write", |
| 170 |
status_code=204) |
| 171 |
|
| 172 |
cli = DataFrameClient(database='db') |
| 173 |
|
| 174 |
cli.write_points(dataframe, 'foo') |
| 175 |
self.assertEqual(m.last_request.body, expected) |
| 176 |
|
| 177 |
cli.write_points(dataframe, 'foo', tags=None) |
| 178 |
self.assertEqual(m.last_request.body, expected) |
| 179 |
|
| 180 |
def test_write_points_from_dataframe_in_batches(self): |
| 181 |
"""Test write points in batch from df in TestDataFrameClient object.""" |
| 182 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 183 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 184 |
index=[now, now + timedelta(hours=1)], |
| 185 |
columns=["column_one", "column_two", |
| 186 |
"column_three"]) |
| 187 |
with requests_mock.Mocker() as m: |
| 188 |
m.register_uri(requests_mock.POST, |
| 189 |
"http://localhost:8086/write", |
| 190 |
status_code=204) |
| 191 |
|
| 192 |
cli = DataFrameClient(database='db') |
| 193 |
self.assertTrue(cli.write_points(dataframe, "foo", batch_size=1)) |
| 194 |
|
| 195 |
def test_write_points_from_dataframe_with_tag_columns(self): |
| 196 |
"""Test write points from df w/tag in TestDataFrameClient object.""" |
| 197 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 198 |
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0], |
| 199 |
['red', 0, "2", 2, 2.0]], |
| 200 |
index=[now, now + timedelta(hours=1)], |
| 201 |
columns=["tag_one", "tag_two", "column_one", |
| 202 |
"column_two", "column_three"]) |
| 203 |
expected = ( |
| 204 |
b"foo,tag_one=blue,tag_two=1 " |
| 205 |
b"column_one=\"1\",column_two=1i,column_three=1.0 " |
| 206 |
b"0\n" |
| 207 |
b"foo,tag_one=red,tag_two=0 " |
| 208 |
b"column_one=\"2\",column_two=2i,column_three=2.0 " |
| 209 |
b"3600000000000\n" |
| 210 |
) |
| 211 |
|
| 212 |
with requests_mock.Mocker() as m: |
| 213 |
m.register_uri(requests_mock.POST, |
| 214 |
"http://localhost:8086/write", |
| 215 |
status_code=204) |
| 216 |
|
| 217 |
cli = DataFrameClient(database='db') |
| 218 |
|
| 219 |
cli.write_points(dataframe, 'foo', |
| 220 |
tag_columns=['tag_one', 'tag_two']) |
| 221 |
self.assertEqual(m.last_request.body, expected) |
| 222 |
|
| 223 |
cli.write_points(dataframe, 'foo', |
| 224 |
tag_columns=['tag_one', 'tag_two'], tags=None) |
| 225 |
self.assertEqual(m.last_request.body, expected) |
| 226 |
|
| 227 |
def test_write_points_from_dataframe_with_tag_cols_and_global_tags(self): |
| 228 |
"""Test write points from df w/tag + cols in TestDataFrameClient.""" |
| 229 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 230 |
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0], |
| 231 |
['red', 0, "2", 2, 2.0]], |
| 232 |
index=[now, now + timedelta(hours=1)], |
| 233 |
columns=["tag_one", "tag_two", "column_one", |
| 234 |
"column_two", "column_three"]) |
| 235 |
expected = ( |
| 236 |
b"foo,global_tag=value,tag_one=blue,tag_two=1 " |
| 237 |
b"column_one=\"1\",column_two=1i,column_three=1.0 " |
| 238 |
b"0\n" |
| 239 |
b"foo,global_tag=value,tag_one=red,tag_two=0 " |
| 240 |
b"column_one=\"2\",column_two=2i,column_three=2.0 " |
| 241 |
b"3600000000000\n" |
| 242 |
) |
| 243 |
|
| 244 |
with requests_mock.Mocker() as m: |
| 245 |
m.register_uri(requests_mock.POST, |
| 246 |
"http://localhost:8086/write", |
| 247 |
status_code=204) |
| 248 |
|
| 249 |
cli = DataFrameClient(database='db') |
| 250 |
|
| 251 |
cli.write_points(dataframe, 'foo', |
| 252 |
tag_columns=['tag_one', 'tag_two'], |
| 253 |
tags={'global_tag': 'value'}) |
| 254 |
self.assertEqual(m.last_request.body, expected) |
| 255 |
|
| 256 |
def test_write_points_from_dataframe_with_tag_cols_and_defaults(self): |
| 257 |
"""Test default write points from df w/tag in TestDataFrameClient.""" |
| 258 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 259 |
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0, 'hot'], |
| 260 |
['red', 0, "2", 2, 2.0, 'cold']], |
| 261 |
index=[now, now + timedelta(hours=1)], |
| 262 |
columns=["tag_one", "tag_two", "column_one", |
| 263 |
"column_two", "column_three", |
| 264 |
"tag_three"]) |
| 265 |
expected_tags_and_fields = ( |
| 266 |
b"foo,tag_one=blue " |
| 267 |
b"column_one=\"1\",column_two=1i " |
| 268 |
b"0\n" |
| 269 |
b"foo,tag_one=red " |
| 270 |
b"column_one=\"2\",column_two=2i " |
| 271 |
b"3600000000000\n" |
| 272 |
) |
| 273 |
|
| 274 |
expected_tags_no_fields = ( |
| 275 |
b"foo,tag_one=blue,tag_two=1 " |
| 276 |
b"column_one=\"1\",column_two=1i,column_three=1.0," |
| 277 |
b"tag_three=\"hot\" 0\n" |
| 278 |
b"foo,tag_one=red,tag_two=0 " |
| 279 |
b"column_one=\"2\",column_two=2i,column_three=2.0," |
| 280 |
b"tag_three=\"cold\" 3600000000000\n" |
| 281 |
) |
| 282 |
|
| 283 |
expected_fields_no_tags = ( |
| 284 |
b"foo,tag_one=blue,tag_three=hot,tag_two=1 " |
| 285 |
b"column_one=\"1\",column_two=1i,column_three=1.0 " |
| 286 |
b"0\n" |
| 287 |
b"foo,tag_one=red,tag_three=cold,tag_two=0 " |
| 288 |
b"column_one=\"2\",column_two=2i,column_three=2.0 " |
| 289 |
b"3600000000000\n" |
| 290 |
) |
| 291 |
|
| 292 |
expected_no_tags_no_fields = ( |
| 293 |
b"foo " |
| 294 |
b"tag_one=\"blue\",tag_two=1i,column_one=\"1\"," |
| 295 |
b"column_two=1i,column_three=1.0,tag_three=\"hot\" " |
| 296 |
b"0\n" |
| 297 |
b"foo " |
| 298 |
b"tag_one=\"red\",tag_two=0i,column_one=\"2\"," |
| 299 |
b"column_two=2i,column_three=2.0,tag_three=\"cold\" " |
| 300 |
b"3600000000000\n" |
| 301 |
) |
| 302 |
|
| 303 |
with requests_mock.Mocker() as m: |
| 304 |
m.register_uri(requests_mock.POST, |
| 305 |
"http://localhost:8086/write", |
| 306 |
status_code=204) |
| 307 |
|
| 308 |
cli = DataFrameClient(database='db') |
| 309 |
|
| 310 |
cli.write_points(dataframe, 'foo', |
| 311 |
field_columns=['column_one', 'column_two'], |
| 312 |
tag_columns=['tag_one']) |
| 313 |
self.assertEqual(m.last_request.body, expected_tags_and_fields) |
| 314 |
|
| 315 |
cli.write_points(dataframe, 'foo', |
| 316 |
tag_columns=['tag_one', 'tag_two']) |
| 317 |
self.assertEqual(m.last_request.body, expected_tags_no_fields) |
| 318 |
|
| 319 |
cli.write_points(dataframe, 'foo', |
| 320 |
field_columns=['column_one', 'column_two', |
| 321 |
'column_three']) |
| 322 |
self.assertEqual(m.last_request.body, expected_fields_no_tags) |
| 323 |
|
| 324 |
cli.write_points(dataframe, 'foo') |
| 325 |
self.assertEqual(m.last_request.body, expected_no_tags_no_fields) |
| 326 |
|
| 327 |
def test_write_points_from_dataframe_with_tag_escaped(self): |
| 328 |
"""Test write points from df w/escaped tag in TestDataFrameClient.""" |
| 329 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 330 |
dataframe = pd.DataFrame( |
| 331 |
data=[ |
| 332 |
['blue orange', "1", 1, 'hot=cold'], # space, equal |
| 333 |
['red,green', "2", 2, r'cold\fire'], # comma, backslash |
| 334 |
['some', "2", 2, ''], # skip empty |
| 335 |
['some', "2", 2, None], # skip None |
| 336 |
['', "2", 2, None], # all tags empty |
| 337 |
], |
| 338 |
index=pd.period_range(now, freq='H', periods=5), |
| 339 |
columns=["tag_one", "column_one", "column_two", "tag_three"] |
| 340 |
) |
| 341 |
|
| 342 |
expected_escaped_tags = ( |
| 343 |
b"foo,tag_one=blue\\ orange,tag_three=hot\\=cold " |
| 344 |
b"column_one=\"1\",column_two=1i " |
| 345 |
b"0\n" |
| 346 |
b"foo,tag_one=red\\,green,tag_three=cold\\\\fire " |
| 347 |
b"column_one=\"2\",column_two=2i " |
| 348 |
b"3600000000000\n" |
| 349 |
b"foo,tag_one=some " |
| 350 |
b"column_one=\"2\",column_two=2i " |
| 351 |
b"7200000000000\n" |
| 352 |
b"foo,tag_one=some " |
| 353 |
b"column_one=\"2\",column_two=2i " |
| 354 |
b"10800000000000\n" |
| 355 |
b"foo " |
| 356 |
b"column_one=\"2\",column_two=2i " |
| 357 |
b"14400000000000\n" |
| 358 |
) |
| 359 |
|
| 360 |
with requests_mock.Mocker() as m: |
| 361 |
m.register_uri(requests_mock.POST, |
| 362 |
"http://localhost:8086/write", |
| 363 |
status_code=204) |
| 364 |
cli = DataFrameClient(database='db') |
| 365 |
cli.write_points(dataframe, 'foo', |
| 366 |
field_columns=['column_one', 'column_two'], |
| 367 |
tag_columns=['tag_one', 'tag_three']) |
| 368 |
self.assertEqual(m.last_request.body, expected_escaped_tags) |
| 369 |
|
| 370 |
def test_write_points_from_dataframe_with_numeric_column_names(self): |
| 371 |
"""Test write points from df with numeric cols.""" |
| 372 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 373 |
# df with numeric column names |
| 374 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 375 |
index=[now, now + timedelta(hours=1)]) |
| 376 |
|
| 377 |
expected = ( |
| 378 |
b'foo,hello=there 0=\"1\",1=1i,2=1.0 0\n' |
| 379 |
b'foo,hello=there 0=\"2\",1=2i,2=2.0 3600000000000\n' |
| 380 |
) |
| 381 |
|
| 382 |
with requests_mock.Mocker() as m: |
| 383 |
m.register_uri(requests_mock.POST, |
| 384 |
"http://localhost:8086/write", |
| 385 |
status_code=204) |
| 386 |
|
| 387 |
cli = DataFrameClient(database='db') |
| 388 |
cli.write_points(dataframe, "foo", {"hello": "there"}) |
| 389 |
|
| 390 |
self.assertEqual(m.last_request.body, expected) |
| 391 |
|
| 392 |
def test_write_points_from_dataframe_with_leading_none_column(self): |
| 393 |
"""write_points detect erroneous leading comma for null first field.""" |
| 394 |
dataframe = pd.DataFrame( |
| 395 |
dict( |
| 396 |
first=[1, None, None, 8, 9], |
| 397 |
second=[2, None, None, None, 10], |
| 398 |
third=[3, 4.1, None, None, 11], |
| 399 |
first_tag=["one", None, None, "eight", None], |
| 400 |
second_tag=["two", None, None, None, None], |
| 401 |
third_tag=["three", "four", None, None, None], |
| 402 |
comment=[ |
| 403 |
"All columns filled", |
| 404 |
"First two of three empty", |
| 405 |
"All empty", |
| 406 |
"Last two of three empty", |
| 407 |
"Empty tags with values", |
| 408 |
] |
| 409 |
), |
| 410 |
index=pd.date_range( |
| 411 |
start=pd.to_datetime('2018-01-01'), |
| 412 |
freq='1D', |
| 413 |
periods=5, |
| 414 |
) |
| 415 |
) |
| 416 |
expected = ( |
| 417 |
b'foo,first_tag=one,second_tag=two,third_tag=three' |
| 418 |
b' comment="All columns filled",first=1.0,second=2.0,third=3.0' |
| 419 |
b' 1514764800000000000\n' |
| 420 |
b'foo,third_tag=four' |
| 421 |
b' comment="First two of three empty",third=4.1' |
| 422 |
b' 1514851200000000000\n' |
| 423 |
b'foo comment="All empty" 1514937600000000000\n' |
| 424 |
b'foo,first_tag=eight' |
| 425 |
b' comment="Last two of three empty",first=8.0' |
| 426 |
b' 1515024000000000000\n' |
| 427 |
b'foo' |
| 428 |
b' comment="Empty tags with values",first=9.0,second=10.0' |
| 429 |
b',third=11.0' |
| 430 |
b' 1515110400000000000\n' |
| 431 |
) |
| 432 |
|
| 433 |
with requests_mock.Mocker() as m: |
| 434 |
m.register_uri(requests_mock.POST, |
| 435 |
"http://localhost:8086/write", |
| 436 |
status_code=204) |
| 437 |
|
| 438 |
cli = DataFrameClient(database='db') |
| 439 |
|
| 440 |
colnames = [ |
| 441 |
"first_tag", |
| 442 |
"second_tag", |
| 443 |
"third_tag", |
| 444 |
"comment", |
| 445 |
"first", |
| 446 |
"second", |
| 447 |
"third" |
| 448 |
] |
| 449 |
cli.write_points(dataframe.loc[:, colnames], 'foo', |
| 450 |
tag_columns=[ |
| 451 |
"first_tag", |
| 452 |
"second_tag", |
| 453 |
"third_tag"]) |
| 454 |
|
| 455 |
self.assertEqual(m.last_request.body, expected) |
| 456 |
|
| 457 |
def test_write_points_from_dataframe_with_numeric_precision(self): |
| 458 |
"""Test write points from df with numeric precision.""" |
| 459 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 460 |
# df with numeric column names |
| 461 |
dataframe = pd.DataFrame(data=[["1", 1, 1.1111111111111], |
| 462 |
["2", 2, 2.2222222222222]], |
| 463 |
index=[now, now + timedelta(hours=1)]) |
| 464 |
|
| 465 |
if np.lib.NumpyVersion(np.__version__) <= '1.13.3': |
| 466 |
expected_default_precision = ( |
| 467 |
b'foo,hello=there 0=\"1\",1=1i,2=1.11111111111 0\n' |
| 468 |
b'foo,hello=there 0=\"2\",1=2i,2=2.22222222222 3600000000000\n' |
| 469 |
) |
| 470 |
else: |
| 471 |
expected_default_precision = ( |
| 472 |
b'foo,hello=there 0=\"1\",1=1i,2=1.1111111111111 0\n' |
| 473 |
b'foo,hello=there 0=\"2\",1=2i,2=2.2222222222222 3600000000000\n' # noqa E501 line too long |
| 474 |
) |
| 475 |
|
| 476 |
expected_specified_precision = ( |
| 477 |
b'foo,hello=there 0=\"1\",1=1i,2=1.1111 0\n' |
| 478 |
b'foo,hello=there 0=\"2\",1=2i,2=2.2222 3600000000000\n' |
| 479 |
) |
| 480 |
|
| 481 |
expected_full_precision = ( |
| 482 |
b'foo,hello=there 0=\"1\",1=1i,2=1.1111111111111 0\n' |
| 483 |
b'foo,hello=there 0=\"2\",1=2i,2=2.2222222222222 3600000000000\n' |
| 484 |
) |
| 485 |
|
| 486 |
with requests_mock.Mocker() as m: |
| 487 |
m.register_uri(requests_mock.POST, |
| 488 |
"http://localhost:8086/write", |
| 489 |
status_code=204) |
| 490 |
|
| 491 |
cli = DataFrameClient(database='db') |
| 492 |
cli.write_points(dataframe, "foo", {"hello": "there"}) |
| 493 |
|
| 494 |
print(expected_default_precision) |
| 495 |
print(m.last_request.body) |
| 496 |
|
| 497 |
self.assertEqual(m.last_request.body, expected_default_precision) |
| 498 |
|
| 499 |
cli = DataFrameClient(database='db') |
| 500 |
cli.write_points(dataframe, "foo", {"hello": "there"}, |
| 501 |
numeric_precision=4) |
| 502 |
|
| 503 |
self.assertEqual(m.last_request.body, expected_specified_precision) |
| 504 |
|
| 505 |
cli = DataFrameClient(database='db') |
| 506 |
cli.write_points(dataframe, "foo", {"hello": "there"}, |
| 507 |
numeric_precision='full') |
| 508 |
|
| 509 |
self.assertEqual(m.last_request.body, expected_full_precision) |
| 510 |
|
| 511 |
def test_write_points_from_dataframe_with_period_index(self): |
| 512 |
"""Test write points from df with period index.""" |
| 513 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 514 |
index=[pd.Period('1970-01-01'), |
| 515 |
pd.Period('1970-01-02')], |
| 516 |
columns=["column_one", "column_two", |
| 517 |
"column_three"]) |
| 518 |
|
| 519 |
expected = ( |
| 520 |
b"foo column_one=\"1\",column_two=1i,column_three=1.0 0\n" |
| 521 |
b"foo column_one=\"2\",column_two=2i,column_three=2.0 " |
| 522 |
b"86400000000000\n" |
| 523 |
) |
| 524 |
|
| 525 |
with requests_mock.Mocker() as m: |
| 526 |
m.register_uri(requests_mock.POST, |
| 527 |
"http://localhost:8086/write", |
| 528 |
status_code=204) |
| 529 |
|
| 530 |
cli = DataFrameClient(database='db') |
| 531 |
cli.write_points(dataframe, "foo") |
| 532 |
|
| 533 |
self.assertEqual(m.last_request.body, expected) |
| 534 |
|
| 535 |
def test_write_points_from_dataframe_with_time_precision(self): |
| 536 |
"""Test write points from df with time precision.""" |
| 537 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 538 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 539 |
index=[now, now + timedelta(hours=1)], |
| 540 |
columns=["column_one", "column_two", |
| 541 |
"column_three"]) |
| 542 |
|
| 543 |
with requests_mock.Mocker() as m: |
| 544 |
m.register_uri(requests_mock.POST, |
| 545 |
"http://localhost:8086/write", |
| 546 |
status_code=204) |
| 547 |
|
| 548 |
cli = DataFrameClient(database='db') |
| 549 |
measurement = "foo" |
| 550 |
|
| 551 |
cli.write_points(dataframe, measurement, time_precision='h') |
| 552 |
self.assertEqual(m.last_request.qs['precision'], ['h']) |
| 553 |
self.assertEqual( |
| 554 |
b'foo column_one="1",column_two=1i,column_three=1.0 0\nfoo ' |
| 555 |
b'column_one="2",column_two=2i,column_three=2.0 1\n', |
| 556 |
m.last_request.body, |
| 557 |
) |
| 558 |
|
| 559 |
cli.write_points(dataframe, measurement, time_precision='m') |
| 560 |
self.assertEqual(m.last_request.qs['precision'], ['m']) |
| 561 |
self.assertEqual( |
| 562 |
b'foo column_one="1",column_two=1i,column_three=1.0 0\nfoo ' |
| 563 |
b'column_one="2",column_two=2i,column_three=2.0 60\n', |
| 564 |
m.last_request.body, |
| 565 |
) |
| 566 |
|
| 567 |
cli.write_points(dataframe, measurement, time_precision='s') |
| 568 |
self.assertEqual(m.last_request.qs['precision'], ['s']) |
| 569 |
self.assertEqual( |
| 570 |
b'foo column_one="1",column_two=1i,column_three=1.0 0\nfoo ' |
| 571 |
b'column_one="2",column_two=2i,column_three=2.0 3600\n', |
| 572 |
m.last_request.body, |
| 573 |
) |
| 574 |
|
| 575 |
cli.write_points(dataframe, measurement, time_precision='ms') |
| 576 |
self.assertEqual(m.last_request.qs['precision'], ['ms']) |
| 577 |
self.assertEqual( |
| 578 |
b'foo column_one="1",column_two=1i,column_three=1.0 0\nfoo ' |
| 579 |
b'column_one="2",column_two=2i,column_three=2.0 3600000\n', |
| 580 |
m.last_request.body, |
| 581 |
) |
| 582 |
|
| 583 |
cli.write_points(dataframe, measurement, time_precision='u') |
| 584 |
self.assertEqual(m.last_request.qs['precision'], ['u']) |
| 585 |
self.assertEqual( |
| 586 |
b'foo column_one="1",column_two=1i,column_three=1.0 0\nfoo ' |
| 587 |
b'column_one="2",column_two=2i,column_three=2.0 3600000000\n', |
| 588 |
m.last_request.body, |
| 589 |
) |
| 590 |
|
| 591 |
cli.write_points(dataframe, measurement, time_precision='n') |
| 592 |
self.assertEqual(m.last_request.qs['precision'], ['n']) |
| 593 |
self.assertEqual( |
| 594 |
b'foo column_one="1",column_two=1i,column_three=1.0 0\n' |
| 595 |
b'foo column_one="2",column_two=2i,column_three=2.0 ' |
| 596 |
b'3600000000000\n', |
| 597 |
m.last_request.body, |
| 598 |
) |
| 599 |
|
| 600 |
@raises(TypeError) |
| 601 |
def test_write_points_from_dataframe_fails_without_time_index(self): |
| 602 |
"""Test failed write points from df without time index.""" |
| 603 |
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]], |
| 604 |
columns=["column_one", "column_two", |
| 605 |
"column_three"]) |
| 606 |
|
| 607 |
with requests_mock.Mocker() as m: |
| 608 |
m.register_uri(requests_mock.POST, |
| 609 |
"http://localhost:8086/db/db/series", |
| 610 |
status_code=204) |
| 611 |
|
| 612 |
cli = DataFrameClient(database='db') |
| 613 |
cli.write_points(dataframe, "foo") |
| 614 |
|
| 615 |
@raises(TypeError) |
| 616 |
def test_write_points_from_dataframe_fails_with_series(self): |
| 617 |
"""Test failed write points from df with series.""" |
| 618 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 619 |
dataframe = pd.Series(data=[1.0, 2.0], |
| 620 |
index=[now, now + timedelta(hours=1)]) |
| 621 |
|
| 622 |
with requests_mock.Mocker() as m: |
| 623 |
m.register_uri(requests_mock.POST, |
| 624 |
"http://localhost:8086/db/db/series", |
| 625 |
status_code=204) |
| 626 |
|
| 627 |
cli = DataFrameClient(database='db') |
| 628 |
cli.write_points(dataframe, "foo") |
| 629 |
|
| 630 |
def test_create_database(self): |
| 631 |
"""Test create database for TestInfluxDBClient object.""" |
| 632 |
cli = DataFrameClient(database='db') |
| 633 |
with requests_mock.Mocker() as m: |
| 634 |
m.register_uri( |
| 635 |
requests_mock.POST, |
| 636 |
"http://localhost:8086/query", |
| 637 |
text='{"results":[{}]}' |
| 638 |
) |
| 639 |
cli.create_database('new_db') |
| 640 |
self.assertEqual( |
| 641 |
m.last_request.qs['q'][0], |
| 642 |
'create database "new_db"' |
| 643 |
) |
| 644 |
|
| 645 |
def test_create_numeric_named_database(self): |
| 646 |
"""Test create db w/numeric name for TestInfluxDBClient object.""" |
| 647 |
cli = DataFrameClient(database='db') |
| 648 |
with requests_mock.Mocker() as m: |
| 649 |
m.register_uri( |
| 650 |
requests_mock.POST, |
| 651 |
"http://localhost:8086/query", |
| 652 |
text='{"results":[{}]}' |
| 653 |
) |
| 654 |
cli.create_database('123') |
| 655 |
self.assertEqual( |
| 656 |
m.last_request.qs['q'][0], |
| 657 |
'create database "123"' |
| 658 |
) |
| 659 |
|
| 660 |
@raises(Exception) |
| 661 |
def test_create_database_fails(self): |
| 662 |
"""Test create database fail for TestInfluxDBClient object.""" |
| 663 |
cli = DataFrameClient(database='db') |
| 664 |
with _mocked_session(cli, 'post', 401): |
| 665 |
cli.create_database('new_db') |
| 666 |
|
| 667 |
def test_drop_database(self): |
| 668 |
"""Test drop database for TestInfluxDBClient object.""" |
| 669 |
cli = DataFrameClient(database='db') |
| 670 |
with requests_mock.Mocker() as m: |
| 671 |
m.register_uri( |
| 672 |
requests_mock.POST, |
| 673 |
"http://localhost:8086/query", |
| 674 |
text='{"results":[{}]}' |
| 675 |
) |
| 676 |
cli.drop_database('new_db') |
| 677 |
self.assertEqual( |
| 678 |
m.last_request.qs['q'][0], |
| 679 |
'drop database "new_db"' |
| 680 |
) |
| 681 |
|
| 682 |
def test_drop_measurement(self): |
| 683 |
"""Test drop measurement for TestInfluxDBClient object.""" |
| 684 |
cli = DataFrameClient(database='db') |
| 685 |
with requests_mock.Mocker() as m: |
| 686 |
m.register_uri( |
| 687 |
requests_mock.POST, |
| 688 |
"http://localhost:8086/query", |
| 689 |
text='{"results":[{}]}' |
| 690 |
) |
| 691 |
cli.drop_measurement('new_measurement') |
| 692 |
self.assertEqual( |
| 693 |
m.last_request.qs['q'][0], |
| 694 |
'drop measurement "new_measurement"' |
| 695 |
) |
| 696 |
|
| 697 |
def test_drop_numeric_named_database(self): |
| 698 |
"""Test drop numeric db for TestInfluxDBClient object.""" |
| 699 |
cli = DataFrameClient(database='db') |
| 700 |
with requests_mock.Mocker() as m: |
| 701 |
m.register_uri( |
| 702 |
requests_mock.POST, |
| 703 |
"http://localhost:8086/query", |
| 704 |
text='{"results":[{}]}' |
| 705 |
) |
| 706 |
cli.drop_database('123') |
| 707 |
self.assertEqual( |
| 708 |
m.last_request.qs['q'][0], |
| 709 |
'drop database "123"' |
| 710 |
) |
| 711 |
|
| 712 |
@raises(Exception) |
| 713 |
def test_get_list_database_fails(self): |
| 714 |
"""Test get list of dbs fail for TestInfluxDBClient object.""" |
| 715 |
cli = DataFrameClient('host', 8086, 'username', 'password') |
| 716 |
with _mocked_session(cli, 'get', 401): |
| 717 |
cli.get_list_database() |
| 718 |
|
| 719 |
def test_get_list_measurements(self): |
| 720 |
"""Test get list of measurements for TestInfluxDBClient object.""" |
| 721 |
cli = DataFrameClient(database='db') |
| 722 |
data = { |
| 723 |
"results": [{ |
| 724 |
"series": [ |
| 725 |
{"name": "measurements", |
| 726 |
"columns": ["name"], |
| 727 |
"values": [["cpu"], ["disk"] |
| 728 |
]}]} |
| 729 |
] |
| 730 |
} |
| 731 |
|
| 732 |
with _mocked_session(cli, 'get', 200, json.dumps(data)): |
| 733 |
self.assertListEqual( |
| 734 |
cli.get_list_measurements(), |
| 735 |
[{'name': 'cpu'}, {'name': 'disk'}] |
| 736 |
) |
| 737 |
|
| 738 |
def test_create_retention_policy_default(self): |
| 739 |
"""Test create default ret policy for TestInfluxDBClient object.""" |
| 740 |
cli = DataFrameClient(database='db') |
| 741 |
example_response = '{"results":[{}]}' |
| 742 |
|
| 743 |
with requests_mock.Mocker() as m: |
| 744 |
m.register_uri( |
| 745 |
requests_mock.POST, |
| 746 |
"http://localhost:8086/query", |
| 747 |
text=example_response |
| 748 |
) |
| 749 |
cli.create_retention_policy( |
| 750 |
'somename', '1d', 4, default=True, database='db' |
| 751 |
) |
| 752 |
|
| 753 |
self.assertEqual( |
| 754 |
m.last_request.qs['q'][0], |
| 755 |
'create retention policy "somename" on ' |
| 756 |
'"db" duration 1d replication 4 shard duration 0s default' |
| 757 |
) |
| 758 |
|
| 759 |
def test_create_retention_policy(self): |
| 760 |
"""Test create retention policy for TestInfluxDBClient object.""" |
| 761 |
cli = DataFrameClient(database='db') |
| 762 |
example_response = '{"results":[{}]}' |
| 763 |
|
| 764 |
with requests_mock.Mocker() as m: |
| 765 |
m.register_uri( |
| 766 |
requests_mock.POST, |
| 767 |
"http://localhost:8086/query", |
| 768 |
text=example_response |
| 769 |
) |
| 770 |
cli.create_retention_policy( |
| 771 |
'somename', '1d', 4, database='db' |
| 772 |
) |
| 773 |
|
| 774 |
self.assertEqual( |
| 775 |
m.last_request.qs['q'][0], |
| 776 |
'create retention policy "somename" on ' |
| 777 |
'"db" duration 1d replication 4 shard duration 0s' |
| 778 |
) |
| 779 |
|
| 780 |
def test_alter_retention_policy(self): |
| 781 |
"""Test alter retention policy for TestInfluxDBClient object.""" |
| 782 |
cli = DataFrameClient(database='db') |
| 783 |
example_response = '{"results":[{}]}' |
| 784 |
|
| 785 |
with requests_mock.Mocker() as m: |
| 786 |
m.register_uri( |
| 787 |
requests_mock.POST, |
| 788 |
"http://localhost:8086/query", |
| 789 |
text=example_response |
| 790 |
) |
| 791 |
# Test alter duration |
| 792 |
cli.alter_retention_policy('somename', 'db', |
| 793 |
duration='4d') |
| 794 |
self.assertEqual( |
| 795 |
m.last_request.qs['q'][0], |
| 796 |
'alter retention policy "somename" on "db" duration 4d' |
| 797 |
) |
| 798 |
# Test alter replication |
| 799 |
cli.alter_retention_policy('somename', 'db', |
| 800 |
replication=4) |
| 801 |
self.assertEqual( |
| 802 |
m.last_request.qs['q'][0], |
| 803 |
'alter retention policy "somename" on "db" replication 4' |
| 804 |
) |
| 805 |
|
| 806 |
# Test alter shard duration |
| 807 |
cli.alter_retention_policy('somename', 'db', |
| 808 |
shard_duration='1h') |
| 809 |
self.assertEqual( |
| 810 |
m.last_request.qs['q'][0], |
| 811 |
'alter retention policy "somename" on "db" shard duration 1h' |
| 812 |
) |
| 813 |
|
| 814 |
# Test alter default |
| 815 |
cli.alter_retention_policy('somename', 'db', |
| 816 |
default=True) |
| 817 |
self.assertEqual( |
| 818 |
m.last_request.qs['q'][0], |
| 819 |
'alter retention policy "somename" on "db" default' |
| 820 |
) |
| 821 |
|
| 822 |
@raises(Exception) |
| 823 |
def test_alter_retention_policy_invalid(self): |
| 824 |
"""Test invalid alter ret policy for TestInfluxDBClient object.""" |
| 825 |
cli = DataFrameClient('host', 8086, 'username', 'password') |
| 826 |
with _mocked_session(cli, 'get', 400): |
| 827 |
cli.alter_retention_policy('somename', 'db') |
| 828 |
|
| 829 |
def test_drop_retention_policy(self): |
| 830 |
"""Test drop retention policy for TestInfluxDBClient object.""" |
| 831 |
cli = DataFrameClient(database='db') |
| 832 |
example_response = '{"results":[{}]}' |
| 833 |
|
| 834 |
with requests_mock.Mocker() as m: |
| 835 |
m.register_uri( |
| 836 |
requests_mock.POST, |
| 837 |
"http://localhost:8086/query", |
| 838 |
text=example_response |
| 839 |
) |
| 840 |
cli.drop_retention_policy('somename', 'db') |
| 841 |
self.assertEqual( |
| 842 |
m.last_request.qs['q'][0], |
| 843 |
'drop retention policy "somename" on "db"' |
| 844 |
) |
| 845 |
|
| 846 |
@raises(Exception) |
| 847 |
def test_drop_retention_policy_fails(self): |
| 848 |
"""Test failed drop ret policy for TestInfluxDBClient object.""" |
| 849 |
cli = DataFrameClient('host', 8086, 'username', 'password') |
| 850 |
with _mocked_session(cli, 'delete', 401): |
| 851 |
cli.drop_retention_policy('default', 'db') |
| 852 |
|
| 853 |
def test_get_list_retention_policies(self): |
| 854 |
"""Test get retention policies for TestInfluxDBClient object.""" |
| 855 |
cli = DataFrameClient(database='db') |
| 856 |
example_response = \ |
| 857 |
'{"results": [{"series": [{"values": [["fsfdsdf", "24h0m0s", 2]],'\ |
| 858 |
' "columns": ["name", "duration", "replicaN"]}]}]}' |
| 859 |
|
| 860 |
with requests_mock.Mocker() as m: |
| 861 |
m.register_uri( |
| 862 |
requests_mock.GET, |
| 863 |
"http://localhost:8086/query", |
| 864 |
text=example_response |
| 865 |
) |
| 866 |
self.assertListEqual( |
| 867 |
cli.get_list_retention_policies("db"), |
| 868 |
[{'duration': '24h0m0s', |
| 869 |
'name': 'fsfdsdf', 'replicaN': 2}] |
| 870 |
) |
| 871 |
|
| 872 |
def test_query_into_dataframe(self): |
| 873 |
"""Test query into df for TestDataFrameClient object.""" |
| 874 |
data = { |
| 875 |
"results": [{ |
| 876 |
"series": [ |
| 877 |
{"measurement": "network", |
| 878 |
"tags": {"direction": ""}, |
| 879 |
"columns": ["time", "value"], |
| 880 |
"values": [["2009-11-10T23:00:00Z", 23422]] |
| 881 |
}, |
| 882 |
{"measurement": "network", |
| 883 |
"tags": {"direction": "in"}, |
| 884 |
"columns": ["time", "value"], |
| 885 |
"values": [["2009-11-10T23:00:00Z", 23422], |
| 886 |
["2009-11-10T23:00:00Z", 23422], |
| 887 |
["2009-11-10T23:00:00Z", 23422]] |
| 888 |
} |
| 889 |
] |
| 890 |
}] |
| 891 |
} |
| 892 |
|
| 893 |
pd1 = pd.DataFrame( |
| 894 |
[[23422]], columns=['value'], |
| 895 |
index=pd.to_datetime(["2009-11-10T23:00:00Z"])) |
| 896 |
if pd1.index.tzinfo is None: |
| 897 |
pd1.index = pd1.index.tz_localize('UTC') |
| 898 |
pd2 = pd.DataFrame( |
| 899 |
[[23422], [23422], [23422]], columns=['value'], |
| 900 |
index=pd.to_datetime(["2009-11-10T23:00:00Z", |
| 901 |
"2009-11-10T23:00:00Z", |
| 902 |
"2009-11-10T23:00:00Z"])) |
| 903 |
if pd2.index.tzinfo is None: |
| 904 |
pd2.index = pd2.index.tz_localize('UTC') |
| 905 |
expected = { |
| 906 |
('network', (('direction', ''),)): pd1, |
| 907 |
('network', (('direction', 'in'),)): pd2 |
| 908 |
} |
| 909 |
|
| 910 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 911 |
with _mocked_session(cli, 'GET', 200, data): |
| 912 |
result = cli.query('select value from network group by direction;') |
| 913 |
for k in expected: |
| 914 |
assert_frame_equal(expected[k], result[k]) |
| 915 |
|
| 916 |
def test_multiquery_into_dataframe(self): |
| 917 |
"""Test multiquery into df for TestDataFrameClient object.""" |
| 918 |
data = { |
| 919 |
"results": [ |
| 920 |
{ |
| 921 |
"series": [ |
| 922 |
{ |
| 923 |
"name": "cpu_load_short", |
| 924 |
"columns": ["time", "value"], |
| 925 |
"values": [ |
| 926 |
["2015-01-29T21:55:43.702900257Z", 0.55], |
| 927 |
["2015-01-29T21:55:43.702900257Z", 23422], |
| 928 |
["2015-06-11T20:46:02Z", 0.64] |
| 929 |
] |
| 930 |
} |
| 931 |
] |
| 932 |
}, { |
| 933 |
"series": [ |
| 934 |
{ |
| 935 |
"name": "cpu_load_short", |
| 936 |
"columns": ["time", "count"], |
| 937 |
"values": [ |
| 938 |
["1970-01-01T00:00:00Z", 3] |
| 939 |
] |
| 940 |
} |
| 941 |
] |
| 942 |
} |
| 943 |
] |
| 944 |
} |
| 945 |
|
| 946 |
pd1 = pd.DataFrame( |
| 947 |
[[0.55], [23422.0], [0.64]], columns=['value'], |
| 948 |
index=pd.to_datetime([ |
| 949 |
"2015-01-29 21:55:43.702900257+0000", |
| 950 |
"2015-01-29 21:55:43.702900257+0000", |
| 951 |
"2015-06-11 20:46:02+0000"])) |
| 952 |
if pd1.index.tzinfo is None: |
| 953 |
pd1.index = pd1.index.tz_localize('UTC') |
| 954 |
pd2 = pd.DataFrame( |
| 955 |
[[3]], columns=['count'], |
| 956 |
index=pd.to_datetime(["1970-01-01 00:00:00+00:00"])) |
| 957 |
if pd2.index.tzinfo is None: |
| 958 |
pd2.index = pd2.index.tz_localize('UTC') |
| 959 |
expected = [{'cpu_load_short': pd1}, {'cpu_load_short': pd2}] |
| 960 |
|
| 961 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 962 |
iql = "SELECT value FROM cpu_load_short WHERE region=$region;"\ |
| 963 |
"SELECT count(value) FROM cpu_load_short WHERE region=$region" |
| 964 |
bind_params = {'region': 'us-west'} |
| 965 |
with _mocked_session(cli, 'GET', 200, data): |
| 966 |
result = cli.query(iql, bind_params=bind_params) |
| 967 |
for r, e in zip(result, expected): |
| 968 |
for k in e: |
| 969 |
assert_frame_equal(e[k], r[k]) |
| 970 |
|
| 971 |
def test_multiquery_into_dataframe_dropna(self): |
| 972 |
"""Test multiquery into df for TestDataFrameClient object.""" |
| 973 |
data = { |
| 974 |
"results": [ |
| 975 |
{ |
| 976 |
"series": [ |
| 977 |
{ |
| 978 |
"name": "cpu_load_short", |
| 979 |
"columns": ["time", "value", "value2", "value3"], |
| 980 |
"values": [ |
| 981 |
["2015-01-29T21:55:43.702900257Z", |
| 982 |
0.55, 0.254, np.NaN], |
| 983 |
["2015-01-29T21:55:43.702900257Z", |
| 984 |
23422, 122878, np.NaN], |
| 985 |
["2015-06-11T20:46:02Z", |
| 986 |
0.64, 0.5434, np.NaN] |
| 987 |
] |
| 988 |
} |
| 989 |
] |
| 990 |
}, { |
| 991 |
"series": [ |
| 992 |
{ |
| 993 |
"name": "cpu_load_short", |
| 994 |
"columns": ["time", "count"], |
| 995 |
"values": [ |
| 996 |
["1970-01-01T00:00:00Z", 3] |
| 997 |
] |
| 998 |
} |
| 999 |
] |
| 1000 |
} |
| 1001 |
] |
| 1002 |
} |
| 1003 |
|
| 1004 |
pd1 = pd.DataFrame( |
| 1005 |
[[0.55, 0.254, np.NaN], |
| 1006 |
[23422.0, 122878, np.NaN], |
| 1007 |
[0.64, 0.5434, np.NaN]], |
| 1008 |
columns=['value', 'value2', 'value3'], |
| 1009 |
index=pd.to_datetime([ |
| 1010 |
"2015-01-29 21:55:43.702900257+0000", |
| 1011 |
"2015-01-29 21:55:43.702900257+0000", |
| 1012 |
"2015-06-11 20:46:02+0000"])) |
| 1013 |
|
| 1014 |
if pd1.index.tzinfo is None: |
| 1015 |
pd1.index = pd1.index.tz_localize('UTC') |
| 1016 |
|
| 1017 |
pd1_dropna = pd.DataFrame( |
| 1018 |
[[0.55, 0.254], [23422.0, 122878], [0.64, 0.5434]], |
| 1019 |
columns=['value', 'value2'], |
| 1020 |
index=pd.to_datetime([ |
| 1021 |
"2015-01-29 21:55:43.702900257+0000", |
| 1022 |
"2015-01-29 21:55:43.702900257+0000", |
| 1023 |
"2015-06-11 20:46:02+0000"])) |
| 1024 |
|
| 1025 |
if pd1_dropna.index.tzinfo is None: |
| 1026 |
pd1_dropna.index = pd1_dropna.index.tz_localize('UTC') |
| 1027 |
|
| 1028 |
pd2 = pd.DataFrame( |
| 1029 |
[[3]], columns=['count'], |
| 1030 |
index=pd.to_datetime(["1970-01-01 00:00:00+00:00"])) |
| 1031 |
|
| 1032 |
if pd2.index.tzinfo is None: |
| 1033 |
pd2.index = pd2.index.tz_localize('UTC') |
| 1034 |
|
| 1035 |
expected_dropna_true = [ |
| 1036 |
{'cpu_load_short': pd1_dropna}, |
| 1037 |
{'cpu_load_short': pd2}] |
| 1038 |
expected_dropna_false = [ |
| 1039 |
{'cpu_load_short': pd1}, |
| 1040 |
{'cpu_load_short': pd2}] |
| 1041 |
|
| 1042 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 1043 |
iql = "SELECT value FROM cpu_load_short WHERE region=$region;" \ |
| 1044 |
"SELECT count(value) FROM cpu_load_short WHERE region=$region" |
| 1045 |
bind_params = {'region': 'us-west'} |
| 1046 |
|
| 1047 |
for dropna in [True, False]: |
| 1048 |
with _mocked_session(cli, 'GET', 200, data): |
| 1049 |
result = cli.query(iql, bind_params=bind_params, dropna=dropna) |
| 1050 |
expected = \ |
| 1051 |
expected_dropna_true if dropna else expected_dropna_false |
| 1052 |
for r, e in zip(result, expected): |
| 1053 |
for k in e: |
| 1054 |
assert_frame_equal(e[k], r[k]) |
| 1055 |
|
| 1056 |
# test default value (dropna = True) |
| 1057 |
with _mocked_session(cli, 'GET', 200, data): |
| 1058 |
result = cli.query(iql, bind_params=bind_params) |
| 1059 |
for r, e in zip(result, expected_dropna_true): |
| 1060 |
for k in e: |
| 1061 |
assert_frame_equal(e[k], r[k]) |
| 1062 |
|
| 1063 |
def test_query_with_empty_result(self): |
| 1064 |
"""Test query with empty results in TestDataFrameClient object.""" |
| 1065 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 1066 |
with _mocked_session(cli, 'GET', 200, {"results": [{}]}): |
| 1067 |
result = cli.query('select column_one from foo;') |
| 1068 |
self.assertEqual(result, {}) |
| 1069 |
|
| 1070 |
def test_get_list_database(self): |
| 1071 |
"""Test get list of databases in TestDataFrameClient object.""" |
| 1072 |
data = {'results': [ |
| 1073 |
{'series': [ |
| 1074 |
{'measurement': 'databases', |
| 1075 |
'values': [ |
| 1076 |
['new_db_1'], |
| 1077 |
['new_db_2']], |
| 1078 |
'columns': ['name']}]} |
| 1079 |
]} |
| 1080 |
|
| 1081 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 1082 |
with _mocked_session(cli, 'get', 200, json.dumps(data)): |
| 1083 |
self.assertListEqual( |
| 1084 |
cli.get_list_database(), |
| 1085 |
[{'name': 'new_db_1'}, {'name': 'new_db_2'}] |
| 1086 |
) |
| 1087 |
|
| 1088 |
def test_datetime_to_epoch(self): |
| 1089 |
"""Test convert datetime to epoch in TestDataFrameClient object.""" |
| 1090 |
timestamp = pd.Timestamp('2013-01-01 00:00:00.000+00:00') |
| 1091 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 1092 |
|
| 1093 |
self.assertEqual( |
| 1094 |
cli._datetime_to_epoch(timestamp), |
| 1095 |
1356998400.0 |
| 1096 |
) |
| 1097 |
self.assertEqual( |
| 1098 |
cli._datetime_to_epoch(timestamp, time_precision='h'), |
| 1099 |
1356998400.0 / 3600 |
| 1100 |
) |
| 1101 |
self.assertEqual( |
| 1102 |
cli._datetime_to_epoch(timestamp, time_precision='m'), |
| 1103 |
1356998400.0 / 60 |
| 1104 |
) |
| 1105 |
self.assertEqual( |
| 1106 |
cli._datetime_to_epoch(timestamp, time_precision='s'), |
| 1107 |
1356998400.0 |
| 1108 |
) |
| 1109 |
self.assertEqual( |
| 1110 |
cli._datetime_to_epoch(timestamp, time_precision='ms'), |
| 1111 |
1356998400000.0 |
| 1112 |
) |
| 1113 |
self.assertEqual( |
| 1114 |
cli._datetime_to_epoch(timestamp, time_precision='u'), |
| 1115 |
1356998400000000.0 |
| 1116 |
) |
| 1117 |
self.assertEqual( |
| 1118 |
cli._datetime_to_epoch(timestamp, time_precision='n'), |
| 1119 |
1356998400000000000.0 |
| 1120 |
) |
| 1121 |
|
| 1122 |
def test_dsn_constructor(self): |
| 1123 |
"""Test data source name deconstructor in TestDataFrameClient.""" |
| 1124 |
client = DataFrameClient.from_dsn('influxdb://localhost:8086') |
| 1125 |
self.assertIsInstance(client, DataFrameClient) |
| 1126 |
self.assertEqual('http://localhost:8086', client._baseurl) |
| 1127 |
|
| 1128 |
def test_write_points_from_dataframe_with_nan_line(self): |
| 1129 |
"""Test write points from dataframe with Nan lines.""" |
| 1130 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 1131 |
dataframe = pd.DataFrame(data=[["1", 1, np.inf], ["2", 2, np.nan]], |
| 1132 |
index=[now, now + timedelta(hours=1)], |
| 1133 |
columns=["column_one", "column_two", |
| 1134 |
"column_three"]) |
| 1135 |
expected = ( |
| 1136 |
b"foo column_one=\"1\",column_two=1i 0\n" |
| 1137 |
b"foo column_one=\"2\",column_two=2i " |
| 1138 |
b"3600000000000\n" |
| 1139 |
) |
| 1140 |
|
| 1141 |
with requests_mock.Mocker() as m: |
| 1142 |
m.register_uri(requests_mock.POST, |
| 1143 |
"http://localhost:8086/write", |
| 1144 |
status_code=204) |
| 1145 |
|
| 1146 |
cli = DataFrameClient(database='db') |
| 1147 |
|
| 1148 |
cli.write_points(dataframe, 'foo', protocol='line') |
| 1149 |
self.assertEqual(m.last_request.body, expected) |
| 1150 |
|
| 1151 |
cli.write_points(dataframe, 'foo', tags=None, protocol='line') |
| 1152 |
self.assertEqual(m.last_request.body, expected) |
| 1153 |
|
| 1154 |
def test_write_points_from_dataframe_with_nan_json(self): |
| 1155 |
"""Test write points from json with NaN lines.""" |
| 1156 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 1157 |
dataframe = pd.DataFrame(data=[["1", 1, np.inf], ["2", 2, np.nan]], |
| 1158 |
index=[now, now + timedelta(hours=1)], |
| 1159 |
columns=["column_one", "column_two", |
| 1160 |
"column_three"]) |
| 1161 |
expected = ( |
| 1162 |
b"foo column_one=\"1\",column_two=1i 0\n" |
| 1163 |
b"foo column_one=\"2\",column_two=2i " |
| 1164 |
b"3600000000000\n" |
| 1165 |
) |
| 1166 |
|
| 1167 |
with requests_mock.Mocker() as m: |
| 1168 |
m.register_uri(requests_mock.POST, |
| 1169 |
"http://localhost:8086/write", |
| 1170 |
status_code=204) |
| 1171 |
|
| 1172 |
cli = DataFrameClient(database='db') |
| 1173 |
|
| 1174 |
cli.write_points(dataframe, 'foo', protocol='json') |
| 1175 |
self.assertEqual(m.last_request.body, expected) |
| 1176 |
|
| 1177 |
cli.write_points(dataframe, 'foo', tags=None, protocol='json') |
| 1178 |
self.assertEqual(m.last_request.body, expected) |
| 1179 |
|
| 1180 |
def test_write_points_from_dataframe_with_tags_and_nan_line(self): |
| 1181 |
"""Test write points from dataframe with NaN lines and tags.""" |
| 1182 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 1183 |
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, np.inf], |
| 1184 |
['red', 0, "2", 2, np.nan]], |
| 1185 |
index=[now, now + timedelta(hours=1)], |
| 1186 |
columns=["tag_one", "tag_two", "column_one", |
| 1187 |
"column_two", "column_three"]) |
| 1188 |
expected = ( |
| 1189 |
b"foo,tag_one=blue,tag_two=1 " |
| 1190 |
b"column_one=\"1\",column_two=1i " |
| 1191 |
b"0\n" |
| 1192 |
b"foo,tag_one=red,tag_two=0 " |
| 1193 |
b"column_one=\"2\",column_two=2i " |
| 1194 |
b"3600000000000\n" |
| 1195 |
) |
| 1196 |
|
| 1197 |
with requests_mock.Mocker() as m: |
| 1198 |
m.register_uri(requests_mock.POST, |
| 1199 |
"http://localhost:8086/write", |
| 1200 |
status_code=204) |
| 1201 |
|
| 1202 |
cli = DataFrameClient(database='db') |
| 1203 |
|
| 1204 |
cli.write_points(dataframe, 'foo', protocol='line', |
| 1205 |
tag_columns=['tag_one', 'tag_two']) |
| 1206 |
self.assertEqual(m.last_request.body, expected) |
| 1207 |
|
| 1208 |
cli.write_points(dataframe, 'foo', tags=None, protocol='line', |
| 1209 |
tag_columns=['tag_one', 'tag_two']) |
| 1210 |
self.assertEqual(m.last_request.body, expected) |
| 1211 |
|
| 1212 |
def test_write_points_from_dataframe_with_tags_and_nan_json(self): |
| 1213 |
"""Test write points from json with NaN lines and tags.""" |
| 1214 |
now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 1215 |
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, np.inf], |
| 1216 |
['red', 0, "2", 2, np.nan]], |
| 1217 |
index=[now, now + timedelta(hours=1)], |
| 1218 |
columns=["tag_one", "tag_two", "column_one", |
| 1219 |
"column_two", "column_three"]) |
| 1220 |
expected = ( |
| 1221 |
b"foo,tag_one=blue,tag_two=1 " |
| 1222 |
b"column_one=\"1\",column_two=1i " |
| 1223 |
b"0\n" |
| 1224 |
b"foo,tag_one=red,tag_two=0 " |
| 1225 |
b"column_one=\"2\",column_two=2i " |
| 1226 |
b"3600000000000\n" |
| 1227 |
) |
| 1228 |
|
| 1229 |
with requests_mock.Mocker() as m: |
| 1230 |
m.register_uri(requests_mock.POST, |
| 1231 |
"http://localhost:8086/write", |
| 1232 |
status_code=204) |
| 1233 |
|
| 1234 |
cli = DataFrameClient(database='db') |
| 1235 |
|
| 1236 |
cli.write_points(dataframe, 'foo', protocol='json', |
| 1237 |
tag_columns=['tag_one', 'tag_two']) |
| 1238 |
self.assertEqual(m.last_request.body, expected) |
| 1239 |
|
| 1240 |
cli.write_points(dataframe, 'foo', tags=None, protocol='json', |
| 1241 |
tag_columns=['tag_one', 'tag_two']) |
| 1242 |
self.assertEqual(m.last_request.body, expected) |
| 1243 |
|
| 1244 |
def test_query_custom_index(self): |
| 1245 |
"""Test query with custom indexes.""" |
| 1246 |
data = { |
| 1247 |
"results": [ |
| 1248 |
{ |
| 1249 |
"series": [ |
| 1250 |
{ |
| 1251 |
"name": "cpu_load_short", |
| 1252 |
"columns": ["time", "value", "host"], |
| 1253 |
"values": [ |
| 1254 |
[1, 0.55, "local"], |
| 1255 |
[2, 23422, "local"], |
| 1256 |
[3, 0.64, "local"] |
| 1257 |
] |
| 1258 |
} |
| 1259 |
] |
| 1260 |
} |
| 1261 |
] |
| 1262 |
} |
| 1263 |
|
| 1264 |
cli = DataFrameClient('host', 8086, 'username', 'password', 'db') |
| 1265 |
iql = "SELECT value FROM cpu_load_short WHERE region=$region;" \ |
| 1266 |
"SELECT count(value) FROM cpu_load_short WHERE region=$region" |
| 1267 |
bind_params = {'region': 'us-west'} |
| 1268 |
with _mocked_session(cli, 'GET', 200, data): |
| 1269 |
result = cli.query(iql, bind_params=bind_params, |
| 1270 |
data_frame_index=["time", "host"]) |
| 1271 |
|
| 1272 |
_data_frame = result['cpu_load_short'] |
| 1273 |
print(_data_frame) |
| 1274 |
|
| 1275 |
self.assertListEqual(["time", "host"], |
| 1276 |
list(_data_frame.index.names)) |
| 1277 |
|
| 1278 |
def test_dataframe_nanosecond_precision(self): |
| 1279 |
"""Test nanosecond precision.""" |
| 1280 |
for_df_dict = { |
| 1281 |
"nanFloats": [1.1, float('nan'), 3.3, 4.4], |
| 1282 |
"onlyFloats": [1.1, 2.2, 3.3, 4.4], |
| 1283 |
"strings": ['one_one', 'two_two', 'three_three', 'four_four'] |
| 1284 |
} |
| 1285 |
df = pd.DataFrame.from_dict(for_df_dict) |
| 1286 |
df['time'] = ['2019-10-04 06:27:19.850557111+00:00', |
| 1287 |
'2019-10-04 06:27:19.850557184+00:00', |
| 1288 |
'2019-10-04 06:27:42.251396864+00:00', |
| 1289 |
'2019-10-04 06:27:42.251396974+00:00'] |
| 1290 |
df['time'] = pd.to_datetime(df['time'], unit='ns') |
| 1291 |
df = df.set_index('time') |
| 1292 |
|
| 1293 |
expected = ( |
| 1294 |
b'foo nanFloats=1.1,onlyFloats=1.1,strings="one_one" 1570170439850557111\n' # noqa E501 line too long |
| 1295 |
b'foo onlyFloats=2.2,strings="two_two" 1570170439850557184\n' # noqa E501 line too long |
| 1296 |
b'foo nanFloats=3.3,onlyFloats=3.3,strings="three_three" 1570170462251396864\n' # noqa E501 line too long |
| 1297 |
b'foo nanFloats=4.4,onlyFloats=4.4,strings="four_four" 1570170462251396974\n' # noqa E501 line too long |
| 1298 |
) |
| 1299 |
|
| 1300 |
with requests_mock.Mocker() as m: |
| 1301 |
m.register_uri( |
| 1302 |
requests_mock.POST, |
| 1303 |
"http://localhost:8086/write", |
| 1304 |
status_code=204 |
| 1305 |
) |
| 1306 |
|
| 1307 |
cli = DataFrameClient(database='db') |
| 1308 |
cli.write_points(df, 'foo', time_precision='n') |
| 1309 |
|
| 1310 |
self.assertEqual(m.last_request.body, expected) |
| 1311 |
|
| 1312 |
def test_dataframe_nanosecond_precision_one_microsecond(self): |
| 1313 |
"""Test nanosecond precision within one microsecond.""" |
| 1314 |
# 1 microsecond = 1000 nanoseconds |
| 1315 |
start = np.datetime64('2019-10-04T06:27:19.850557000') |
| 1316 |
end = np.datetime64('2019-10-04T06:27:19.850558000') |
| 1317 |
|
| 1318 |
# generate timestamps with nanosecond precision |
| 1319 |
timestamps = np.arange( |
| 1320 |
start, |
| 1321 |
end + np.timedelta64(1, 'ns'), |
| 1322 |
np.timedelta64(1, 'ns') |
| 1323 |
) |
| 1324 |
# generate values |
| 1325 |
values = np.arange(0.0, len(timestamps)) |
| 1326 |
|
| 1327 |
df = pd.DataFrame({'value': values}, index=timestamps) |
| 1328 |
with requests_mock.Mocker() as m: |
| 1329 |
m.register_uri( |
| 1330 |
requests_mock.POST, |
| 1331 |
"http://localhost:8086/write", |
| 1332 |
status_code=204 |
| 1333 |
) |
| 1334 |
|
| 1335 |
cli = DataFrameClient(database='db') |
| 1336 |
cli.write_points(df, 'foo', time_precision='n') |
| 1337 |
|
| 1338 |
lines = m.last_request.body.decode('utf-8').split('\n') |
| 1339 |
self.assertEqual(len(lines), 1002) |
| 1340 |
|
| 1341 |
for index, line in enumerate(lines): |
| 1342 |
if index == 1001: |
| 1343 |
self.assertEqual(line, '') |
| 1344 |
continue |
| 1345 |
self.assertEqual( |
| 1346 |
line, |
| 1347 |
f"foo value={index}.0 157017043985055{7000 + index:04}" |
| 1348 |
) |