Skip to content

Commit

Permalink
Update UT with the last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed May 29, 2019
1 parent 587386b commit f5ee1f4
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 24 deletions.
6 changes: 6 additions & 0 deletions python-shaptools.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Wed May 29 12:26:08 UTC 2019 - Ayoub Belarbi (abelarbi@suse.com)

- Update hdb connector to return metadata besides the query
records.

-------------------------------------------------------------------
Thu May 16 09:35:41 UTC 2019 - Xabier Arbulu Insausti <xarbulu@suse.com>

Expand Down
6 changes: 3 additions & 3 deletions shaptools/hdb_connector/connectors/base_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def load_cursor(cls, cursor):
Args:
cursor (obj): Cursor object created by the connector (dbapi or pydhb)
"""
instance = cls()
instance.records = cursor.fetchall() # TODO catch any exceptions raised by fetchall()
instance.metadata = cursor.description
records = cursor.fetchall() # TODO: catch any exceptions raised by fetchall()
metadata = cursor.description
instance = cls(records, metadata)
instance._logger.info('query records: %s', instance.records)
return instance

Expand Down
4 changes: 3 additions & 1 deletion shaptools/hdb_connector/connectors/pyhdb_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ def query(self, sql_statement):
"""
self._logger.info('executing sql query: %s', sql_statement)
try:
cursor = None
cursor = self._connection.cursor()
cursor.execute(sql_statement)
result = base_connector.QueryResult.load_cursor(cursor)
except pyhdb.exceptions.DatabaseError as err:
raise base_connector.QueryError('query failed: {}'.format(err))
finally:
cursor.close()
if cursor:
cursor.close()
return result

def disconnect(self):
Expand Down
44 changes: 43 additions & 1 deletion tests/hdb_connector/base_connect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,51 @@
import mock


class TestQueryResul(unittest.TestCase):
"""
Unitary tests for base_connector.py QueryResult class
"""

@classmethod
def setUpClass(cls):
"""
Global setUp.
"""

logging.basicConfig(level=logging.INFO)
from shaptools.hdb_connector.connectors import base_connector
cls._base_connector = base_connector

def setUp(self):
"""
Test setUp.
"""

def tearDown(self):
"""
Test tearDown.
"""

@classmethod
def tearDownClass(cls):
"""
Global tearDown.
"""

@mock.patch('logging.Logger.info')
def test_load_cursor(self, logger):
mock_cursor = mock.Mock()
mock_cursor.description = 'metadata'
mock_cursor.fetchall.return_value = ['data1', 'data2']
result = self._base_connector.QueryResult.load_cursor(mock_cursor)
logger.assert_called_once_with('query records: %s', ['data1', 'data2'])
self.assertEqual(result.records, ['data1', 'data2'])
self.assertEqual(result.metadata, 'metadata')


class TestHana(unittest.TestCase):
"""
Unitary tests for hana.py.
Unitary tests for base_connector.py BaseConnector class
"""

@classmethod
Expand Down
20 changes: 11 additions & 9 deletions tests/hdb_connector/dbapi_connector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,30 @@ def test_connect_error(self, mock_logger, mock_dbapi):
mock_logger.assert_called_once_with(
'connecting to SAP HANA database at %s:%s', 'host', 1234)

@mock.patch('shaptools.hdb_connector.connectors.base_connector.QueryResult')
@mock.patch('logging.Logger.info')
def test_query(self, mock_logger):
def test_query(self, mock_logger, mock_result):
cursor_mock_instance = mock.Mock()
cursor_mock = mock.Mock(return_value=cursor_mock_instance)
cursor_mock_instance.fetchall.return_value = 'result'
mock_result_inst = mock.Mock()
mock_result_inst.records = ['data1', 'data2']
mock_result_inst.metadata = 'metadata'
mock_result.load_cursor.return_value = mock_result_inst
context_manager_mock = mock.Mock(
__enter__ = cursor_mock,
__exit__ = mock.Mock()
)
self._conn._connection = mock.Mock()
self._conn._connection.cursor.return_value = context_manager_mock

response = self._conn.query('query')
result = self._conn.query('query')

