6
6
import json
7
7
import requests_mock
8
8
from nose .tools import raises
9
- from datetime import datetime , timedelta
10
- import time
9
+ from datetime import timedelta
11
10
from tests import skipIfPYpy , using_pypy
12
11
13
12
if not using_pypy :
22
21
class TestDataFrameClient (unittest .TestCase ):
23
22
24
23
def test_write_points_from_dataframe (self ):
25
- now = datetime ( 2014 , 11 , 15 , 15 , 42 , 44 , 543 )
24
+ now = pd . Timestamp ( '1970-01-01 00:00+00:00' )
26
25
dataframe = pd .DataFrame (data = [["1" , 1 , 1.0 ], ["2" , 2 , 2.0 ]],
27
26
index = [now , now + timedelta (hours = 1 )],
28
27
columns = ["column_one" , "column_two" ,
29
28
"column_three" ])
30
29
points = [
31
30
{
32
31
"points" : [
33
- ["1" , 1 , 1.0 , time .mktime (now .timetuple ())],
34
- ["2" , 2 , 2.0 , time .mktime ((now + timedelta (hours = 1 ))
35
- .timetuple ())]
32
+ ["1" , 1 , 1.0 , 0 ],
33
+ ["2" , 2 , 2.0 , 3600 ]
36
34
],
37
35
"name" : "foo" ,
38
36
"columns" : ["column_one" , "column_two" , "column_three" , "time" ]
@@ -48,17 +46,30 @@ def test_write_points_from_dataframe(self):
48
46
49
47
self .assertListEqual (json .loads (m .last_request .body ), points )
50
48
49
+ def test_write_points_from_dataframe_in_batches (self ):
50
+ now = pd .Timestamp ('1970-01-01 00:00+00:00' )
51
+ dataframe = pd .DataFrame (data = [["1" , 1 , 1.0 ], ["2" , 2 , 2.0 ]],
52
+ index = [now , now + timedelta (hours = 1 )],
53
+ columns = ["column_one" , "column_two" ,
54
+ "column_three" ])
55
+ with requests_mock .Mocker () as m :
56
+ m .register_uri (requests_mock .POST ,
57
+ "http://localhost:8086/db/db/series" )
58
+
59
+ cli = DataFrameClient (database = 'db' )
60
+ assert cli .write_points ({"foo" : dataframe },
61
+ batch_size = 1 ) is True
62
+
51
63
def test_write_points_from_dataframe_with_numeric_column_names (self ):
52
- now = datetime ( 2014 , 11 , 15 , 15 , 42 , 44 , 543 )
64
+ now = pd . Timestamp ( '1970-01-01 00:00+00:00' )
53
65
# df with numeric column names
54
66
dataframe = pd .DataFrame (data = [["1" , 1 , 1.0 ], ["2" , 2 , 2.0 ]],
55
67
index = [now , now + timedelta (hours = 1 )])
56
68
points = [
57
69
{
58
70
"points" : [
59
- ["1" , 1 , 1.0 , time .mktime (now .timetuple ())],
60
- ["2" , 2 , 2.0 , time .mktime ((now + timedelta (hours = 1 ))
61
- .timetuple ())]
71
+ ["1" , 1 , 1.0 , 0 ],
72
+ ["2" , 2 , 2.0 , 3600 ]
62
73
],
63
74
"name" : "foo" ,
64
75
"columns" : ['0' , '1' , '2' , "time" ]
@@ -75,18 +86,16 @@ def test_write_points_from_dataframe_with_numeric_column_names(self):
75
86
self .assertListEqual (json .loads (m .last_request .body ), points )
76
87
77
88
def test_write_points_from_dataframe_with_period_index (self ):
78
- now = datetime (2014 , 11 , 16 )
79
89
dataframe = pd .DataFrame (data = [["1" , 1 , 1.0 ], ["2" , 2 , 2.0 ]],
80
- index = [pd .Period ('2014-11-16 ' ),
81
- pd .Period ('2014-11-17 ' )],
90
+ index = [pd .Period ('1970-01-01 ' ),
91
+ pd .Period ('1970-01-02 ' )],
82
92
columns = ["column_one" , "column_two" ,
83
93
"column_three" ])
84
94
points = [
85
95
{
86
96
"points" : [
87
- ["1" , 1 , 1.0 , time .mktime (now .timetuple ())],
88
- ["2" , 2 , 2.0 , time .mktime ((now + timedelta (hours = 24 ))
89
- .timetuple ())]
97
+ ["1" , 1 , 1.0 , 0 ],
98
+ ["2" , 2 , 2.0 , 86400 ]
90
99
],
91
100
"name" : "foo" ,
92
101
"columns" : ["column_one" , "column_two" , "column_three" , "time" ]
@@ -117,7 +126,7 @@ def test_write_points_from_dataframe_fails_without_time_index(self):
117
126
118
127
@raises (TypeError )
119
128
def test_write_points_from_dataframe_fails_with_series (self ):
120
- now = datetime ( 2014 , 11 , 16 )
129
+ now = pd . Timestamp ( '1970-01-01 00:00+00:00' )
121
130
dataframe = pd .Series (data = [1.0 , 2.0 ],
122
131
index = [now , now + timedelta (hours = 1 )])
123
132
@@ -134,17 +143,37 @@ def test_query_into_dataframe(self):
134
143
"name" : "foo" ,
135
144
"columns" : ["time" , "sequence_number" , "column_one" ],
136
145
"points" : [
137
- [1383876043 , 16 , 2 ], [1383876043 , 15 , 1 ],
138
- [1383876035 , 14 , 2 ], [1383876035 , 13 , 1 ]
146
+ [3600 , 16 , 2 ], [3600 , 15 , 1 ],
147
+ [0 , 14 , 2 ], [0 , 13 , 1 ]
139
148
]
140
149
}
141
150
]
142
- dataframe = pd .DataFrame (data = [[16 , 2 ], [15 , 1 ], [14 , 2 ], [13 , 1 ]],
143
- index = pd .to_datetime ([1383876043 , 1383876043 ,
144
- 1383876035 , 1383876035 ],
151
+ # dataframe sorted ascending by time first, then sequence_number
152
+ dataframe = pd .DataFrame (data = [[13 , 1 ], [14 , 2 ], [15 , 1 ], [16 , 2 ]],
153
+ index = pd .to_datetime ([0 , 0 ,
154
+ 3600 , 3600 ],
145
155
unit = 's' , utc = True ),
146
156
columns = ['sequence_number' , 'column_one' ])
147
157
with _mocked_session ('get' , 200 , data ):
148
158
cli = DataFrameClient ('host' , 8086 , 'username' , 'password' , 'db' )
149
159
result = cli .query ('select column_one from foo;' )
150
160
assert_frame_equal (dataframe , result )
161
+
162
+ def test_query_with_empty_result (self ):
163
+ with _mocked_session ('get' , 200 , []):
164
+ cli = DataFrameClient ('host' , 8086 , 'username' , 'password' , 'db' )
165
+ result = cli .query ('select column_one from foo;' )
166
+ assert result == []
167
+
168
+ def test_list_series (self ):
169
+ response = [
170
+ {
171
+ 'columns' : ['time' , 'name' ],
172
+ 'name' : 'list_series_result' ,
173
+ 'points' : [[0 , 'seriesA' ], [0 , 'seriesB' ]]
174
+ }
175
+ ]
176
+ with _mocked_session ('get' , 200 , response ):
177
+ cli = DataFrameClient ('host' , 8086 , 'username' , 'password' , 'db' )
178
+ series_list = cli .get_list_series ()
179
+ assert series_list == ['seriesA' , 'seriesB' ]
0 commit comments