def test_next_page(self, mock_session, mock_executor): mock_page1 = { 'Values': [{ 'IonBinary': 1 }, { 'IonBinary': 2 }], 'NextPageToken': 'token' } mock_page2 = { 'Values': [{ 'IonBinary': 2 }, { 'IonBinary': 3 }], 'NextPageToken': None } mock_statement_result1 = {'Page': mock_page1} mock_statement_result2 = {'Page': mock_page2} mock_session.return_value = None mock_session._fetch_page.return_value = mock_statement_result2 read_ahead_cursor = ReadAheadCursor(mock_statement_result1, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = Queue() read_ahead_cursor._queue.put(mock_statement_result2) read_ahead_cursor._next_page() self.assertEqual(read_ahead_cursor._page, mock_page2) self.assertEqual(read_ahead_cursor._index, 0)
def test_next_page_client_error(self, mock_session, mock_executor): mock_session.return_value = None mock_session._fetch_page.return_value = {'Page': MOCK_STATEMENT_RESULT} read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = Queue() read_ahead_cursor._queue.put( ClientError(MOCK_CLIENT_ERROR_MESSAGE, MOCK_MESSAGE)) self.assertRaises(ClientError, read_ahead_cursor._next_page)
def test_next_with_next_page_returns_empty_values_and_not_none_token( self, mock_session, mock_executor): read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = Queue() read_ahead_cursor._queue.put({'NextPageToken': 'token', 'Values': []}) read_ahead_cursor._queue.put({'NextPageToken': None, 'Values': []}) read_ahead_cursor._index = len(MOCK_ION_BINARY_VALUES) self.assertRaises(StopIteration, next, read_ahead_cursor)
def test_next_page(self, mock_session, mock_executor): mock_session.return_value = None mock_session._fetch_page.return_value = {'Page': MOCK_STATEMENT_RESULT} read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = Queue() read_ahead_cursor._queue.put(MOCK_STATEMENT_RESULT) read_ahead_cursor._next_page() self.assertEqual(read_ahead_cursor._page, MOCK_STATEMENT_RESULT) self.assertEqual(read_ahead_cursor._index, 0)
def test_are_there_more_results(self, mock_session, mock_executor): mock_session.return_value = None read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._page = {'NextPageToken': 'token', 'Values': []} self.assertTrue(read_ahead_cursor._are_there_more_results()) read_ahead_cursor._page = {'NextPageToken': None, 'Values': []} read_ahead_cursor._queue = Queue() self.assertFalse(read_ahead_cursor._are_there_more_results())
def test_populate_queue(self, mock_queue, mock_session, mock_executor): mock_session.return_value = None mock_queue.return_value = mock_queue mock_page = {'NextPageToken': None, 'Values': []} mock_session._fetch_page.return_value = {'Page': mock_page} mock_queue.full.return_value = False read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = mock_queue read_ahead_cursor._populate_queue() mock_session._fetch_page.assert_called_once_with( MOCK_TRANSACTION_ID, MOCK_STATEMENT_RESULT.get('NextPageToken')) mock_queue.put.assert_called_once_with(mock_page, timeout=0.05)
def test_populate_queue_client_error(self, mock_session, mock_logger_debug, mock_executor): mock_logger_debug.return_value = None mock_session.return_value = None mock_session._fetch_page.side_effect = ClientError( MOCK_CLIENT_ERROR_MESSAGE, MOCK_MESSAGE) read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = Queue(1) read_ahead_cursor._queue.put('value to be removed') read_ahead_cursor._populate_queue() mock_logger_debug.assert_called_once() self.assertIsInstance(read_ahead_cursor._queue.get(), ClientError) self.assertEqual(read_ahead_cursor._queue.qsize(), 0)
def test_populate_queue(self, mock_queue, mock_session, mock_executor): mock_session.return_value = None mock_queue.return_value = mock_queue mock_session._fetch_page.return_value = MOCK_FETCH_PAGE_RESULT_WITHOUT_TOKEN mock_queue.full.return_value = False read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = mock_queue read_ahead_cursor._populate_queue() next_page_token = MOCK_STATEMENT_RESULT.get('FirstPage').get( 'NextPageToken') mock_session._fetch_page.assert_called_once_with( MOCK_TRANSACTION_ID, next_page_token) mock_queue.put.assert_called_once_with( MOCK_FETCH_PAGE_RESULT_WITHOUT_TOKEN, timeout=0.05)
def test_populate_queue_result_closed_error(self, mock_session, mock_logger_debug, mock_executor): def close_parent_txn(txn_id, token): read_ahead_cursor._is_open = False return MOCK_STATEMENT_RESULT mock_logger_debug.return_value = None mock_session.return_value = None mock_session._fetch_page.side_effect = close_parent_txn read_ahead_cursor = ReadAheadCursor(MOCK_STATEMENT_RESULT, mock_session, MOCK_TRANSACTION_ID, MOCK_READ_AHEAD, mock_executor) read_ahead_cursor._queue = Queue(1) read_ahead_cursor._queue.put('value to be removed') read_ahead_cursor._populate_queue() self.assertEqual(mock_logger_debug.call_count, 2) self.assertIsInstance(read_ahead_cursor._queue.get(), ResultClosedError) self.assertEqual(read_ahead_cursor._queue.qsize(), 0)