Skip to content

Commit

Permalink
Add test for session properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jingw committed Mar 22, 2016
1 parent b177001 commit 7674d41
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pyhive/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class Connection(object):
def __init__(self, *args, **kwargs):
self._args = args
self._kwargs = kwargs
if 'session_props' not in kwargs:
kwargs['session_props'] = {}

This comment has been minimized.

Copy link
@klaussfreire

klaussfreire Nov 16, 2016

Contributor

This breaks SQLAlchemy

Session properties ought to be per-connection, not per-cursor. This is what this code was for, without this, "set session" doesn't work under SQLAlchemy becuase it won't reuse cursors.


def close(self):
"""Presto does not have anything to close"""
Expand Down Expand Up @@ -212,6 +210,9 @@ def _process_response(self, response):
assert self._state == self._STATE_RUNNING, "Should be running if processing response"
self._nextUri = response_json.get('nextUri')
self._columns = response_json.get('columns')
if 'X-Presto-Clear-Session' in response.headers:
propname = response.headers['X-Presto-Clear-Session']
self._session_props.pop(propname, None)
if 'X-Presto-Set-Session' in response.headers:
propname, propval = response.headers['X-Presto-Set-Session'].split('=', 1)
self._session_props[propname] = propval
Expand Down
44 changes: 44 additions & 0 deletions pyhive/tests/test_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

from __future__ import absolute_import
from __future__ import unicode_literals

import contextlib

from pyhive import exc
from pyhive import presto
from pyhive.tests.dbapi_test_case import DBAPITestCase
Expand Down Expand Up @@ -103,3 +106,44 @@ 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):
cursor.execute("SET SESSION query_max_run_time = '1234m'")
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'

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_set_session_in_consructor(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'

0 comments on commit 7674d41

Please sign in to comment.