UNPKG

10.6 kBJavaScriptView Raw
1'use strict';
2
3
4module.exports = function (dbType, context) {
5 describe(dbType + ': Array', function () {
6
7 beforeEach(function () {
8 return context.db.bulkDocs([
9 { name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20 },
10 { name: 'Mary', _id: 'mary', favorites: ['Pokemon'], age: 21 },
11 { name: 'Link', _id: 'link', favorites: ['Zelda', 'Pokemon'], age: 22},
12 { name: 'William', _id: 'william', favorites: ['Mario'], age: 23}
13 ]).then(function () {
14 var index = {
15 "index": {
16 "fields": ["name"]
17 },
18 "name": "name-index",
19 "type": "json"
20 };
21 return context.db.createIndex(index);
22 });
23 });
24
25 describe('$in', function () {
26 it('should return docs match single value in array', function () {
27 var db = context.db;
28 return db.find({
29 selector: {
30 name: {
31 $gt: null
32 },
33 favorites: {
34 $in: ["Mario"]
35 }
36 },
37 }).then(function (resp) {
38 var docs = resp.docs.map(function (doc) {
39 delete doc._rev;
40 return doc;
41 });
42
43 docs.should.deep.equal([
44 { name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20},
45 { name: 'William', _id: 'william', favorites: ['Mario'], age: 23 }
46 ]);
47 });
48 });
49
50 it('should return docs match single field that is not an array', function () {
51 var db = context.db;
52 return db.find({
53 selector: {
54 _id: {
55 $gt: 'a'
56 },
57 name: {
58 $in: ['James', 'William']
59 }
60 },
61 }).then(function (resp) {
62 var docs = resp.docs.map(function (doc) {
63 delete doc._rev;
64 return doc;
65 });
66
67 docs.should.deep.equal([
68 { name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20 },
69 { name: 'William', _id: 'william', favorites: ['Mario'], age: 23 }
70 ]);
71 });
72 });
73
74 it('should return docs match single field that is not an array and number', function () {
75 var db = context.db;
76 return db.find({
77 selector: {
78 name: {
79 $gt: null
80 },
81 age: {
82 $in: [20, 23]
83 }
84 },
85 }).then(function (resp) {
86 var docs = resp.docs.map(function (doc) {
87 delete doc._rev;
88 return doc;
89 });
90
91 docs.should.deep.equal([
92 { name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20 },
93 { name: 'William', _id: 'william', favorites: ['Mario'], age: 23 }
94 ]);
95 });
96 });
97
98
99 it('should return docs match two values in array', function () {
100 var db = context.db;
101 return db.find({
102 selector: {
103 name: {
104 $gt: null
105 },
106 favorites: {
107 $in: ["Mario", "Zelda"]
108 }
109 },
110 }).then(function (resp) {
111 var docs = resp.docs.map(function (doc) {
112 delete doc._rev;
113 return doc;
114 });
115
116 docs.should.deep.equal([
117 { name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20 },
118 { name: 'Link', _id: 'link', favorites: ['Zelda', 'Pokemon'], age: 22},
119 { name: 'William', _id: 'william', favorites: ['Mario'], age: 23 }
120 ]);
121 });
122 });
123
124 it('should return no docs for no $in match', function () {
125 var db = context.db;
126 return db.find({
127 selector: {
128 name: {
129 $gt: null
130 },
131 favorites: {
132 $in: ["TMNT"]
133 }
134 },
135 }).then(function (resp) {
136 resp.docs.should.have.length(0);
137 });
138 });
139 });
140
141 describe('$all', function () {
142 it('should return docs that match single value in $all array', function () {
143 var db = context.db;
144 return db.find({
145 selector: {
146 name: {
147 $gt: null
148 },
149 favorites: {
150 $all: ["Mario"]
151 }
152 },
153 }).then(function (resp) {
154 var docs = resp.docs.map(function (doc) {
155 delete doc._rev;
156 return doc;
157 });
158
159 docs.should.deep.equal([
160 { name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20},
161 { name: 'William', _id: 'william', favorites: ['Mario'], age: 23}
162 ]);
163 });
164 });
165
166 it('should return docs match two values in $all array', function () {
167 var db = context.db;
168 return db.find({
169 selector: {
170 name: {
171 $gt: null
172 },
173 favorites: {
174 $all: ['Mario', 'Pokemon']
175 }
176 },
177 }).then(function (resp) {
178 var docs = resp.docs.map(function (doc) {
179 delete doc._rev;
180 return doc;
181 });
182
183 docs.should.deep.equal([
184 { name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20},
185 ]);
186 });
187 });
188
189 it('should return no docs for no match for $all', function () {
190 var db = context.db;
191 return db.find({
192 selector: {
193 name: {
194 $gt: null
195 },
196 favorites: {
197 $all: ["Mario", "Zelda"]
198 }
199 },
200 }).then(function (resp) {
201 resp.docs.should.have.length(0);
202 });
203 });
204 });
205
206 describe('$size', function () {
207 it('should return docs with array length 1', function () {
208 var db = context.db;
209 return db.find({
210 selector: {
211 name: {
212 $gt: null
213 },
214 favorites: {
215 $size: 1
216 }
217 },
218 }).then(function (resp) {
219 var docs = resp.docs.map(function (doc) {
220 delete doc._rev;
221 return doc;
222 });
223
224 docs.should.deep.equal([
225 { name: 'Mary', _id: 'mary', favorites: ['Pokemon'], age: 21 },
226 { name: 'William', _id: 'william', favorites: ['Mario'], age: 23 }
227 ]);
228 });
229 });
230
231 it('should return docs array length 2', function () {
232 var db = context.db;
233 return db.find({
234 selector: {
235 name: {
236 $gt: null
237 },
238 favorites: {
239 $size: 2
240 }
241 },
242 }).then(function (resp) {
243 var docs = resp.docs.map(function (doc) {
244 delete doc._rev;
245 return doc;
246 });
247
248 docs.should.deep.equal([
249 { name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20 },
250 { name: 'Link', _id: 'link', favorites: ['Zelda', 'Pokemon'], age: 22 },
251 ]);
252 });
253 });
254
255 it('should return no docs for length 5', function () {
256 var db = context.db;
257 return db.find({
258 selector: {
259 name: {
260 $gt: null
261 },
262 favorites: {
263 $size: 5
264 }
265 },
266 }).then(function (resp) {
267 resp.docs.should.have.length(0);
268 });
269 });
270 });
271
272 describe('$nin', function () {
273 it('should return docs match single value $nin array', function () {
274 var db = context.db;
275 return db.find({
276 selector: {
277 name: {
278 $gt: null
279 },
280 favorites: {
281 $nin: ["Mario"]
282 }
283 },
284 }).then(function (resp) {
285 var docs = resp.docs.map(function (doc) {
286 delete doc._rev;
287 return doc;
288 });
289
290 docs.should.deep.equal([
291 { name: 'Link', _id: 'link', favorites: ['Zelda', 'Pokemon'], age: 22},
292 { name: 'Mary', _id: 'mary', favorites: ['Pokemon'], age: 21 },
293 ]);
294 });
295 });
296
297 it('should return docs that do not match single field that is not an array', function () {
298 var db = context.db;
299 return db.find({
300 selector: {
301 _id: {
302 $gt: 'a'
303 },
304 name: {
305 $nin: ['James', 'William']
306 }
307 },
308 }).then(function (resp) {
309 var docs = resp.docs.map(function (doc) {
310 delete doc._rev;
311 return doc;
312 });
313
314 docs.should.deep.equal([
315 { name: 'Link', _id: 'link', favorites: ['Zelda', 'Pokemon'], age: 22},
316 { name: 'Mary', _id: 'mary', favorites: ['Pokemon'], age: 21 },
317 ]);
318 });
319 });
320
321 it('should return docs with single field that is not an array and number', function () {
322 var db = context.db;
323 return db.find({
324 selector: {
325 name: {
326 $gt: null
327 },
328 age: {
329 $nin: [20, 23]
330 }
331 },
332 }).then(function (resp) {
333 var docs = resp.docs.map(function (doc) {
334 delete doc._rev;
335 return doc;
336 });
337
338 docs.should.deep.equal([
339 { name: 'Link', _id: 'link', favorites: ['Zelda', 'Pokemon'], age: 22},
340 { name: 'Mary', _id: 'mary', favorites: ['Pokemon'], age: 21 },
341 ]);
342 });
343 });
344
345 it('should return docs that do not match two values $nin array', function () {
346 var db = context.db;
347 return db.find({
348 selector: {
349 name: {
350 $gt: null
351 },
352 favorites: {
353 $nin: ["Pokemon", "Zelda"]
354 }
355 },
356 }).then(function (resp) {
357 var docs = resp.docs.map(function (doc) {
358 delete doc._rev;
359 return doc;
360 });
361
362 docs.should.deep.equal([
363 { name: 'William', _id: 'william', favorites: ['Mario'], age: 23 }
364 ]);
365 });
366 });
367
368 it('should return all docs for no match for $nin', function () {
369 var db = context.db;
370 return db.find({
371 selector: {
372 name: {
373 $gt: null
374 },
375 favorites: {
376 $nin: ["TMNT"]
377 }
378 },
379 }).then(function (resp) {
380 resp.docs.should.have.length(4);
381 });
382 });
383 });
384 });
385};