Skip to content

Commit e8fcacf

Browse files
committed
Return list from fetchall instead of generator
Fixes #8
1 parent ac19425 commit e8fcacf

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

postgres.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,25 +185,37 @@ def execute(self, sql, parameters=None):
185185
:param unicode sql: the SQL statement to execute
186186
:param parameters: the bind parameters for the SQL statement
187187
:type parameters: tuple or dict
188+
:returns: :py:const:`None`
188189
189190
"""
190191
with self.get_cursor() as cursor:
191192
cursor.execute(sql, parameters)
192193

193194
def fetchone(self, sql, parameters=None):
194-
"""Execute the query and return a single result (``dict`` or ``None``).
195+
"""Execute the query and return a single result.
196+
197+
:param unicode sql: the SQL statement to execute
198+
:param parameters: the bind parameters for the SQL statement
199+
:type parameters: tuple or dict
200+
:returns: :py:class:`dict` or :py:const:`None`
201+
195202
"""
196203
with self.get_cursor() as cursor:
197204
cursor.execute(sql, parameters)
198205
return cursor.fetchone()
199206

200207
def fetchall(self, sql, parameters=None):
201-
"""Execute the query and yield the results (``dict``).
208+
"""Execute the query and return all results.
209+
210+
:param unicode sql: the SQL statement to execute
211+
:param parameters: the bind parameters for the SQL statement
212+
:type parameters: tuple or dict
213+
:returns: :py:class:`list` of :py:class:`dict`
214+
202215
"""
203216
with self.get_cursor() as cursor:
204217
cursor.execute(sql, parameters)
205-
for row in cursor:
206-
yield row
218+
return list(cursor)
207219

208220
def get_cursor(self, *a, **kw):
209221
"""Return a :py:class:`~postgres.CursorContextManager` that uses our

tests.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class TestExecute(WithSchema):
3333

3434
def test_execute_executes(self):
3535
self.db.execute("CREATE TABLE foo (bar text)")
36-
actual = list(self.db.fetchall("SELECT tablename FROM pg_tables "
37-
"WHERE schemaname='public'"))
36+
actual = self.db.fetchall("SELECT tablename FROM pg_tables "
37+
"WHERE schemaname='public'")
3838
assert actual == [{"tablename": "foo"}]
3939

4040
def test_execute_inserts(self):
4141
self.db.execute("CREATE TABLE foo (bar text)")
4242
self.db.execute("INSERT INTO foo VALUES ('baz')")
43-
actual = len(list(self.db.fetchone("SELECT * FROM foo ORDER BY bar")))
43+
actual = len(self.db.fetchone("SELECT * FROM foo ORDER BY bar"))
4444
assert actual == 1
4545

4646

@@ -55,7 +55,7 @@ def test_fetchone_returns_None_if_theres_none(self):
5555
assert actual is None
5656

5757
def test_fetchall_fetches_all(self):
58-
actual = list(self.db.fetchall("SELECT * FROM foo ORDER BY bar"))
58+
actual = self.db.fetchall("SELECT * FROM foo ORDER BY bar")
5959
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
6060

6161

@@ -81,14 +81,14 @@ def test_transaction_is_isolated(self):
8181
with self.db.get_transaction() as txn:
8282
txn.execute("INSERT INTO foo VALUES ('blam')")
8383
txn.execute("SELECT * FROM foo ORDER BY bar")
84-
actual = list(self.db.fetchall("SELECT * FROM foo ORDER BY bar"))
84+
actual = self.db.fetchall("SELECT * FROM foo ORDER BY bar")
8585
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
8686

8787
def test_transaction_commits_on_success(self):
8888
with self.db.get_transaction() as txn:
8989
txn.execute("INSERT INTO foo VALUES ('blam')")
9090
txn.execute("SELECT * FROM foo ORDER BY bar")
91-
actual = list(self.db.fetchall("SELECT * FROM foo ORDER BY bar"))
91+
actual = self.db.fetchall("SELECT * FROM foo ORDER BY bar")
9292
assert actual == [{"bar": "baz"}, {"bar": "blam"}, {"bar": "buz"}]
9393

9494
def test_transaction_rolls_back_on_failure(self):
@@ -100,7 +100,7 @@ class Heck(Exception): pass
100100
raise Heck
101101
except Heck:
102102
pass
103-
actual = list(self.db.fetchall("SELECT * FROM foo ORDER BY bar"))
103+
actual = self.db.fetchall("SELECT * FROM foo ORDER BY bar")
104104
assert actual == [{"bar": "baz"}, {"bar": "buz"}]
105105

106106

0 commit comments

Comments
 (0)