This repository was archived by the owner on Aug 30, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathquery_tests.py
More file actions
189 lines (169 loc) · 5.9 KB
/
Copy pathquery_tests.py
File metadata and controls
189 lines (169 loc) · 5.9 KB
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
#!/usr/bin/env python
# Copyright (C) 2015, 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 the Query class tested against Cloudant only.
See configuration options for environment variables in unit_t_db_base
module docstring.
"""
import os
import unittest
from cloudant.error import CloudantArgumentError
from cloudant.query import Query
from cloudant.result import QueryResult
from nose.plugins.attrib import attr
from .unit_t_db_base import UnitTestDbBase
@attr(db=['cloudant','couch'])
@attr(couchapi=2)
class QueryTests(UnitTestDbBase):
"""
Query unit tests
"""
def setUp(self):
"""
Set up test attributes
"""
super(QueryTests, self).setUp()
self.db_set_up()
def tearDown(self):
"""
Reset test attributes
"""
self.db_tear_down()
super(QueryTests, self).tearDown()
def test_constructor_with_kwargs(self):
"""
Test instantiating a Query by passing in query parameters
"""
query = Query(self.db, foo={'bar': 'baz'})
self.assertIsInstance(query, Query)
self.assertIsInstance(query.result, QueryResult)
self.assertEqual(query, {'foo': {'bar': 'baz'}})
def test_constructor_without_kwargs(self):
"""
Test instantiating a Query without parameters
"""
query = Query(self.db)
self.assertIsInstance(query, Query)
self.assertIsInstance(query.result, QueryResult)
self.assertEqual(query, {})
def test_retrieve_query_url(self):
"""
Test constructing the query test url
"""
query = Query(self.db)
self.assertEqual(
query.url,
'/'.join((self.db.database_url, '_find'))
)
def test_callable_with_invalid_argument(self):
"""
Test Query __call__ by passing in invalid arguments
"""
query = Query(self.db)
try:
query(foo={'bar': 'baz'})
self.fail('Above statement should raise an Exception')
except CloudantArgumentError as err:
self.assertEqual(str(err), 'Invalid argument: foo')
def test_callable_with_invalid_value_types(self):
"""
Test Query __call__ by passing in invalid selector
"""
test_data = [
{'selector': 'blah'}, # Should be a dict
{'limit': 'blah'}, # Should be an int
{'skip': 'blah'}, # Should be an int
{'sort': 'blah'}, # Should be a list
{'fields': 'blah'}, # Should be a list
{'r': 'blah'}, # Should be an int
{'bookmark': 1}, # Should be a basestring
{'use_index': 1} # Should be a basestring
]
for argument in test_data:
query = Query(self.db)
try:
query(**argument)
self.fail('Above statement should raise an Exception')
except CloudantArgumentError as err:
self.assertTrue(str(err).startswith(
'Argument {0} is not an instance of expected type:'.format(
list(argument.keys())[0]
)
))
def test_callable_without_selector(self):
"""
Test Query __call__ without providing a selector
"""
query = Query(self.db)
try:
query(fields=['_id', '_rev'])
self.fail('Above statement should raise an Exception')
except CloudantArgumentError as err:
self.assertEqual(
str(err),
'No selector in the query or the selector was empty. '
'Add a selector to define the query and retry.'
)
def test_callable_with_empty_selector(self):
"""
Test Query __call__ without providing a selector
"""
query = Query(self.db)
try:
query(selector={}, fields=['_id', '_rev'])
self.fail('Above statement should raise an Exception')
except CloudantArgumentError as err:
self.assertEqual(
str(err),
'No selector in the query or the selector was empty. '
'Add a selector to define the query and retry.'
)
def test_callable_executes_query(self):
"""
Test Query __call__ executes a query
"""
self.populate_db_with_documents(100)
query = Query(self.db)
resp = query(
selector={'_id': {'$lt': 'julia050'}},
fields=['_id'],
sort=[{'_id': 'desc'}],
skip=10,
limit=3,
r=1
)
self.assertEqual(
resp['docs'],
[{'_id': 'julia039'}, {'_id': 'julia038'}, {'_id': 'julia037'}]
)
def test_custom_result_context_manager(self):
"""
Test that custom_result yields a context manager and returns expected
content
"""
self.populate_db_with_documents(100)
query = Query(
self.db,
selector={'_id': {'$lt': 'julia050'}},
fields=['_id'],
r=1
)
with query.custom_result(sort=[{'_id': 'desc'}]) as rslt:
self.assertEqual(
rslt[10:13],
[{'_id': 'julia039'}, {'_id': 'julia038'}, {'_id': 'julia037'}]
)
if __name__ == '__main__':
unittest.main()