Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 271c4bd

Browse files
authored
Support new view parameters (#388)
* Support new view parameters: `stable` and `update` * Copyrights * Grammar (full stop) * Add deprecation note.
1 parent 0369755 commit 271c4bd

6 files changed

Lines changed: 81 additions & 11 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [NEW] Added functionality to test if a key is in a database as in `key in db`, overriding dict `__contains__` and checking in the remote database.
66
- [NEW] Moved `create_query_index` and other query related methods to `CouchDatabase` as the `_index`/`_find` API is available in CouchDB 2.x.
77
- [NEW] Support IAM authentication in replication documents.
8+
- [NEW] Add new view parameters, `stable` and `update`, as keyword arguments to `get_view_result`.
89
- [FIXED] Case where `Document` context manager would throw instead of creating a new document if no `_id` was provided.
910
- [IMPROVED] Added support for IAM API key in `cloudant_bluemix` method.
1011
- [IMPROVED] Shortened length of client URLs by removing username and password.

src/cloudant/_common_util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@
6060
'limit': (int, LONGTYPE, NONETYPE,),
6161
'reduce': (bool,),
6262
'skip': (int, LONGTYPE, NONETYPE,),
63+
'stable': (bool,),
6364
'stale': (STRTYPE,),
6465
'startkey': (int, LONGTYPE, STRTYPE, Sequence,),
6566
'startkey_docid': (STRTYPE,),
67+
'update': (STRTYPE,),
6668
}
6769

6870
# pylint: disable=unnecessary-lambda
@@ -196,7 +198,7 @@ def _py_to_couch_translate(key, val):
196198
equivalent.
197199
"""
198200
try:
199-
if key in ['keys', 'endkey_docid', 'startkey_docid', 'stale']:
201+
if key in ['keys', 'endkey_docid', 'startkey_docid', 'stale', 'update']:
200202
return {key: val}
201203
elif val is None:
202204
return {key: None}

src/cloudant/database.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,24 @@ def get_view_result(self, ddoc_id, view_name, raw_result=False, **kwargs):
312312
:param bool reduce: True to use the reduce function, false otherwise.
313313
:param int skip: Skip this number of rows from the start.
314314
Not valid when used with :class:`~cloudant.result.Result` iteration.
315+
:param bool stable: Whether or not the view results should be returned
316+
from a "stable" set of shards.
315317
:param str stale: Allow the results from a stale view to be used. This
316318
makes the request return immediately, even if the view has not been
317319
completely built yet. If this parameter is not given, a response is
318-
returned only after the view has been built.
320+
returned only after the view has been built. Note that this
321+
parameter is deprecated and the appropriate combination of `stable`
322+
and `update` should be used instead.
319323
:param startkey: Return records starting with the specified key.
320324
Not valid when used with :class:`~cloudant.result.Result` key
321325
access and key slicing.
322326
:param str startkey_docid: Return records starting with the specified
323327
document ID.
328+
:param str update: Determine whether the view in question should be
329+
updated prior to or after responding to the user. Valid values are:
330+
false: return results before updating the view; true: Return results
331+
after updating the view; lazy: Return the view results without
332+
waiting for an update, but update them immediately after the request.
324333
325334
:returns: The result content either wrapped in a QueryResult or
326335
as the raw response JSON content

src/cloudant/result.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# Copyright (c) 2015 IBM. All rights reserved.
2+
# Copyright (C) 2015, 2018 IBM Corp. All rights reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -150,14 +150,24 @@ class Result(object):
150150
:param bool reduce: True to use the reduce function, false otherwise.
151151
:param int skip: Skip this number of rows from the start.
152152
Not valid when used with key iteration.
153-
:param str stale: Allow the results from a stale view to be used. This
154-
makes the request return immediately, even if the view has not been
155-
completely built yet. If this parameter is not given, a response is
156-
returned only after the view has been built.
153+
:param bool stable: Whether or not the view results should be returned from
154+
a "stable" set of shards.
155+
:param str stale: Allow the results from a stale view to be used. This makes
156+
the request return immediately, even if the view has not been completely
157+
built yet. If this parameter is not given, a response is returned only
158+
after the view has been built. Note that this parameter is deprecated
159+
and the appropriate combination of `stable` and `update` should be used
160+
instead.
157161
:param startkey: Return records starting with the specified key.
158162
Not valid when used with key access and key slicing.
159163
:param str startkey_docid: Return records starting with the specified
160164
document ID.
165+
:param str update: Determine whether the view in question should be
166+
updated prior to or after responding to the user. Valid values are:
167+
false: return results before updating the view; true: Return results
168+
after updating the view; lazy: Return the view results without
169+
waiting for an update, but update them immediately after the request.
170+
161171
"""
162172
def __init__(self, method_ref, **options):
163173
self.options = options

