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

Commit

Permalink
Merge pull request #112 from helminster/master
Browse files Browse the repository at this point in the history
Add aliases to register_(hstore|json) methods for use with NamedTuples
  • Loading branch information
haizaar committed Aug 3, 2015
2 parents 99f69b9 + aa63fb4 commit f5f7b4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit f5f7b4c

Please sign in to comment.