Browse files

Database: Accept callback as keyword argument in Database.set

To support tornado.gen mechanism, we'll now on accept callback as a
keyword argument for Database.set.

Fixes GH-34
  • Loading branch information...
1 parent 440102d commit b0a45d2a70548406ad30fb61508911c37425644d @nailor nailor committed Nov 28, 2011
Showing with 25 additions and 7 deletions.
  1. +7 −6 doc/python-api.rst
  2. +14 −0 test/test_client.py
  3. +4 −1 trombi/client.py
View
13 doc/python-api.rst
@@ -264,12 +264,13 @@ argument.
.. method:: set([doc_id, ]data, callback[, attachments=None])
Creates a new or modifies an existing document in the database.
- If called with three arguments, the first argument, *doc_id* is
- the document id of the new or existing document. If only two
- arguments are given the document id is generated by the
- database. *data* is the data to the document, either a Python
- :class:`dict` or an instance of :class:`Document`.
- *doc_id* can be omitted if *data* is an existing document.
+ If called with two positional arguments, the first argument,
+ *doc_id* is the document id of the new or existing document. If
+ only one positional argument is given the document id is
+ generated by the database. *data* is the data to the document,
+ either a Python :class:`dict` or an instance of
+ :class:`Document`. *doc_id* can be omitted if *data* is an
+ existing document.
This method makes distinction between creating a new document
and updating an existing by inspecting the *data* argument. If
View
14 test/test_client.py
@@ -685,6 +685,20 @@ def check_original(doc):
@with_ioloop
@with_couchdb
+def test_set_document_with_kw_callback(baseurl, ioloop):
+ def do_test(db):
+ def update_doc(doc):
+ ioloop.stop()
+
+ db.set('testid', {'testvalue': 'something'}, callback=update_doc)
+
+ s = trombi.Server(baseurl, io_loop=ioloop)
+ s.create('testdb', do_test)
+ ioloop.start()
+
+
+@with_ioloop
+@with_couchdb
def test_get_document_does_not_exist(baseurl, ioloop):
def create_db_callback(db):
db.get('foo', callback=get_callback)
View
5 trombi/client.py
@@ -331,14 +331,17 @@ def _really_callback(response):
self._fetch('', _really_callback)
def set(self, *args, **kwargs):
+ cb = kwargs.pop('callback', None)
+ if cb:
+ args += (cb,)
if len(args) == 2:
data, callback = args
doc_id = None
elif len(args) == 3:
doc_id, data, callback = args
else:
raise TypeError(
- 'Database.set expected 2 or 3 arguments, got %d' % len(args))
+ 'Database.set takes at most 2 non-keyword arguments.')
if kwargs:
if list(kwargs.keys()) != ['attachments']:

0 comments on commit b0a45d2

Please sign in to comment.