-
Notifications
You must be signed in to change notification settings - Fork 552
/
Copy pathtest_presto.py
250 lines (217 loc) Β· 9.46 KB
/
test_presto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""Presto integration tests.
These rely on having a Presto+Hadoop cluster set up.
They also require a tables created by make_test_tables.sh.
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import contextlib
import os
from decimal import Decimal
import requests
from pyhive import exc
from pyhive import presto
from pyhive.tests.dbapi_test_case import DBAPITestCase
from pyhive.tests.dbapi_test_case import with_cursor
import mock
import unittest
import datetime
_HOST = 'localhost'
_PORT = '8080'
class TestPresto(unittest.TestCase, DBAPITestCase):
__test__ = True
def connect(self):
return presto.connect(host=_HOST, port=_PORT, source=self.id())
def test_bad_protocol(self):
self.assertRaisesRegexp(ValueError, 'Protocol must be',
lambda: presto.connect('localhost', protocol='nonsense').cursor())
def test_escape_args(self):
escaper = presto.PrestoParamEscaper()
self.assertEqual(escaper.escape_args((datetime.date(2020, 4, 17),)),
("date '2020-04-17'",))
self.assertEqual(escaper.escape_args((datetime.datetime(2020, 4, 17, 12, 0, 0, 123456),)),
("timestamp '2020-04-17 12:00:00.123'",))
@with_cursor
def test_description(self, cursor):
cursor.execute('SELECT 1 AS foobar FROM one_row')
self.assertEqual(cursor.description, [('foobar', 'integer', None, None, None, None, True)])
self.assertIsNotNone(cursor.last_query_id)
@with_cursor
def test_complex(self, cursor):
cursor.execute('SELECT * FROM one_row_complex')
# TODO Presto drops the union field
if os.environ.get('PRESTO') == '0.147':
tinyint_type = 'integer'
smallint_type = 'integer'
float_type = 'double'
else:
# some later version made these map to more specific types
tinyint_type = 'tinyint'
smallint_type = 'smallint'
float_type = 'real'
self.assertEqual(cursor.description, [
('boolean', 'boolean', None, None, None, None, True),
('tinyint', tinyint_type, None, None, None, None, True),
('smallint', smallint_type, None, None, None, None, True),
('int', 'integer', None, None, None, None, True),
('bigint', 'bigint', None, None, None, None, True),
('float', float_type, None, None, None, None, True),
('double', 'double', None, None, None, None, True),
('string', 'varchar', None, None, None, None, True),
('timestamp', 'timestamp', None, None, None, None, True),
('binary', 'varbinary', None, None, None, None, True),
('array', 'array(integer)', None, None, None, None, True),
('map', 'map(integer,integer)', None, None, None, None, True),
('struct', 'row(a integer,b integer)', None, None, None, None, True),
# ('union', 'varchar', None, None, None, None, True),
('decimal', 'decimal(10,1)', None, None, None, None, True),
])
rows = cursor.fetchall()
expected = [(
True,
127,
32767,
2147483647,
9223372036854775807,
0.5,
0.25,
'a string',
'1970-01-01 00:00:00.000',
b'123',
[1, 2],
{"1": 2, "3": 4}, # Presto converts all keys to strings so that they're valid JSON
[1, 2], # struct is returned as a list of elements
# '{0:1}',
Decimal('0.1'),
)]
self.assertEqual(rows, expected)
# catch unicode/str
self.assertEqual(list(map(type, rows[0])), list(map(type, expected[0])))
@with_cursor
def test_cancel(self, cursor):
cursor.execute(
"SELECT a.a * rand(), b.a * rand()"
"FROM many_rows a "
"CROSS JOIN many_rows b "
)
self.assertIn(cursor.poll()['stats']['state'], (
'STARTING', 'PLANNING', 'RUNNING', 'WAITING_FOR_RESOURCES', 'QUEUED'))
cursor.cancel()
self.assertIsNotNone(cursor.last_query_id)
self.assertIsNone(cursor.poll())
def test_noops(self):
"""The DB-API specification requires that certain actions exist, even though they might not
be applicable."""
# Wohoo inflating coverage stats!
connection = self.connect()
cursor = connection.cursor()
self.assertEqual(cursor.rowcount, -1)
cursor.setinputsizes([])
cursor.setoutputsize(1, 'blah')
self.assertIsNone(cursor.last_query_id)
connection.commit()
@mock.patch('requests.post')
def test_non_200(self, post):
cursor = self.connect().cursor()
post.return_value.status_code = 404
self.assertRaises(exc.OperationalError, lambda: cursor.execute('show tables'))
@with_cursor
def test_poll(self, cursor):
self.assertRaises(presto.ProgrammingError, cursor.poll)
cursor.execute('SELECT * FROM one_row')
while True:
status = cursor.poll()
if status is None:
break
self.assertIn('stats', status)
def fail(*args, **kwargs):
self.fail("Should not need requests.get after done polling") # pragma: no cover
with mock.patch('requests.get', fail):
self.assertEqual(cursor.fetchall(), [(1,)])
@with_cursor
def test_set_session(self, cursor):
id = None
self.assertIsNone(cursor.last_query_id)
cursor.execute("SET SESSION query_max_run_time = '1234m'")
self.assertIsNotNone(cursor.last_query_id)
id = cursor.last_query_id
cursor.fetchall()
self.assertEqual(id, cursor.last_query_id)
cursor.execute('SHOW SESSION')
self.assertIsNotNone(cursor.last_query_id)
self.assertNotEqual(id, cursor.last_query_id)
id = cursor.last_query_id
rows = [r for r in cursor.fetchall() if r[0] == 'query_max_run_time']
self.assertEqual(len(rows), 1)
session_prop = rows[0]
self.assertEqual(session_prop[1], '1234m')
self.assertEqual(id, cursor.last_query_id)
cursor.execute('RESET SESSION query_max_run_time')
self.assertIsNotNone(cursor.last_query_id)
self.assertNotEqual(id, cursor.last_query_id)
id = cursor.last_query_id
cursor.fetchall()
self.assertEqual(id, cursor.last_query_id)
cursor.execute('SHOW SESSION')
self.assertIsNotNone(cursor.last_query_id)
self.assertNotEqual(id, cursor.last_query_id)
id = cursor.last_query_id
rows = [r for r in cursor.fetchall() if r[0] == 'query_max_run_time']
self.assertEqual(len(rows), 1)
session_prop = rows[0]
self.assertNotEqual(session_prop[1], '1234m')
self.assertEqual(id, cursor.last_query_id)
def test_set_session_in_constructor(self):
conn = presto.connect(
host=_HOST, source=self.id(), session_props={'query_max_run_time': '1234m'}
)
with contextlib.closing(conn):
with contextlib.closing(conn.cursor()) as cursor:
cursor.execute('SHOW SESSION')
rows = [r for r in cursor.fetchall() if r[0] == 'query_max_run_time']
assert len(rows) == 1
session_prop = rows[0]
assert session_prop[1] == '1234m'
cursor.execute('RESET SESSION query_max_run_time')
cursor.fetchall()
cursor.execute('SHOW SESSION')
rows = [r for r in cursor.fetchall() if r[0] == 'query_max_run_time']
assert len(rows) == 1
session_prop = rows[0]
assert session_prop[1] != '1234m'
def test_invalid_protocol_config(self):
"""protocol should be https when passing password"""
self.assertRaisesRegexp(
ValueError, 'Protocol.*https.*password', lambda: presto.connect(
host=_HOST, username='user', password='secret', protocol='http').cursor()
)
def test_invalid_password_and_kwargs(self):
"""password and requests_kwargs authentication are incompatible"""
self.assertRaisesRegexp(
ValueError, 'Cannot use both', lambda: presto.connect(
host=_HOST, username='user', password='secret', protocol='https',
requests_kwargs={'auth': requests.auth.HTTPBasicAuth('user', 'secret')}
).cursor()
)
def test_invalid_kwargs(self):
"""some kwargs are reserved"""
self.assertRaisesRegexp(
ValueError, 'Cannot override', lambda: presto.connect(
host=_HOST, username='user', requests_kwargs={'url': 'test'}
).cursor()
)
def test_requests_kwargs(self):
connection = presto.connect(
host=_HOST, port=_PORT, source=self.id(),
requests_kwargs={'proxies': {'http': 'localhost:9999'}},
)
cursor = connection.cursor()
self.assertRaises(requests.exceptions.ProxyError,
lambda: cursor.execute('SELECT * FROM one_row'))
def test_requests_session(self):
with requests.Session() as session:
connection = presto.connect(
host=_HOST, port=_PORT, source=self.id(), requests_session=session
)
cursor = connection.cursor()
cursor.execute('SELECT * FROM one_row')
self.assertEqual(cursor.fetchall(), [(1,)])