Skip to content

Commit

Permalink
Fix empty result (#133)
Browse files Browse the repository at this point in the history
* Fix empty result

* Remove unused namedtuple
  • Loading branch information
betodealmeida authored and mistercrunch committed Jun 19, 2018
1 parent 6c6176e commit cbfcb7b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
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()

0 comments on commit cbfcb7b

Please sign in to comment.