Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty result #133

Merged
merged 2 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pydruid/db/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ def execute(self, operation, parameters=None):
# to consume the first row so that `description` is properly set, so
# let's consume it and insert it back.
results = self._stream_query(query)
first_row = next(results)
self._results = itertools.chain([first_row], results)
try:
first_row = next(results)
self._results = itertools.chain([first_row], results)
except StopIteration:
self._results = iter([])

return self

Expand Down
48 changes: 48 additions & 0 deletions tests/db/test_cursor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-

from collections import namedtuple
from mock import patch
import unittest

from requests.models import Response
from six import BytesIO

from pydruid.db.api import Cursor


class CursorTestSuite(unittest.TestCase):

@patch('requests.post')
def test_execute(self, requests_post_mock):
response = Response()
response.status_code = 200
response.raw = BytesIO(b'[{"name": "alice"}, {"name": "bob"}, {"name": "charlie"}]')
requests_post_mock.return_value = response
Row = namedtuple('Row', ['name'])

cursor = Cursor('http://example.com/')
cursor.execute('SELECT * FROM table')
result = cursor.fetchall()
expected = [
Row(name='alice'),
Row(name='bob'),
Row(name='charlie'),
]
self.assertEquals(result, expected)

@patch('requests.post')
def test_execute_empty_result(self, requests_post_mock):
response = Response()
response.status_code = 200
response.raw = BytesIO(b'[]')
requests_post_mock.return_value = response

cursor = Cursor('http://example.com/')
cursor.execute('SELECT * FROM table')
result = cursor.fetchall()
expected = []
self.assertEquals(result, expected)


if __name__ == '__main__':
unittest.main()