|
13 | 13 | import warnings
|
14 | 14 | import requests_mock
|
15 | 15 |
|
16 |
| -from influxdb.tests import skip_if_pypy, using_pypy |
17 | 16 | from nose.tools import raises
|
| 17 | +from influxdb.tests import skip_if_pypy, using_pypy |
18 | 18 |
|
19 | 19 | from .client_test import _mocked_session
|
20 | 20 |
|
21 | 21 | if not using_pypy:
|
22 | 22 | import pandas as pd
|
23 | 23 | from pandas.util.testing import assert_frame_equal
|
24 | 24 | from influxdb import DataFrameClient
|
25 |
| - import numpy |
| 25 | + import numpy as np |
26 | 26 |
|
27 | 27 |
|
28 | 28 | @skip_if_pypy
|
@@ -462,7 +462,7 @@ def test_write_points_from_dataframe_with_numeric_precision(self):
|
462 | 462 | ["2", 2, 2.2222222222222]],
|
463 | 463 | index=[now, now + timedelta(hours=1)])
|
464 | 464 |
|
465 |
| - if numpy.lib.NumpyVersion(numpy.__version__) <= '1.13.3': |
| 465 | + if np.lib.NumpyVersion(np.__version__) <= '1.13.3': |
466 | 466 | expected_default_precision = (
|
467 | 467 | b'foo,hello=there 0=\"1\",1=1i,2=1.11111111111 0\n'
|
468 | 468 | b'foo,hello=there 0=\"2\",1=2i,2=2.22222222222 3600000000000\n'
|
@@ -1032,3 +1032,119 @@ def test_dsn_constructor(self):
|
1032 | 1032 | client = DataFrameClient.from_dsn('influxdb://localhost:8086')
|
1033 | 1033 | self.assertIsInstance(client, DataFrameClient)
|
1034 | 1034 | self.assertEqual('http://localhost:8086', client._baseurl)
|
| 1035 | + |
| 1036 | + def test_write_points_from_dataframe_with_nan_line(self): |
| 1037 | + """Test write points from dataframe with Nan lines.""" |
| 1038 | + now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 1039 | + dataframe = pd.DataFrame(data=[["1", 1, np.inf], ["2", 2, np.nan]], |
| 1040 | + index=[now, now + timedelta(hours=1)], |
| 1041 | + columns=["column_one", "column_two", |
| 1042 | + "column_three"]) |
| 1043 | + expected = ( |
| 1044 | + b"foo column_one=\"1\",column_two=1i 0\n" |
| 1045 | + b"foo column_one=\"2\",column_two=2i " |
| 1046 | + b"3600000000000\n" |
| 1047 | + ) |
| 1048 | + |
| 1049 | + with requests_mock.Mocker() as m: |
| 1050 | + m.register_uri(requests_mock.POST, |
| 1051 | + "http://localhost:8086/write", |
| 1052 | + status_code=204) |
| 1053 | + |
| 1054 | + cli = DataFrameClient(database='db') |
| 1055 | + |
| 1056 | + cli.write_points(dataframe, 'foo', protocol='line') |
| 1057 | + self.assertEqual(m.last_request.body, expected) |
| 1058 | + |
| 1059 | + cli.write_points(dataframe, 'foo', tags=None, protocol='line') |
| 1060 | + self.assertEqual(m.last_request.body, expected) |
| 1061 | + |
| 1062 | + def test_write_points_from_dataframe_with_nan_json(self): |
| 1063 | + """Test write points from json with NaN lines.""" |
| 1064 | + now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 1065 | + dataframe = pd.DataFrame(data=[["1", 1, np.inf], ["2", 2, np.nan]], |
| 1066 | + index=[now, now + timedelta(hours=1)], |
| 1067 | + columns=["column_one", "column_two", |
| 1068 | + "column_three"]) |
| 1069 | + expected = ( |
| 1070 | + b"foo column_one=\"1\",column_two=1i 0\n" |
| 1071 | + b"foo column_one=\"2\",column_two=2i " |
| 1072 | + b"3600000000000\n" |
| 1073 | + ) |
| 1074 | + |
| 1075 | + with requests_mock.Mocker() as m: |
| 1076 | + m.register_uri(requests_mock.POST, |
| 1077 | + "http://localhost:8086/write", |
| 1078 | + status_code=204) |
| 1079 | + |
| 1080 | + cli = DataFrameClient(database='db') |
| 1081 | + |
| 1082 | + cli.write_points(dataframe, 'foo', protocol='json') |
| 1083 | + self.assertEqual(m.last_request.body, expected) |
| 1084 | + |
| 1085 | + cli.write_points(dataframe, 'foo', tags=None, protocol='json') |
| 1086 | + self.assertEqual(m.last_request.body, expected) |
| 1087 | + |
| 1088 | + def test_write_points_from_dataframe_with_tags_and_nan_line(self): |
| 1089 | + """Test write points from dataframe with NaN lines and tags.""" |
| 1090 | + now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 1091 | + dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, np.inf], |
| 1092 | + ['red', 0, "2", 2, np.nan]], |
| 1093 | + index=[now, now + timedelta(hours=1)], |
| 1094 | + columns=["tag_one", "tag_two", "column_one", |
| 1095 | + "column_two", "column_three"]) |
| 1096 | + expected = ( |
| 1097 | + b"foo,tag_one=blue,tag_two=1 " |
| 1098 | + b"column_one=\"1\",column_two=1i " |
| 1099 | + b"0\n" |
| 1100 | + b"foo,tag_one=red,tag_two=0 " |
| 1101 | + b"column_one=\"2\",column_two=2i " |
| 1102 | + b"3600000000000\n" |
| 1103 | + ) |
| 1104 | + |
| 1105 | + with requests_mock.Mocker() as m: |
| 1106 | + m.register_uri(requests_mock.POST, |
| 1107 | + "http://localhost:8086/write", |
| 1108 | + status_code=204) |
| 1109 | + |
| 1110 | + cli = DataFrameClient(database='db') |
| 1111 | + |
| 1112 | + cli.write_points(dataframe, 'foo', protocol='line', |
| 1113 | + tag_columns=['tag_one', 'tag_two']) |
| 1114 | + self.assertEqual(m.last_request.body, expected) |
| 1115 | + |
| 1116 | + cli.write_points(dataframe, 'foo', tags=None, protocol='line', |
| 1117 | + tag_columns=['tag_one', 'tag_two']) |
| 1118 | + self.assertEqual(m.last_request.body, expected) |
| 1119 | + |
| 1120 | + def test_write_points_from_dataframe_with_tags_and_nan_json(self): |
| 1121 | + """Test write points from json with NaN lines and tags.""" |
| 1122 | + now = pd.Timestamp('1970-01-01 00:00+00:00') |
| 1123 | + dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, np.inf], |
| 1124 | + ['red', 0, "2", 2, np.nan]], |
| 1125 | + index=[now, now + timedelta(hours=1)], |
| 1126 | + columns=["tag_one", "tag_two", "column_one", |
| 1127 | + "column_two", "column_three"]) |
| 1128 | + expected = ( |
| 1129 | + b"foo,tag_one=blue,tag_two=1 " |
| 1130 | + b"column_one=\"1\",column_two=1i " |
| 1131 | + b"0\n" |
| 1132 | + b"foo,tag_one=red,tag_two=0 " |
| 1133 | + b"column_one=\"2\",column_two=2i " |
| 1134 | + b"3600000000000\n" |
| 1135 | + ) |
| 1136 | + |
| 1137 | + with requests_mock.Mocker() as m: |
| 1138 | + m.register_uri(requests_mock.POST, |
| 1139 | + "http://localhost:8086/write", |
| 1140 | + status_code=204) |
| 1141 | + |
| 1142 | + cli = DataFrameClient(database='db') |
| 1143 | + |
| 1144 | + cli.write_points(dataframe, 'foo', protocol='json', |
| 1145 | + tag_columns=['tag_one', 'tag_two']) |
| 1146 | + self.assertEqual(m.last_request.body, expected) |
| 1147 | + |
| 1148 | + cli.write_points(dataframe, 'foo', tags=None, protocol='json', |
| 1149 | + tag_columns=['tag_one', 'tag_two']) |
| 1150 | + self.assertEqual(m.last_request.body, expected) |
0 commit comments