Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Add aliases to register_(hstore|json) methods for use with NamedTuples #112

Merged
merged 2 commits into from
Aug 3, 2015
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
4 changes: 2 additions & 2 deletions momoko/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ def register_hstore(self, globally=False, unicode=False):
registrator = partial(_psy_register_hstore, None, globally, unicode)
callback = partial(self._register, future, registrator)
self.ioloop.add_future(self.execute(
"SELECT 'hstore'::regtype::oid, 'hstore[]'::regtype::oid",
"SELECT 'hstore'::regtype::oid AS hstore_oid, 'hstore[]'::regtype::oid AS hstore_arr_oid",
), callback)

return future
Expand Down Expand Up @@ -915,7 +915,7 @@ def register_json(self, globally=False, loads=None):
registrator = partial(_psy_register_json, None, globally, loads)
callback = partial(self._register, future, registrator)
self.ioloop.add_future(self.execute(
"SELECT 'json'::regtype::oid, 'json[]'::regtype::oid"
"SELECT 'json'::regtype::oid AS json_oid, 'json[]'::regtype::oid AS json_arr_oid"
), callback)

return future
Expand Down
14 changes: 14 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,20 @@ def test_connection_factory(self):
cursor = yield db.execute("SELECT 1 AS a")
self.assertEqual(cursor.fetchone(), {"a": 1})

@gen_test
def test_cursor_factory_with_extensions(self):
"""Testing that NamedTupleCursor factory is working with hstore and json"""
db = yield self.build_pool(cur_factory=NamedTupleCursor)

yield db.register_hstore()
yield db.register_json()

cursor = yield self.db.execute("SELECT 'a=>b, c=>d'::hstore;")
self.assertEqual(cursor.fetchall(), [({"a": "b", "c": "d"},)])

cursor = yield self.db.execute("SELECT %s;", ({'e': 'f', 'g': 'h'},))
self.assertEqual(cursor.fetchall(), [({"e": "f", "g": "h"},)])


class MomokoPoolParallelTest(PoolBaseTest):
pool_size = 1
Expand Down