cursor_mock_instance.execute.assert_called_once_with('query')
cursor_mock_instance.fetchall.assert_called_once_with()
mock_result.load_cursor.assert_called_once_with(cursor_mock_instance)

self.assertEqual(response.data, 'result')
mock_logger.assert_has_calls([
mock.call('executing sql query: %s', 'query'),
mock.call('query result: %s', 'result')
])
self.assertEqual(result.records, ['data1', 'data2'])
self.assertEqual(result.metadata, 'metadata')
mock_logger.assert_called_once_with('executing sql query: %s', 'query')

@mock.patch('shaptools.hdb_connector.connectors.dbapi_connector.dbapi')
@mock.patch('logging.Logger.info')
Expand Down
41 changes: 31 additions & 10 deletions tests/hdb_connector/pyhdb_connector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,58 @@ def test_connect_error(self, mock_logger, mock_pyhdb, mock_socket):
mock_logger.assert_called_once_with(
'connecting to SAP HANA database at %s:%s', 'host', 1234)

@mock.patch('shaptools.hdb_connector.connectors.base_connector.QueryResult')
@mock.patch('logging.Logger.info')
def test_query(self, mock_logger):
def test_query(self, mock_logger, mock_result):

mock_cursor = mock.Mock()
mock_cursor.fetchall.return_value = 'result'
self._conn._connection = mock.Mock()
self._conn._connection.cursor.return_value = mock_cursor

response = self._conn.query('query')
mock_result_inst = mock.Mock()
mock_result_inst.records = ['data1', 'data2']
mock_result_inst.metadata = 'metadata'
mock_result.load_cursor.return_value = mock_result_inst

result = self._conn.query('query')

mock_cursor.execute.assert_called_once_with('query')
mock_cursor.fetchall.assert_called_once_with()
mock_result.load_cursor.assert_called_once_with(mock_cursor)

self.assertEqual(response.data, 'result')
mock_logger.assert_has_calls([
mock.call('executing sql query: %s', 'query'),
mock.call('query result: %s', 'result')
])
self.assertEqual(result.records, ['data1', 'data2'])
self.assertEqual(result.metadata, 'metadata')
mock_logger.assert_called_once_with('executing sql query: %s', 'query')
mock_cursor.close.assert_called_once_with()

@mock.patch('shaptools.hdb_connector.connectors.pyhdb_connector.pyhdb')
@mock.patch('logging.Logger.info')
def test_query_error(self, mock_logger, mock_pyhdb):
mock_pyhdb.exceptions.DatabaseError = PyhdbException
self._conn._connection = mock.Mock()
self._conn._connection.cursor.side_effect = mock_pyhdb.exceptions.DatabaseError('error')
self._conn._connection.cursor.side_effect = PyhdbException('error')
with self.assertRaises(self._pyhdb_connector.base_connector.QueryError) as err:
self._conn.query('query')

self.assertTrue('query failed: {}'.format('error') in str(err.exception))
self._conn._connection.cursor.assert_called_once_with()
mock_logger.assert_called_once_with('executing sql query: %s', 'query')

@mock.patch('shaptools.hdb_connector.connectors.pyhdb_connector.pyhdb')
@mock.patch('logging.Logger.info')
def test_query_error_execute(self, mock_logger, mock_pyhdb):
mock_pyhdb.exceptions.DatabaseError = PyhdbException
self._conn._connection = mock.Mock()
cursor_mock = mock.Mock()
self._conn._connection.cursor.return_value = cursor_mock
cursor_mock.execute = mock.Mock()
cursor_mock.execute.side_effect = PyhdbException('error')
with self.assertRaises(self._pyhdb_connector.base_connector.QueryError) as err:
self._conn.query('query')

self.assertTrue('query failed: {}'.format('error') in str(err.exception))
self._conn._connection.cursor.assert_called_once_with()
mock_logger.assert_called_once_with('executing sql query: %s', 'query')
cursor_mock.close.assert_called_once_with()

@mock.patch('logging.Logger.info')
def test_disconnect(self, mock_logger):
Expand Down

0 comments on commit f5ee1f4

Please sign in to comment.