Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_view_callable_view_result(self):
"""
Test that by referencing the .result attribute the view callable
method is invoked and the data returned is wrapped as a Result.
"""
self.populate_db_with_documents()
ddoc = DesignDocument(self.db, 'ddoc001')
ddoc.add_view(
'view001',
'function (doc) {\n emit(doc._id, 1);\n}'
)
ddoc.save()
view = ddoc.get_view('view001')
rslt = view.result
self.assertIsInstance(rslt, Result)
ids = []
# rslt[:3] limits the Result to the first 3 elements
for row in rslt[:3]:
ids.append(row['id'])
expected = ['julia000', 'julia001', 'julia002']
self.assertTrue(all(x in ids for x in expected))
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_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 __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)
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)
"""
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_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_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:
{'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_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)