4
4
import pytest
5
5
from mock import patch , Mock
6
6
from six .moves import urllib
7
- from six import StringIO
7
+ from six import StringIO , binary_type , BytesIO
8
8
9
9
from pydruid .client import PyDruid
10
10
from pydruid .query import Query
@@ -17,23 +17,22 @@ def create_client():
17
17
18
18
19
19
def create_blank_query ():
20
- return Query ({}, ' none' )
20
+ return Query ({}, " none" )
21
21
22
22
23
- def _http_error (code , msg , data = '' ):
23
+ def _http_error (code , msg , data = "" ):
24
24
# Need a file-like object for the response data
25
- fp = StringIO (data )
25
+ if isinstance (data , binary_type ):
26
+ fp = BytesIO (data )
27
+ else :
28
+ fp = StringIO (data )
26
29
return urllib .error .HTTPError (
27
- url = 'http://fakeurl:8080/druid/v2/' ,
28
- hdrs = {},
29
- code = code ,
30
- msg = msg ,
31
- fp = fp ,
30
+ url = "http://fakeurl:8080/druid/v2/" , hdrs = {}, code = code , msg = msg , fp = fp ,
32
31
)
33
32
34
33
35
34
class TestPyDruid :
36
- @patch (' pydruid.client.urllib.request.urlopen' )
35
+ @patch (" pydruid.client.urllib.request.urlopen" )
37
36
def test_druid_returns_error (self , mock_urlopen ):
38
37
# given
39
38
mock_urlopen .side_effect = _http_error (500 , "Druid error" )
@@ -42,52 +41,63 @@ def test_druid_returns_error(self, mock_urlopen):
42
41
# when / then
43
42
with pytest .raises (IOError ):
44
43
client .topn (
45
- datasource = "testdatasource" ,
46
- granularity = "all" ,
47
- intervals = "2015-12-29/pt1h" ,
48
- aggregations = {"count" : doublesum ("count" )},
49
- dimension = "user_name" ,
50
- metric = "count" ,
51
- filter = Dimension ("user_lang" ) == "en" ,
52
- threshold = 1 ,
53
- context = {"timeout" : 1000 })
54
-
55
- @patch ('pydruid.client.urllib.request.urlopen' )
44
+ datasource = "testdatasource" ,
45
+ granularity = "all" ,
46
+ intervals = "2015-12-29/pt1h" ,
47
+ aggregations = {"count" : doublesum ("count" )},
48
+ dimension = "user_name" ,
49
+ metric = "count" ,
50
+ filter = Dimension ("user_lang" ) == "en" ,
51
+ threshold = 1 ,
52
+ context = {"timeout" : 1000 },
53
+ )
54
+
55
+ @patch ("pydruid.client.urllib.request.urlopen" )
56
56
def test_druid_returns_html_error (self , mock_urlopen ):
57
57
# given
58
- message = textwrap .dedent ("""
58
+ message = textwrap .dedent (
59
+ """
59
60
<html>
60
61
<head>
61
- <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
62
+ <meta http-equiv="Content-Type" content="text/html;
63
+ charset=ISO-8859-1"/>
62
64
<title>Error 500 </title>
63
65
</head>
64
66
<body>
65
67
<h2>HTTP ERROR: 500</h2>
66
68
<p>Problem accessing /druid/v2/. Reason:
67
- <pre> javax.servlet.ServletException: java.lang.OutOfMemoryError: GC overhead limit exceeded</pre></p>
68
- <hr /><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.19.v20170502</a><hr/>
69
+ <pre> javax.servlet.ServletException:
70
+ java.lang.OutOfMemoryError: GC overhead limit exceeded</pre></p>
71
+ <hr /><a href="http://eclipse.org/jetty">
72
+ Powered by Jetty:// 9.3.19.v20170502</a><hr/>
69
73
</body>
70
74
</html>
71
- """ ).strip ()
72
- mock_urlopen .side_effect = _http_error (500 , 'Internal Server Error' , message )
75
+ """
76
+ ).strip ()
77
+ mock_urlopen .side_effect = _http_error (500 , "Internal Server Error" , message )
73
78
client = create_client ()
74
79
75
80
# when / then
76
81
with pytest .raises (IOError ) as e :
77
82
client .topn (
78
- datasource = "testdatasource" ,
79
- granularity = "all" ,
80
- intervals = "2015-12-29/pt1h" ,
81
- aggregations = {"count" : doublesum ("count" )},
82
- dimension = "user_name" ,
83
- metric = "count" ,
84
- filter = Dimension ("user_lang" ) == "en" ,
85
- threshold = 1 ,
86
- context = {"timeout" : 1000 })
87
-
88
- assert str (e .value ) == textwrap .dedent ("""
89
- HTTP Error 500: Internal Server Error
90
- Druid Error: javax.servlet.ServletException: java.lang.OutOfMemoryError: GC overhead limit exceeded
83
+ datasource = "testdatasource" ,
84
+ granularity = "all" ,
85
+ intervals = "2015-12-29/pt1h" ,
86
+ aggregations = {"count" : doublesum ("count" )},
87
+ dimension = "user_name" ,
88
+ metric = "count" ,
89
+ filter = Dimension ("user_lang" ) == "en" ,
90
+ threshold = 1 ,
91
+ context = {"timeout" : 1000 },
92
+ )
93
+
94
+ assert (
95
+ str (e .value )
96
+ == textwrap .dedent (
97
+ """
98
+ HTTP Error 500: Internal Server Error
99
+ Druid Error: javax.servlet.ServletException:
100
+ java.lang.OutOfMemoryError: GC overhead limit exceeded
91
101
Query is: {
92
102
"aggregations": [
93
103
{
@@ -112,9 +122,34 @@ def test_druid_returns_html_error(self, mock_urlopen):
112
122
"queryType": "topN",
113
123
"threshold": 1
114
124
}
115
- """ ).strip ()
125
+ """
126
+ ).strip ()
127
+ )
128
+
129
+ @patch ("pydruid.client.urllib.request.urlopen" )
130
+ def test_druid_returns_string_error_bytes_error_response (self , mock_urlopen ):
131
+ # given
132
+ message = b"Error as bytes, please decode me"
133
+ mock_urlopen .side_effect = _http_error (500 , "Internal Server Error" , message )
134
+ client = create_client ()
116
135
117
- @patch ('pydruid.client.urllib.request.urlopen' )
136
+ # when / then
137
+ with pytest .raises (IOError ) as e :
138
+ client .topn (
139
+ datasource = "testdatasource" ,
140
+ granularity = "all" ,
141
+ intervals = "2015-12-29/pt1h" ,
142
+ aggregations = {"count" : doublesum ("count" )},
143
+ dimension = "user_name" ,
144
+ metric = "count" ,
145
+ filter = Dimension ("user_lang" ) == "en" ,
146
+ threshold = 1 ,
147
+ context = {"timeout" : 1000 },
148
+ )
149
+
150
+ assert "Error as bytes, please decode me" in str (e .value )
151
+
152
+ @patch ("pydruid.client.urllib.request.urlopen" )
118
153
def test_druid_returns_results (self , mock_urlopen ):
119
154
# given
120
155
response = Mock ()
@@ -126,28 +161,31 @@ def test_druid_returns_results(self, mock_urlopen):
126
161
"metric" : 100
127
162
} ]
128
163
} ]
129
- """ .encode ("utf-8" )
164
+ """ .encode (
165
+ "utf-8"
166
+ )
130
167
mock_urlopen .return_value = response
131
168
client = create_client ()
132
169
133
170
# when
134
171
top = client .topn (
135
- datasource = "testdatasource" ,
136
- granularity = "all" ,
137
- intervals = "2015-12-29/pt1h" ,
138
- aggregations = {"count" : doublesum ("count" )},
139
- dimension = "user_name" ,
140
- metric = "count" ,
141
- filter = Dimension ("user_lang" ) == "en" ,
142
- threshold = 1 ,
143
- context = {"timeout" : 1000 })
172
+ datasource = "testdatasource" ,
173
+ granularity = "all" ,
174
+ intervals = "2015-12-29/pt1h" ,
175
+ aggregations = {"count" : doublesum ("count" )},
176
+ dimension = "user_name" ,
177
+ metric = "count" ,
178
+ filter = Dimension ("user_lang" ) == "en" ,
179
+ threshold = 1 ,
180
+ context = {"timeout" : 1000 },
181
+ )
144
182
145
183
# then
146
184
assert top is not None
147
185
assert len (top .result ) == 1
148
- assert len (top .result [0 ][' result' ]) == 1
186
+ assert len (top .result [0 ][" result" ]) == 1
149
187
150
- @patch (' pydruid.client.urllib.request.urlopen' )
188
+ @patch (" pydruid.client.urllib.request.urlopen" )
151
189
def test_client_allows_to_export_last_query (self , mock_urlopen ):
152
190
# given
153
191
response = Mock ()
@@ -159,29 +197,33 @@ def test_client_allows_to_export_last_query(self, mock_urlopen):
159
197
"metric" : 100
160
198
} ]
161
199
} ]
162
- """ .encode ("utf-8" )
200
+ """ .encode (
201
+ "utf-8"
202
+ )
163
203
mock_urlopen .return_value = response
164
204
client = create_client ()
165
205
client .topn (
166
- datasource = "testdatasource" ,
167
- granularity = "all" ,
168
- intervals = "2015-12-29/pt1h" ,
169
- aggregations = {"count" : doublesum ("count" )},
170
- dimension = "user_name" ,
171
- metric = "count" ,
172
- filter = Dimension ("user_lang" ) == "en" ,
173
- threshold = 1 ,
174
- context = {"timeout" : 1000 })
206
+ datasource = "testdatasource" ,
207
+ granularity = "all" ,
208
+ intervals = "2015-12-29/pt1h" ,
209
+ aggregations = {"count" : doublesum ("count" )},
210
+ dimension = "user_name" ,
211
+ metric = "count" ,
212
+ filter = Dimension ("user_lang" ) == "en" ,
213
+ threshold = 1 ,
214
+ context = {"timeout" : 1000 },
215
+ )
175
216
176
217
# when / then
177
- # assert that last_query.export_tsv method was called (it should throw an exception, given empty path)
218
+ # assert that last_query.export_tsv method was called (it should throw
219
+ # an exception, given empty path)
178
220
with pytest .raises (TypeError ):
179
221
client .export_tsv (None )
180
222
181
- @patch (' pydruid.client.urllib.request.urlopen' )
223
+ @patch (" pydruid.client.urllib.request.urlopen" )
182
224
def test_client_auth_creds (self , mock_urlopen ):
183
225
client = create_client ()
184
226
query = create_blank_query ()
185
- client .set_basic_auth_credentials (' myUsername' , ' myPassword' )
227
+ client .set_basic_auth_credentials (" myUsername" , " myPassword" )
186
228
headers , _ , _ = client ._prepare_url_headers_and_body (query )
187
- assert headers [' Authorization' ] == "Basic bXlVc2VybmFtZTpteVBhc3N3b3Jk"
229
+ assert headers [" Authorization" ] == "Basic bXlVc2VybmFtZTpteVBhc3N3b3Jk"
0 commit comments