1 | 'use strict';
|
2 |
|
3 | var testUtils = require('../test-utils');
|
4 | var should = testUtils.should;
|
5 |
|
6 | module.exports = function (dbType, context) {
|
7 |
|
8 | describe(dbType + ': defaultindex', function () {
|
9 |
|
10 | it('uses all_docs with warning if no index found simple query 1', function () {
|
11 | var db = context.db;
|
12 | return db.bulkDocs([
|
13 | { name: 'mario', _id: 'mario', rank: 5, series: 'mario', debut: 1981 },
|
14 | { name: 'jigglypuff', _id: 'puff', rank: 8, series: 'pokemon', debut: 1996 },
|
15 | { name: 'link', rank: 10, _id: 'link', series: 'zelda', debut: 1986 },
|
16 | { name: 'donkey kong', rank: 7, _id: 'dk', series: 'mario', debut: 1981 },
|
17 | { name: 'pikachu', series: 'pokemon', _id: 'pikachu', rank: 1, debut: 1996 },
|
18 | { name: 'captain falcon', _id: 'falcon', rank: 4, series: 'f-zero', debut: 1990 },
|
19 | { name: 'luigi', rank: 11, _id: 'luigi', series: 'mario', debut: 1983 },
|
20 | { name: 'fox', _id: 'fox', rank: 3, series: 'star fox', debut: 1993 },
|
21 | { name: 'ness', rank: 9, _id: 'ness', series: 'earthbound', debut: 1994 },
|
22 | { name: 'samus', rank: 12, _id: 'samus', series: 'metroid', debut: 1986 },
|
23 | { name: 'yoshi', _id: 'yoshi', rank: 6, series: 'mario', debut: 1990 },
|
24 | { name: 'kirby', _id: 'kirby', series: 'kirby', rank: 2, debut: 1992 }
|
25 | ]).then(function () {
|
26 | return db.find({
|
27 | selector: {
|
28 | series: 'mario'
|
29 | },
|
30 | fields: ["_id"],
|
31 | });
|
32 | }).then(function (resp) {
|
33 | resp.should.deep.equal({
|
34 | warning: 'no matching index found, create an index to optimize query time',
|
35 | docs: [
|
36 | {_id: 'dk'},
|
37 | {_id: 'luigi'},
|
38 | {_id: 'mario'},
|
39 | {_id: 'yoshi'}
|
40 | ]
|
41 | });
|
42 | });
|
43 | });
|
44 |
|
45 |
|
46 | it('uses all_docs with warning if no index found simple query 2', function () {
|
47 | var db = context.db;
|
48 | return db.bulkDocs([
|
49 | { name: 'mario', _id: 'mario', rank: 5, series: 'mario', debut: 1981 },
|
50 | { name: 'jigglypuff', _id: 'puff', rank: 8, series: 'pokemon', debut: 1996 },
|
51 | { name: 'link', rank: 10, _id: 'link', series: 'zelda', debut: 1986 },
|
52 | { name: 'donkey kong', rank: 7, _id: 'dk', series: 'mario', debut: 1981 },
|
53 | { name: 'pikachu', series: 'pokemon', _id: 'pikachu', rank: 1, debut: 1996 },
|
54 | { name: 'captain falcon', _id: 'falcon', rank: 4, series: 'f-zero', debut: 1990 },
|
55 | { name: 'luigi', rank: 11, _id: 'luigi', series: 'mario', debut: 1983 },
|
56 | { name: 'fox', _id: 'fox', rank: 3, series: 'star fox', debut: 1993 },
|
57 | { name: 'ness', rank: 9, _id: 'ness', series: 'earthbound', debut: 1994 },
|
58 | { name: 'samus', rank: 12, _id: 'samus', series: 'metroid', debut: 1986 },
|
59 | { name: 'yoshi', _id: 'yoshi', rank: 6, series: 'mario', debut: 1990 },
|
60 | { name: 'kirby', _id: 'kirby', series: 'kirby', rank: 2, debut: 1992 }
|
61 | ]).then(function () {
|
62 | return db.find({
|
63 | selector: {
|
64 | debut: {
|
65 | $gt: 1992,
|
66 | $lte: 1996
|
67 | },
|
68 | rank: {
|
69 | $gte: 3,
|
70 | $lte: 8
|
71 | }
|
72 | },
|
73 | fields: ["_id"],
|
74 | });
|
75 | }).then(function (resp) {
|
76 | resp.should.deep.equal({
|
77 | warning: 'no matching index found, create an index to optimize query time',
|
78 | docs: [
|
79 | {_id: 'fox'},
|
80 | {_id: 'puff'}
|
81 | ]
|
82 | });
|
83 | });
|
84 | });
|
85 |
|
86 | it('works with complex query', function () {
|
87 | var db = context.db;
|
88 | return db.bulkDocs([
|
89 | { _id: '1', age: 75, name: {first: 'Nancy', surname: 'Sinatra'}},
|
90 | { _id: '2', age: 40, name: {first: 'Eddie', surname: 'Vedder'}},
|
91 | { _id: '3', age: 80, name: {first: 'John', surname: 'Fogerty'}},
|
92 | { _id: '4', age: 76, name: {first: 'Mick', surname: 'Jagger'}},
|
93 | ]).then(function () {
|
94 | return db.find({
|
95 | selector: {
|
96 | $and: [
|
97 | {age:{$gte: 40}},
|
98 | {$not:{age: {$eq: 75}}},
|
99 | ]
|
100 | },
|
101 | fields: ["_id"],
|
102 | });
|
103 | }).then(function (resp) {
|
104 | resp.should.deep.equal({
|
105 | warning: 'no matching index found, create an index to optimize query time',
|
106 | docs: [
|
107 | { _id: '2'},
|
108 | { _id: '3'},
|
109 | { _id: '4'}
|
110 | ]
|
111 | });
|
112 | });
|
113 | });
|
114 |
|
115 | it('throws an error if a sort is required', function () {
|
116 | var db = context.db;
|
117 |
|
118 | return db.bulkDocs([
|
119 | { _id: '1', foo: 'eyo'},
|
120 | { _id: '2', foo: 'ebb'},
|
121 | { _id: '3', foo: 'eba'},
|
122 | { _id: '4', foo: 'abo'}
|
123 | ]).then(function () {
|
124 | return db.find({
|
125 | selector: {foo: {$ne: "eba"}},
|
126 | fields: ["_id", "foo"],
|
127 | sort: [{"foo": "asc"}]
|
128 | });
|
129 | }).then(function () {
|
130 | throw new Error('should have thrown an error');
|
131 | }, function (err) {
|
132 | should.exist(err);
|
133 | });
|
134 | });
|
135 |
|
136 | it('sorts ok if _id used', function () {
|
137 | var db = context.db;
|
138 |
|
139 | return db.bulkDocs([
|
140 | { _id: '1', foo: 'eyo'},
|
141 | { _id: '2', foo: 'ebb'},
|
142 | { _id: '3', foo: 'eba'},
|
143 | { _id: '4', foo: 'abo'}
|
144 | ]).then(function () {
|
145 | return db.find({
|
146 | selector: {foo: {$ne: "eba"}},
|
147 | fields: ["_id",],
|
148 | sort: ["_id"]
|
149 | });
|
150 | }).then(function (resp) {
|
151 | resp.should.deep.equal({
|
152 | warning: 'no matching index found, create an index to optimize query time',
|
153 | docs: [
|
154 | { _id: '1'},
|
155 | { _id: '2'},
|
156 | { _id: '4'}
|
157 | ]
|
158 | });
|
159 | });
|
160 | });
|
161 | });
|
162 |
|
163 | it.skip('ne query will work and sort', function () {
|
164 | var db = context.db;
|
165 | var index = {
|
166 | "index": {
|
167 | "fields": ["foo"]
|
168 | },
|
169 | "name": "foo-index",
|
170 | "type": "json"
|
171 | };
|
172 |
|
173 | return db.createIndex(index).then(function () {
|
174 | return db.bulkDocs([
|
175 | { _id: '1', foo: 'eyo'},
|
176 | { _id: '2', foo: 'ebb'},
|
177 | { _id: '3', foo: 'eba'},
|
178 | { _id: '4', foo: 'abo'}
|
179 | ]);
|
180 | }).then(function () {
|
181 | return db.find({
|
182 | selector: {foo: {$ne: "eba"}},
|
183 | fields: ["_id" ],
|
184 | sort: [{foo: "asc"}]
|
185 | });
|
186 | }).then(function (resp) {
|
187 | resp.should.deep.equal({
|
188 | warning: 'no matching index found, create an index to optimize query time',
|
189 | docs: [
|
190 | {_id: '1'},
|
191 | {_id: '2'},
|
192 | {_id: '4'}
|
193 | ]
|
194 | });
|
195 | });
|
196 | });
|
197 |
|
198 | it.skip('$and empty selector returns all docs', function () {
|
199 | var db = context.db;
|
200 |
|
201 | return db.createIndex({
|
202 | index: {
|
203 | fields: ['foo']
|
204 | }
|
205 | }).then(function () {
|
206 | return db.bulkDocs([
|
207 | {_id: '1', foo: 1},
|
208 | {_id: '2', foo: 2},
|
209 | {_id: '3', foo: 3},
|
210 | {_id: '4', foo: 4}
|
211 | ]);
|
212 | }).then(function () {
|
213 | return db.find({
|
214 | selector: {
|
215 | $and: [{}, {}]
|
216 | },
|
217 | fields: ['_id']
|
218 | }).then(function (resp) {
|
219 | resp.should.deep.equal({
|
220 | warning: 'no matching index found, create an index to optimize query time',
|
221 | docs: [
|
222 | {_id: '1'},
|
223 | {_id: '2'},
|
224 | {_id: '3'},
|
225 | {_id: '4'}
|
226 | ]
|
227 | });
|
228 | });
|
229 | });
|
230 | });
|
231 |
|
232 | it('$elemMatch works with no other index', function () {
|
233 | var db = context.db;
|
234 |
|
235 | return db.createIndex({
|
236 | index: {
|
237 | fields: ['foo']
|
238 | }
|
239 | }).then(function () {
|
240 | return db.bulkDocs([
|
241 | {_id: '1', foo: [1]},
|
242 | {_id: '2', foo: [2]},
|
243 | {_id: '3', foo: [3]},
|
244 | {_id: '4', foo: [4]}
|
245 | ]);
|
246 | }).then(function () {
|
247 | return db.find({
|
248 | selector: {
|
249 | foo: {$elemMatch: {$gte: 3}}
|
250 | },
|
251 | fields: ['_id']
|
252 | }).then(function (resp) {
|
253 | resp.should.deep.equal({
|
254 | warning: 'no matching index found, create an index to optimize query time',
|
255 | docs: [
|
256 | { _id: "3" },
|
257 | { _id: "4" }
|
258 | ]
|
259 | });
|
260 | });
|
261 | });
|
262 | });
|
263 |
|
264 | it.skip('error - no usable index', function () {
|
265 | var db = context.db;
|
266 | var index = {
|
267 | "index": {
|
268 | "fields": ["foo"]
|
269 | },
|
270 | "name": "foo-index",
|
271 | "type": "json"
|
272 | };
|
273 | return db.createIndex(index).then(function () {
|
274 | return db.find({
|
275 | "selector": {"foo": "$exists"},
|
276 | "fields": ["_id", "foo"],
|
277 | "sort": [{"bar": "asc"}]
|
278 | });
|
279 | }).then(function () {
|
280 | throw new Error('shouldnt be here');
|
281 | }, function (err) {
|
282 | should.exist(err);
|
283 | });
|
284 | });
|
285 |
|
286 | it.skip('handles just regex selector', function () {
|
287 | var db = context.db;
|
288 | return db.bulkDocs([
|
289 | {_id: '1', foo: 1},
|
290 | {_id: '2', foo: 2},
|
291 | {_id: '3', foo: 3},
|
292 | {_id: '4', foo: 4}
|
293 | ]).then(function () {
|
294 | return db.find({
|
295 | selector: {
|
296 | _id: {$regex: /1/}
|
297 | },
|
298 | fields: ['_id']
|
299 | }).then(function (resp) {
|
300 | resp.should.deep.equal({
|
301 | warning: 'no matching index found, create an index to optimize query time',
|
302 | docs: [
|
303 | { _id: "1" }
|
304 | ]
|
305 | });
|
306 | });
|
307 | });
|
308 | });
|
309 | };
|