tests/unit/param_translation_tests.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# Copyright (c) 2016 IBM. All rights reserved.
2+
# Copyright (C) 2016, 2018 IBM Corp. All rights reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -215,6 +215,14 @@ def test_valid_startkey_docid(self):
215215
{'startkey_docid': 'foo'}
216216
)
217217

218+
def test_valid_update(self):
219+
"""
220+
Test lazy translation is successful.
221+
"""
222+
self.assertEqual(python_to_couch({'update': 'true'}), {'update': 'true'})
223+
self.assertEqual(python_to_couch({'update': 'false'}), {'update': 'false'})
224+
self.assertEqual(python_to_couch({'update': 'lazy'}), {'update': 'lazy'})
225+
218226
def test_invalid_argument(self):
219227
"""
220228
Test translation fails when an invalid argument is passed in.

tests/unit/view_execution_tests.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# Copyright (c) 2016 IBM. All rights reserved.
2+
# Copyright (C) 2016, 2018 IBM Corp. All rights reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -467,7 +467,7 @@ def test_stale_ok(self):
467467
try:
468468
self.view001(stale='ok')
469469
except Exception as err:
470-
self.assertFail(str(err), 'An unexpected error was encountered.')
470+
self.fail('An unexpected error was encountered: '+str(err))
471471

472472
def test_stale_update_after(self):
473473
"""
@@ -480,8 +480,48 @@ def test_stale_update_after(self):
480480
try:
481481
self.view001(stale='update_after')
482482
except Exception as err:
483-
self.assertFail(str(err), 'An unexpected error was encountered.')
483+
self.fail('An unexpected error was encountered:' +str(err))
484+
485+
def test_stable_true(self):
486+
"""
487+
Test view query using the stable parameter set to true
488+
489+
490+
Since there is no way to know whether the view will return a response from a stable set of
491+
shards or not the test here focuses on ensuring that the call itself is successful.
492+
493+
"""
494+
try:
495+
self.view001(stable=True)
496+
except Exception as err:
497+
self.fail('An unexpected error was encountered: '+str(err))
498+
499+
def test_stable_update_lazy(self):
500+
"""
501+
Test view query using the update parameter set to lazy
484502
503+
Since there is no way to know whether the view will update lazily or not the test here
504+
focuses on ensuring that the call itself is successful.
505+
506+
"""
507+
try:
508+
self.view001(update='lazy')
509+
except Exception as err:
510+
self.fail('An unexpected error was encountered: '+str(err))
511+
512+
def test_stable_update_true(self):
513+
"""
514+
Test view query using the update parameter set to true
515+
516+
Since there is no way to know whether the view will update or not the test here focuses on
517+
ensuring that the call itself is successful.
518+
519+
"""
520+
try:
521+
self.view001(update='true')
522+
except Exception as err:
523+
self.fail('An unexpected error was encountered: '+str(err))
524+
485525
def test_startkey_int(self):
486526
"""
487527
Test view query using startkey parameter as an integer.

0 commit comments

Comments
 (0)