This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathparam_translation_tests.py
400 lines (363 loc) · 14 KB
/
param_translation_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env python
# Copyright (C) 2016, 2018 IBM Corp. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Unit tests for Python to CouchDB translation of query parameters.
"""
import unittest
from cloudant.error import CloudantArgumentError
from cloudant._common_util import python_to_couch
from tests.unit._test_util import LONG_NUMBER
class PythonToCouchTests(unittest.TestCase):
"""
Test cases for validating python_to_couch translation functionality
"""
def test_valid_descending(self):
"""
Test descending translation is successful.
"""
self.assertEqual(
python_to_couch({'descending': True}),
{'descending': 'true'}
)
self.assertEqual(
python_to_couch({'descending': False}),
{'descending': 'false'}
)
def test_valid_endkey(self):
"""
Test endkey translation is successful.
"""
self.assertEqual(python_to_couch({'endkey': 10}), {'endkey': '10'})
# Test with long type
self.assertEqual(python_to_couch({'endkey': LONG_NUMBER}), {'endkey': str(LONG_NUMBER)})
self.assertEqual(
python_to_couch({'endkey': 'foo'}),
{'endkey': '"foo"'}
)
self.assertEqual(
python_to_couch({'endkey': ['foo', 10]}),
{'endkey': '["foo", 10]'}
)
self.assertEqual(
python_to_couch({'endkey': True}),
{'endkey': 'true'}
)
def test_valid_endkey_docid(self):
"""
Test endkey_docid translation is successful.
"""
self.assertEqual(
python_to_couch({'endkey_docid': 'foo'}),
{'endkey_docid': 'foo'}
)
def test_valid_group(self):
"""
Test group translation is successful.
"""
self.assertEqual(python_to_couch({'group': True}), {'group': 'true'})
self.assertEqual(python_to_couch({'group': False}), {'group': 'false'})
def test_valid_group_level(self):
"""
Test group_level translation is successful.
"""
self.assertEqual(
python_to_couch({'group_level': 100}),
{'group_level': 100}
)
# Test with long type
self.assertEqual(
python_to_couch({'group_level': LONG_NUMBER}),
{'group_level': LONG_NUMBER}
)
self.assertEqual(
python_to_couch({'group_level': None}),
{'group_level': None}
)
def test_valid_include_docs(self):
"""
Test include_docs translation is successful.
"""
self.assertEqual(
python_to_couch({'include_docs': True}),
{'include_docs': 'true'}
)
self.assertEqual(
python_to_couch({'include_docs': False}),
{'include_docs': 'false'}
)
def test_valid_inclusive_end(self):
"""
Test inclusive_end translation is successful.
"""
self.assertEqual(
python_to_couch({'inclusive_end': True}),
{'inclusive_end': 'true'}
)
self.assertEqual(
python_to_couch({'inclusive_end': False}),
{'inclusive_end': 'false'}
)
def test_valid_key(self):
"""
Test key translation is successful.
"""
self.assertEqual(python_to_couch({'key': 10}), {'key': '10'})
# Test with long type
self.assertEqual(python_to_couch({'key': LONG_NUMBER}), {'key': str(LONG_NUMBER)})
self.assertEqual(python_to_couch({'key': 'foo'}), {'key': '"foo"'})
self.assertEqual(
python_to_couch({'key': ['foo', 10]}),
{'key': '["foo", 10]'}
)
self.assertEqual(
python_to_couch({'key': True}),
{'key': 'true'}
)
def test_valid_keys(self):
"""
Test keys translation is successful.
"""
self.assertEqual(
python_to_couch({'keys': [100, 200]}),
{'keys': [100, 200]}
)
# Test with long type
LONG_NUM_KEY = 92233720368547758071
self.assertEqual(
python_to_couch({'keys': [LONG_NUMBER, LONG_NUM_KEY]}),
{'keys': [LONG_NUMBER, LONG_NUM_KEY]}
)
self.assertEqual(
python_to_couch({'keys': ['foo', 'bar']}),
{'keys': ['foo', 'bar']}
)
self.assertEqual(
python_to_couch({'keys': [['foo', 100], ['bar', 200]]}),
{'keys': [['foo', 100], ['bar', 200]]}
)
def test_valid_limit(self):
"""
Test limit translation is successful.
"""
self.assertEqual(python_to_couch({'limit': 100}), {'limit': 100})
# Test with long type
self.assertEqual(python_to_couch({'limit': LONG_NUMBER}), {'limit': LONG_NUMBER})
self.assertEqual(python_to_couch({'limit': None}), {'limit': None})
def test_valid_reduce(self):
"""
Test reduce translation is successful.
"""
self.assertEqual(python_to_couch({'reduce': True}), {'reduce': 'true'})
self.assertEqual(
python_to_couch({'reduce': False}),
{'reduce': 'false'}
)
def test_valid_skip(self):
"""
Test skip translation is successful.
"""
self.assertEqual(python_to_couch({'skip': 100}), {'skip': 100})
# Test with long type
self.assertEqual(python_to_couch({'skip': LONG_NUMBER}), {'skip': LONG_NUMBER})
self.assertEqual(python_to_couch({'skip': None}), {'skip': None})
def test_valid_stale(self):
"""
Test stale translation is successful.
"""
self.assertEqual(python_to_couch({'stale': 'ok'}), {'stale': 'ok'})
self.assertEqual(
python_to_couch({'stale': 'update_after'}),
{'stale': 'update_after'}
)
def test_valid_startkey(self):
"""
Test startkey translation is successful.
"""
self.assertEqual(python_to_couch({'startkey': 10}), {'startkey': '10'})
# Test with long type
self.assertEqual(python_to_couch({'startkey': LONG_NUMBER}), {'startkey': str(LONG_NUMBER)})
self.assertEqual(
python_to_couch({'startkey': 'foo'}),
{'startkey': '"foo"'}
)
self.assertEqual(
python_to_couch({'startkey': ['foo', 10]}),
{'startkey': '["foo", 10]'}
)
self.assertEqual(
python_to_couch({'startkey': True}),
{'startkey': 'true'}
)
def test_valid_startkey_docid(self):
"""
Test startkey_docid translation is successful.
"""
self.assertEqual(
python_to_couch({'startkey_docid': 'foo'}),
{'startkey_docid': 'foo'}
)
def test_valid_update(self):
"""
Test lazy translation is successful.
"""
self.assertEqual(python_to_couch({'update': 'true'}), {'update': 'true'})
self.assertEqual(python_to_couch({'update': 'false'}), {'update': 'false'})
self.assertEqual(python_to_couch({'update': 'lazy'}), {'update': 'lazy'})
def test_invalid_argument(self):
"""
Test translation fails when an invalid argument is passed in.
"""
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'foo': 'bar'})
self.assertEqual(str(cm.exception), 'Invalid argument foo')
def test_invalid_descending(self):
"""
Test descending translation fails when a non-bool value is used.
"""
msg = 'Argument descending not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'descending': 10})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_endkey(self):
"""
Test endkey translation fails when a non-string or a non-list value is
used.
"""
msg = 'Argument endkey not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'endkey': {'foo': 'bar'}})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_endkey_docid(self):
"""
Test endkey_docid translation fails when a non-string value is used.
"""
msg = 'Argument endkey_docid not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'endkey_docid': 10})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_group(self):
"""
Test group translation fails when a non-bool value is used.
"""
msg = 'Argument group not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'group': 10})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_group_level(self):
"""
Test group_level translation fails when a non-integer value is used.
"""
msg = 'Argument group_level not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'group_level': True})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_include_docs(self):
"""
Test include_docs translation fails when a non-bool value is used.
"""
msg = 'Argument include_docs not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'include_docs': 10})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_inclusive_end(self):
"""
Test inclusive_end translation fails when a non-bool value is used.
"""
msg = 'Argument inclusive_end not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'inclusive_end': 10})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_key(self):
"""
Test key translation fails when a non-string or a non-list value is
used.
"""
msg = 'Argument key not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'key': {'foo': 'bar'}})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_keys_not_list(self):
"""
Test keys translation fails when a non-list value is used.
"""
msg = 'Argument keys not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'keys': 'foo'})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_keys_invalid_key(self):
"""
Test keys translation fails when a key value used in the key list is
not a valid value.
"""
msg = 'Key list element not of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'keys': ['foo', True, 'bar']})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_limit(self):
"""
Test limit translation fails when a non-integer value is used.
"""
msg = 'Argument limit not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'limit': True})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_reduce(self):
"""
Test reduce translation fails when a non-bool value is used.
"""
msg = 'Argument reduce not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'reduce': 10})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_skip(self):
"""
Test skip translation fails when a non-integer value is used.
"""
msg = 'Argument skip not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'skip': True})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_stale(self):
"""
Test stale translation fails when the value is not either
'ok' or 'update_after' is used.
"""
msg = 'Argument stale not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'stale': 10})
self.assertTrue(str(cm.exception).startswith(msg))
msg = 'Invalid value for stale option foo'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'stale': 'foo'})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_startkey(self):
"""
Test startkey translation fails when a non-string or a non-list value
is used.
"""
msg = 'Argument startkey not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'startkey': {'foo': 'bar'}})
self.assertTrue(str(cm.exception).startswith(msg))
def test_invalid_startkey_docid(self):
"""
Test startkey_docid translation fails when a non-string value is used.
"""
msg = 'Argument startkey_docid not instance of expected type:'
with self.assertRaises(CloudantArgumentError) as cm:
python_to_couch({'startkey_docid': 10})
self.assertTrue(str(cm.exception).startswith(msg))
if __name__ == '__main__':
unittest.main()