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 pathresult_tests.py
683 lines (606 loc) · 28.1 KB
/
result_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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#!/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.
import mock
"""
result module - Unit tests for Result class
"""
import unittest
from cloudant.error import ResultException
from cloudant.result import Result, ResultByKey
from cloudant.view import View
from nose.plugins.attrib import attr
from requests.exceptions import HTTPError
from .unit_t_db_base import UnitTestDbBase
class ResultExceptionTests(unittest.TestCase):
"""
Ensure ResultException functions as expected.
"""
def test_raise_without_code(self):
"""
Ensure that a default exception/code is used if none is provided.
"""
with self.assertRaises(ResultException) as cm:
raise ResultException()
self.assertEqual(cm.exception.status_code, 100)
def test_raise_using_invalid_code(self):
"""
Ensure that a default exception/code is used if invalid code is provided.
"""
with self.assertRaises(ResultException) as cm:
raise ResultException('foo')
self.assertEqual(cm.exception.status_code, 100)
def test_raise_without_args(self):
"""
Ensure that a default exception/code is used if the message requested
by the code provided requires an argument list and none is provided.
"""
with self.assertRaises(ResultException) as cm:
raise ResultException(101)
self.assertEqual(cm.exception.status_code, 100)
def test_raise_without_insufficient_args(self):
"""
Ensure that a default exception/code is used if the message requested
by the code provided requires an argument list but the one provided
does not contain the correct amount of arguments.
"""
with self.assertRaises(ResultException) as cm:
raise ResultException(102, 'foo')
self.assertEqual(cm.exception.status_code, 100)
def test_raise_with_proper_code_and_args(self):
"""
Ensure that the requested exception is raised.
"""
with self.assertRaises(ResultException) as cm:
raise ResultException(102, 'foo', 'bar')
self.assertEqual(cm.exception.status_code, 102)
@attr(db=['cloudant','couch'])
class ResultTests(UnitTestDbBase):
"""
Result unit tests
"""
def setUp(self):
"""
Set up test attributes
"""
super(ResultTests, self).setUp()
self.db_set_up()
self.populate_db_with_documents()
self.create_views()
def tearDown(self):
"""
Reset test attributes
"""
self.db_tear_down()
super(ResultTests, self).tearDown()
def test_constructor(self):
"""
Test instantiating a Result
"""
result = Result(
self.ddoc.get_view('view001'),
startkey='1',
endkey='9',
page_size=1000
)
self.assertIsInstance(result, Result)
self.assertDictEqual(result.options, {'startkey': '1', 'endkey': '9'})
def test_get_item_by_index(self):
"""
Test retrieving a result using a value that refers to an index of the
result.
"""
result = Result(self.view001)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1}]
self.assertEqual(result[0], expected)
expected = [{'key': 'julia010', 'id': 'julia010', 'value': 1}]
self.assertEqual(result[10], expected)
expected = [{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result[99], expected)
self.assertEqual(result[100], [])
self.assertEqual(result[110], [])
def test_get_item_by_index_using_skip_limit(self):
"""
Test retrieving a result using a value that refers to an index of the
result when the result uses skip and limit.
"""
result = Result(self.view001, skip=10, limit=10)
expected = [{'key': 'julia010', 'id': 'julia010', 'value': 1}]
self.assertEqual(result[0], expected)
expected = [{'key': 'julia015', 'id': 'julia015', 'value': 1}]
self.assertEqual(result[5], expected)
expected = [{'key': 'julia019', 'id': 'julia019', 'value': 1}]
self.assertEqual(result[9], expected)
self.assertEqual(result[10], [])
self.assertEqual(result[20], [])
def test_get_item_by_index_using_limit(self):
"""
Test retrieving a result using a value that refers to an index of the
result when the result uses limit.
"""
result = Result(self.view001, limit=10)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1}]
self.assertEqual(result[0], expected)
expected = [{'key': 'julia005', 'id': 'julia005', 'value': 1}]
self.assertEqual(result[5], expected)
expected = [{'key': 'julia009', 'id': 'julia009', 'value': 1}]
self.assertEqual(result[9], expected)
self.assertEqual(result[10], [])
self.assertEqual(result[20], [])
def test_get_item_by_index_using_skip(self):
"""
Test retrieving a result using a value that refers to an index of the
result when the result uses limit.
"""
result = Result(self.view001, skip=10)
expected = [{'key': 'julia010', 'id': 'julia010', 'value': 1}]
self.assertEqual(result[0], expected)
expected = [{'key': 'julia015', 'id': 'julia015', 'value': 1}]
self.assertEqual(result[5], expected)
expected = [{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result[89], expected)
self.assertEqual(result[90], [])
self.assertEqual(result[100], [])
def test_get_item_by_negative_index(self):
"""
Test retrieving a result raises an exception when using a negative index.
"""
result = Result(self.view001)
with self.assertRaises(ResultException) as cm:
invalid_result = result[-1]
self.assertEqual(cm.exception.status_code, 101)
def test_get_item_by_key_using_invalid_options(self):
"""
Since the __getitem__ method uses the 'key' parameter to retrieve the
specified data using a Result, any Result that uses any of 'key',
'keys', 'startkey' or 'endkey' as arguments would yield unexpected
results. For this reason a check was added to ensure that these options
are not used in this case. This test verifies that check.
"""
options = ('key', 'keys', 'startkey', 'endkey')
for option in options:
result = Result(self.view001, **{option: 'julia010'})
with self.assertRaises(ResultException) as cm:
invalid_result = result['julia000']
self.assertEqual(cm.exception.status_code, 102)
def test_get_item_by_key(self):
"""
Test retrieving a result using value that refers to a key of the
result.
"""
result = Result(self.view001)
expected = [{'key': 'julia010', 'id': 'julia010', 'value': 1}]
self.assertEqual(result['julia010'], expected)
self.assertEqual(result[ResultByKey('julia010')], expected)
def test_get_item_by_missing_key(self):
"""
Test retrieving a result using value that refers to a key that does not
exist in the result.
"""
result = Result(self.view001)
self.assertEqual(result['ruby010'], [])
self.assertEqual(result[ResultByKey('ruby010')], [])
def test_get_item_by_complex_key(self):
"""
Test retrieving a result using value that refers to a complex key of the
result.
"""
result = Result(self.view005)
expected = [{'key': ['julia', 10], 'id': 'julia010', 'value': 1}]
self.assertEqual(result[['julia', 10]], expected)
self.assertEqual(result[ResultByKey(['julia', 10])], expected)
def test_get_item_by_integer_key(self):
"""
Test retrieving a result using an integer value that refers to a key of
the result.
"""
result = Result(self.view003)
expected = [{'key': 10, 'id': 'julia020', 'value': 1},
{'key': 10, 'id': 'julia021', 'value': 1}]
self.assertEqual(result[ResultByKey(10)], expected)
def test_get_item_by_missing_integer_key(self):
"""
Test retrieving a result using an integer value that refers to a key
that does not exist in the result.
"""
result = Result(self.view003)
self.assertEqual(result[ResultByKey(99)], [])
def test_get_item_slice_no_start_no_stop(self):
"""
Test that by not providing a start and a stop slice value, the entire
result is returned.
"""
result = Result(self.view001, limit=3)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1},
{'key': 'julia002', 'id': 'julia002', 'value': 1}]
self.assertEqual(result[:], expected)
def test_get_all_items(self):
"""
Test that all results can be retrieved.
"""
result = Result(self.view001, limit=3)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1},
{'key': 'julia002', 'id': 'julia002', 'value': 1}]
self.assertEqual(result.all(), expected)
def test_get_item_invalid_index_slice(self):
"""
Test that when invalid start and stop values are provided in a slice
an exception is raised.
"""
result = Result(self.view001)
with self.assertRaises(ResultException) as cm:
invalid_result = result[-1: 10]
self.assertEqual(cm.exception.status_code, 101)
with self.assertRaises(ResultException) as cm:
invalid_result = result[1: -10]
self.assertEqual(cm.exception.status_code, 101)
with self.assertRaises(ResultException) as cm:
invalid_result = result[-1: -10]
self.assertEqual(cm.exception.status_code, 101)
with self.assertRaises(ResultException) as cm:
invalid_result = result[2: 2]
self.assertEqual(cm.exception.status_code, 101)
with self.assertRaises(ResultException) as cm:
invalid_result = result[5: 2]
self.assertEqual(cm.exception.status_code, 101)
def test_get_item_index_slice_using_start_stop(self):
"""
Test getting an index slice by using start and stop slice values.
"""
result = Result(self.view001)
expected = [{'key': 'julia098', 'id': 'julia098', 'value': 1},
{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result[98:100], expected)
self.assertEqual(result[98:102], expected)
self.assertEqual(result[100:102], [])
result = Result(self.view001, limit=20)
expected = [{'key': 'julia018', 'id': 'julia018', 'value': 1},
{'key': 'julia019', 'id': 'julia019', 'value': 1}]
self.assertEqual(result[18:20], expected)
self.assertEqual(result[18:22], expected)
self.assertEqual(result[20:22], [])
result = Result(self.view001, skip=98)
expected = [{'key': 'julia098', 'id': 'julia098', 'value': 1},
{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result[0:2], expected)
self.assertEqual(result[0:4], expected)
self.assertEqual(result[2:4], [])
result = Result(self.view001, limit=20, skip=20)
expected = [{'key': 'julia038', 'id': 'julia038', 'value': 1},
{'key': 'julia039', 'id': 'julia039', 'value': 1}]
self.assertEqual(result[18:20], expected)
self.assertEqual(result[18:22], expected)
self.assertEqual(result[20:22], [])
def test_get_item_index_slice_using_start_only(self):
"""
Test getting an index slice by using start slice value only.
"""
result = Result(self.view001)
expected = [{'key': 'julia098', 'id': 'julia098', 'value': 1},
{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result[98:], expected)
self.assertEqual(result[100:], [])
result = Result(self.view001, limit=20)
expected = [{'key': 'julia018', 'id': 'julia018', 'value': 1},
{'key': 'julia019', 'id': 'julia019', 'value': 1}]
self.assertEqual(result[18:], expected)
self.assertEqual(result[20:], [])
result = Result(self.view001, skip=98)
expected = [{'key': 'julia098', 'id': 'julia098', 'value': 1},
{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result[0:], expected)
self.assertEqual(result[2:], [])
result = Result(self.view001, limit=20, skip=20)
expected = [{'key': 'julia038', 'id': 'julia038', 'value': 1},
{'key': 'julia039', 'id': 'julia039', 'value': 1}]
self.assertEqual(result[18:], expected)
self.assertEqual(result[20:], [])
def test_get_item_index_slice_using_stop_only(self):
"""
Test getting an index slice by using stop slice value only.
"""
result = Result(self.view001)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1}]
self.assertEqual(result[:2], expected)
expected = [{'key': 'julia{0:03d}'.format(x),
'id': 'julia{0:03d}'.format(x),
'value': 1} for x in range(100)]
self.assertEqual(result[:102], expected)
result = Result(self.view001, limit=20)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1}]
self.assertEqual(result[:2], expected)
expected = [{'key': 'julia{0:03d}'.format(x),
'id': 'julia{0:03d}'.format(x),
'value': 1} for x in range(20)]
self.assertEqual(result[:22], expected)
result = Result(self.view001, skip=98)
expected = [{'key': 'julia098', 'id': 'julia098', 'value': 1},
{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result[:2], expected)
self.assertEqual(result[:4], expected)
result = Result(self.view001, limit=2, skip=20)
expected = [{'key': 'julia020', 'id': 'julia020', 'value': 1},
{'key': 'julia021', 'id': 'julia021', 'value': 1}]
self.assertEqual(result[:2], expected)
self.assertEqual(result[:4], expected)
def test_get_item_key_slice_using_invalid_options(self):
"""
Test that when "key" and/or "keys" are used in the result an exception
is raised.
"""
result = Result(self.view001, key='foo')
with self.assertRaises(ResultException) as cm:
invalid_result = result['foo':]
self.assertEqual(cm.exception.status_code, 102)
result = Result(self.view001, keys=['foo', 'bar'])
with self.assertRaises(ResultException) as cm:
invalid_result = result['foo':]
self.assertEqual(cm.exception.status_code, 102)
result = Result(self.view001, startkey='foo')
with self.assertRaises(ResultException) as cm:
invalid_result = result['foo':]
self.assertEqual(cm.exception.status_code, 102)
result = Result(self.view001, endkey='foo')
with self.assertRaises(ResultException) as cm:
invalid_result = result['foo':]
self.assertEqual(cm.exception.status_code, 102)
def test_get_item_invalid_key_slice(self):
"""
Test that when invalid start and stop values are provided in a slice
an exception is raised. Specifically this happens when the slice start
and stop are different types.
"""
result = Result(self.view001)
with self.assertRaises(ResultException) as cm:
invalid_result = result['foo': ['bar', 'baz']]
self.assertEqual(cm.exception.status_code, 101)
ten = ResultByKey(10)
with self.assertRaises(ResultException) as cm:
invalid_result = result['foo': ten]
self.assertEqual(cm.exception.status_code, 101)
def test_get_item_key_slice_using_start_stop(self):
"""
Test getting a key slice by using start and stop slice values.
"""
result = Result(self.view001)
expected = [{'key': 'julia097', 'id': 'julia097', 'value': 1},
{'key': 'julia098', 'id': 'julia098', 'value': 1},
{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result['julia097': 'julia099'], expected)
self.assertEqual(
result[ResultByKey('julia097'): ResultByKey('julia099')],
expected
)
self.assertEqual(result['julia097': 'ruby'], expected)
self.assertEqual(
result['julia098': 'julia098'],
[{'key': 'julia098', 'id': 'julia098', 'value': 1}]
)
self.assertEqual(result['bar': 'foo'], [])
result = Result(self.view003)
expected = [{'key': 47, 'id': 'julia094', 'value': 1},
{'key': 47, 'id': 'julia095', 'value': 1},
{'key': 48, 'id': 'julia096', 'value': 1},
{'key': 48, 'id': 'julia097', 'value': 1},
{'key': 49, 'id': 'julia098', 'value': 1},
{'key': 49, 'id': 'julia099', 'value': 1}]
self.assertEqual(result[ResultByKey(47): ResultByKey(49)], expected)
self.assertEqual(result[ResultByKey(47): ResultByKey(52)], expected)
self.assertEqual(
result[ResultByKey(48): ResultByKey(48)],
[{'key': 48, 'id': 'julia096', 'value': 1}, {'key': 48, 'id': 'julia097', 'value': 1}]
)
self.assertEqual(result[ResultByKey(52): ResultByKey(54)], [])
result = Result(self.view005)
expected = [{'key': ['julia', 97], 'id': 'julia097', 'value': 1},
{'key': ['julia', 98], 'id': 'julia098', 'value': 1},
{'key': ['julia', 99], 'id': 'julia099', 'value': 1}]
self.assertEqual(result[['julia', 97]: ['julia', 99]], expected)
self.assertEqual(
result[ResultByKey(['julia', 97]): ResultByKey(['julia', 99])],
expected
)
self.assertEqual(result[['julia', 97]: ['ruby', 97]], expected)
self.assertEqual(
result[['julia', 98]: ['julia', 98]],
[{'key': ['julia', 98], 'id': 'julia098', 'value': 1}]
)
self.assertEqual(result[['ruby', 'bar']: ['ruby', 'foo']], [])
def test_get_item_key_slice_start_greater_than_stop(self):
"""
Test getting a key slice by using start value greater than stop value.
The behavior when using CouchDB and newer versions of Cloudant
is to return an HTTP 400 Bad Request.
"""
result = Result(self.view001)
with self.assertRaises(HTTPError) as cm:
invalid_result = result['foo': 'bar']
self.assertTrue(
str(cm.exception).startswith('400 Client Error: Bad Request'))
def test_get_item_key_slice_using_start_only(self):
"""
Test getting a key slice by using the start slice value only.
"""
result = Result(self.view001)
expected = [{'key': 'julia097', 'id': 'julia097', 'value': 1},
{'key': 'julia098', 'id': 'julia098', 'value': 1},
{'key': 'julia099', 'id': 'julia099', 'value': 1}]
self.assertEqual(result['julia097':], expected)
self.assertEqual(result[ResultByKey('julia097'):], expected)
self.assertEqual(result['ruby':], [])
result = Result(self.view003)
expected = [{'key': 47, 'id': 'julia094', 'value': 1},
{'key': 47, 'id': 'julia095', 'value': 1},
{'key': 48, 'id': 'julia096', 'value': 1},
{'key': 48, 'id': 'julia097', 'value': 1},
{'key': 49, 'id': 'julia098', 'value': 1},
{'key': 49, 'id': 'julia099', 'value': 1}]
self.assertEqual(result[ResultByKey(47):], expected)
self.assertEqual(result[ResultByKey(52):], [])
result = Result(self.view005)
expected = [{'key': ['julia', 97], 'id': 'julia097', 'value': 1},
{'key': ['julia', 98], 'id': 'julia098', 'value': 1},
{'key': ['julia', 99], 'id': 'julia099', 'value': 1}]
self.assertEqual(result[['julia', 97]:], expected)
self.assertEqual(result[ResultByKey(['julia', 97]):], expected)
self.assertEqual(result[ResultByKey(['ruby', 'foo']):], [])
def test_get_item_key_slice_using_stop_only(self):
"""
Test getting a key slice by using the stop slice value only.
"""
result = Result(self.view001)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1},
{'key': 'julia002', 'id': 'julia002', 'value': 1}]
self.assertEqual(result[:'julia002'], expected)
self.assertEqual(result[:ResultByKey('julia002')], expected)
self.assertEqual(
result[:'ruby'],
[{'key': 'julia{0:03d}'.format(x),
'id': 'julia{0:03d}'.format(x),
'value': 1} for x in range(100)]
)
self.assertEqual(result[:'foo'], [])
result = Result(self.view003)
expected = [{'key': 0, 'id': 'julia000', 'value': 1},
{'key': 0, 'id': 'julia001', 'value': 1},
{'key': 1, 'id': 'julia002', 'value': 1},
{'key': 1, 'id': 'julia003', 'value': 1},
{'key': 2, 'id': 'julia004', 'value': 1},
{'key': 2, 'id': 'julia005', 'value': 1}]
self.assertEqual(result[:ResultByKey(2)], expected)
self.assertEqual(
result[:ResultByKey(51)],
[{'key': x // 2,
'id': 'julia{0:03d}'.format(x),
'value': 1} for x in range(100)]
)
self.assertEqual(result[:ResultByKey(-10)], [])
result = Result(self.view005)
expected = [{'key': ['julia', 0], 'id': 'julia000', 'value': 1},
{'key': ['julia', 1], 'id': 'julia001', 'value': 1},
{'key': ['julia', 2], 'id': 'julia002', 'value': 1}]
self.assertEqual(result[:['julia', 2]], expected)
self.assertEqual(result[:ResultByKey(['julia', 2])], expected)
self.assertEqual(
result[:['julia', 102]],
[{'key': ['julia', x],
'id': 'julia{0:03d}'.format(x),
'value': 1} for x in range(100)]
)
self.assertEqual(result[:ResultByKey(['foo', 'bar'])], [])
def test_iteration_with_invalid_options(self):
"""
Test that iteration raises an exception when "limit" is
used as option for the result.
"""
result = Result(self.view001, limit=10)
with self.assertRaises(ResultException) as cm:
invalid_result = [row for row in result]
self.assertEqual(cm.exception.status_code, 103)
def test_iteration_invalid_page_size(self):
"""
Test that iteration raises an exception when and invalid "page_size" is
is used as an option for the result.
"""
result = Result(self.view001, page_size=-1)
with self.assertRaises(ResultException) as cm:
invalid_result = [row for row in result]
self.assertEqual(cm.exception.status_code, 104)
result = Result(self.view001, page_size='foo')
with self.assertRaises(ResultException) as cm:
invalid_result = [row for row in result]
self.assertEqual(cm.exception.status_code, 104)
def test_iteration_using_valid_page_size(self):
"""
Test that iteration works as expected when "page_size" is provided as
an option for the result.
"""
result = Result(self.view001, endkey='julia004', page_size=3)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1},
{'key': 'julia002', 'id': 'julia002', 'value': 1},
{'key': 'julia003', 'id': 'julia003', 'value': 1},
{'key': 'julia004', 'id': 'julia004', 'value': 1}]
self.assertEqual([x for x in result], expected)
result = Result(self.view001, endkey='julia004', page_size='3')
self.assertEqual([x for x in result], expected)
result = Result(self.view001, endkey='julia002', page_size=3)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1},
{'key': 'julia002', 'id': 'julia002', 'value': 1}]
self.assertEqual([x for x in result], expected)
result = Result(self.view001, endkey='julia001', page_size=3)
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1}]
self.assertEqual([x for x in result], expected)
def test_iteration_using_default_page_size(self):
"""
Test that iteration works as expected when "page_size" is not provided
as an option for the result.
"""
result = Result(self.view001, endkey='julia004')
expected = [{'key': 'julia000', 'id': 'julia000', 'value': 1},
{'key': 'julia001', 'id': 'julia001', 'value': 1},
{'key': 'julia002', 'id': 'julia002', 'value': 1},
{'key': 'julia003', 'id': 'julia003', 'value': 1},
{'key': 'julia004', 'id': 'julia004', 'value': 1}]
self.assertEqual([x for x in result], expected)
def test_iteration_no_data(self):
"""
Test that iteration works as expected when no data matches the result.
"""
result = Result(self.view001, startkey='ruby')
self.assertEqual([x for x in result], [])
def test_iteration_integer_keys(self):
"""
Test that iteration works as expected when keys are integer.
"""
result = Result(self.view007, page_size=10)
self.assertEqual(len([x for x in result]), 100)
def test_iteration_pagination(self):
"""
Test that iteration pagination works as expected.
"""
class CallMock:
expected_calls = [
{'limit': 28},
{'limit': 28, 'startkey': 1, 'startkey_docid': 'julia027'},
{'limit': 28, 'startkey': 1, 'startkey_docid': 'julia054'},
{'limit': 28, 'startkey': 1, 'startkey_docid': 'julia081'},
]
def __init__(self, outer):
self.outer = outer
self.expected_calls.reverse()
def call(self, *args, **kwargs):
self.outer.assertEqual(dict(kwargs),
self.expected_calls.pop(),
'pagination error')
return View.__call__(self.outer.view007, *args, **kwargs)
with mock.patch.object(self, 'view007',
CallMock(self).call) as _:
result = Result(self.view007, page_size=27)
expected = [
{'id': 'julia{0:03d}'.format(i),
'key': 1,
'value': 'julia'}
for i in range(100)
]
self.assertEqual([x for x in result], expected)
if __name__ == '__main__':
unittest.main()