Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit f0c0883

Browse filesBrowse files
authored
Use custom encoder (if provided) for all view key params not just keys (#501)
1 parent c2a3b3a commit f0c0883
Copy full SHA for f0c0883

File tree

Expand file treeCollapse file tree

4 files changed

+24
-11
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+24
-11
lines changed

‎CHANGES.md

Copy file name to clipboardExpand all lines: CHANGES.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [FIXED] Fixed result paging for grouped view queries.
66
- [FIXED] Incorrect use of username as account name in `Cloudant.bluemix()`.
77
- [IMPROVED] Documented use of None account name and url override for `Cloudant.iam()`.
8+
- [FIXED] Use custom encoder (if provided) for all view `key` params not just `keys`.
89

910
# 2.14.0 (2020-08-17)
1011

‎src/cloudant/_common_util.py

Copy file name to clipboardExpand all lines: src/cloudant/_common_util.py
+7-4Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def feed_arg_types(feed_type):
160160
return _COUCH_DB_UPDATES_ARG_TYPES
161161
return _CHANGES_ARG_TYPES
162162

163-
def python_to_couch(options):
163+
def python_to_couch(options, encoder=None):
164164
"""
165165
Translates query options from python style options into CouchDB/Cloudant
166166
query options. For example ``{'include_docs': True}`` will
@@ -171,13 +171,14 @@ def python_to_couch(options):
171171
:func:`~cloudant.view.View.__call__` callable, both used to retrieve data.
172172
173173
:param dict options: Python style parameters to be translated.
174+
:param encoder: Custom encoder, defaults to None
174175
175176
:returns: Dictionary of translated CouchDB/Cloudant query parameters
176177
"""
177178
translation = dict()
178179
for key, val in iteritems_(options):
179180
py_to_couch_validate(key, val)
180-
translation.update(_py_to_couch_translate(key, val))
181+
translation.update(_py_to_couch_translate(key, val, encoder))
181182
return translation
182183

183184
def py_to_couch_validate(key, val):
@@ -201,14 +202,16 @@ def py_to_couch_validate(key, val):
201202
if val not in ('ok', 'update_after'):
202203
raise CloudantArgumentError(135, val)
203204

204-
def _py_to_couch_translate(key, val):
205+
def _py_to_couch_translate(key, val, encoder=None):
205206
"""
206207
Performs the conversion of the Python parameter value to its CouchDB
207208
equivalent.
208209
"""
209210
try:
210211
if key in ['keys', 'endkey_docid', 'startkey_docid', 'stale', 'update']:
211212
return {key: val}
213+
if key in ['endkey', 'key', 'startkey']:
214+
return {key: json.dumps(val, cls=encoder)}
212215
if val is None:
213216
return {key: None}
214217
arg_converter = TYPE_CONVERTERS.get(type(val))
@@ -249,7 +252,7 @@ def get_docs(r_session, url, encoder=None, headers=None, **params):
249252
keys = None
250253
if keys_list is not None:
251254
keys = json.dumps({'keys': keys_list}, cls=encoder)
252-
f_params = python_to_couch(params)
255+
f_params = python_to_couch(params, encoder)
253256
resp = None
254257
if keys is not None:
255258
# If we're using POST we are sending JSON so add the header

‎tests/unit/database_tests.py

Copy file name to clipboardExpand all lines: tests/unit/database_tests.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,15 @@ def test_all_docs_get_with_long_type(self):
512512
data = self.db.all_docs(limit=1, skip=LONG_NUMBER)
513513
self.assertEqual(len(data.get('rows')), 1)
514514

515+
def test_all_docs_get_uses_custom_encoder(self):
516+
"""
517+
Test that all_docs uses the custom encoder.
518+
"""
519+
self.set_up_client(auto_connect=True, encoder="AEncoder")
520+
database = self.client[self.test_dbname]
521+
with self.assertRaises(CloudantArgumentError):
522+
database.all_docs(endkey=['foo', 10])
523+
515524
def test_custom_result_context_manager(self):
516525
"""
517526
Test using the database custom result context manager

‎tests/unit/param_translation_tests.py

Copy file name to clipboardExpand all lines: tests/unit/param_translation_tests.py
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ def test_valid_descending(self):
3636
{'descending': 'true'}
3737
)
3838
self.assertEqual(
39-
python_to_couch({'descending': False}),
39+
python_to_couch({'descending': False}),
4040
{'descending': 'false'}
4141
)
4242

4343
def test_valid_endkey(self):
4444
"""
4545
Test endkey translation is successful.
4646
"""
47-
self.assertEqual(python_to_couch({'endkey': 10}), {'endkey': 10})
47+
self.assertEqual(python_to_couch({'endkey': 10}), {'endkey': '10'})
4848
# Test with long type
49-
self.assertEqual(python_to_couch({'endkey': LONG_NUMBER}), {'endkey': LONG_NUMBER})
49+
self.assertEqual(python_to_couch({'endkey': LONG_NUMBER}), {'endkey': str(LONG_NUMBER)})
5050
self.assertEqual(
5151
python_to_couch({'endkey': 'foo'}),
5252
{'endkey': '"foo"'}
@@ -120,9 +120,9 @@ def test_valid_key(self):
120120
"""
121121
Test key translation is successful.
122122
"""
123-
self.assertEqual(python_to_couch({'key': 10}), {'key': 10})
123+
self.assertEqual(python_to_couch({'key': 10}), {'key': '10'})
124124
# Test with long type
125-
self.assertEqual(python_to_couch({'key': LONG_NUMBER}), {'key': LONG_NUMBER})
125+
self.assertEqual(python_to_couch({'key': LONG_NUMBER}), {'key': str(LONG_NUMBER)})
126126
self.assertEqual(python_to_couch({'key': 'foo'}), {'key': '"foo"'})
127127
self.assertEqual(
128128
python_to_couch({'key': ['foo', 10]}),
@@ -194,9 +194,9 @@ def test_valid_startkey(self):
194194
"""
195195
Test startkey translation is successful.
196196
"""
197-
self.assertEqual(python_to_couch({'startkey': 10}), {'startkey': 10})
197+
self.assertEqual(python_to_couch({'startkey': 10}), {'startkey': '10'})
198198
# Test with long type
199-
self.assertEqual(python_to_couch({'startkey': LONG_NUMBER}), {'startkey': LONG_NUMBER})
199+
self.assertEqual(python_to_couch({'startkey': LONG_NUMBER}), {'startkey': str(LONG_NUMBER)})
200200
self.assertEqual(
201201
python_to_couch({'startkey': 'foo'}),
202202
{'startkey': '"foo"'}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.