UNPKG

12.8 kBJavaScriptView Raw
1'use strict';
2
3var testUtils = require('../test-utils');
4var should = testUtils.should;
5var Promise = testUtils.Promise;
6
7module.exports = function (dbType, context) {
8
9 describe(dbType + ': basic', function () {
10
11 it('should create an index', function () {
12 var db = context.db;
13 var index = {
14 "index": {
15 "fields": ["foo"]
16 },
17 "name": "foo-index",
18 "type": "json"
19 };
20 return db.createIndex(index).then(function (response) {
21 response.id.should.match(/^_design\//);
22 response.name.should.equal('foo-index');
23 response.result.should.equal('created');
24 return db.createIndex(index);
25 }).then(function (response) {
26 response.id.should.match(/^_design\//);
27 response.name.should.equal('foo-index');
28 response.result.should.equal('exists');
29 });
30 });
31
32 it('throws an error for an invalid index creation', function () {
33 var db = context.db;
34 return db.createIndex('yo yo yo').then(function () {
35 throw new Error('expected an error');
36 }, function (err) {
37 should.exist(err);
38 });
39 });
40
41 it('throws an error for an invalid index deletion', function () {
42 var db = context.db;
43 return db.deleteIndex('yo yo yo').then(function () {
44 throw new Error('expected an error');
45 }, function (err) {
46 should.exist(err);
47 });
48 });
49
50 it('should not recognize duplicate indexes', function () {
51 var db = context.db;
52 var index = {
53 "index": {
54 "fields": ["foo"]
55 },
56 "name": "foo-index",
57 "type": "json"
58 };
59 var index2 = {
60 "index": {
61 "fields": ["foo"]
62 },
63 "name": "bar-index",
64 "type": "json"
65 };
66
67 return db.createIndex(index).then(function (response) {
68 response.id.should.match(/^_design\//);
69 response.name.should.equal('foo-index');
70 response.result.should.equal('created');
71 return db.createIndex(index2);
72 }).then(function (response) {
73 response.id.should.match(/^_design\//);
74 response.name.should.equal('bar-index');
75 response.result.should.equal('created');
76 return db.getIndexes();
77 }).then(function (res) {
78 res.indexes.should.have.length(3);
79 var ddoc1 = res.indexes[1].ddoc;
80 var ddoc2 = res.indexes[2].ddoc;
81 ddoc1.should.not.equal(ddoc2,
82 'essentially duplicate indexes are not md5summed to the' +
83 'same ddoc');
84 });
85 });
86
87 it('should find existing indexes', function () {
88 var db = context.db;
89 return db.getIndexes().then(function (response) {
90 response.should.deep.equal({
91 "total_rows": 1,
92 indexes: [{
93 ddoc: null,
94 name: '_all_docs',
95 type: 'special',
96 def: {fields: [{_id: 'asc'}]}
97 }]
98 });
99 var index = {
100 "index": {
101 "fields": ["foo"]
102 },
103 "name": "foo-index",
104 "type": "json"
105 };
106 return db.createIndex(index);
107 }).then(function () {
108 return db.getIndexes();
109 }).then(function (resp) {
110 var ddoc = resp.indexes[1].ddoc;
111 ddoc.should.match(/_design\/.+/);
112 delete resp.indexes[1].ddoc;
113 resp.should.deep.equal({
114 "total_rows": 2,
115 "indexes": [
116 {
117 "ddoc": null,
118 "name": "_all_docs",
119 "type": "special",
120 "def": {
121 "fields": [
122 {
123 "_id": "asc"
124 }
125 ]
126 }
127 },
128 {
129 "name": "foo-index",
130 "type": "json",
131 "def": {
132 "fields": [
133 {
134 "foo": "asc"
135 }
136 ]
137 }
138 }
139 ]
140 });
141 });
142 });
143
144 it('should create ddocs automatically', function () {
145 var db = context.db;
146 var index = {
147 "index": {
148 "fields": ["foo"]
149 },
150 "name": "foo-index",
151 "type": "json"
152 };
153 var ddocId;
154 return db.createIndex(index).then(function () {
155 return db.getIndexes();
156 }).then(function (resp) {
157 ddocId = resp.indexes[1].ddoc;
158 return db.get(ddocId);
159 }).then(function (ddoc) {
160 ddoc._id.should.equal(ddocId);
161 should.exist(ddoc._rev);
162 delete ddoc._id;
163 delete ddoc._rev;
164 delete ddoc.views['foo-index'].options.w; // wtf is this?
165 ddoc.should.deep.equal({
166 "language": "query",
167 "views": {
168 "foo-index": {
169 "map": {
170 "fields": {
171 "foo": "asc"
172 }
173 },
174 "reduce": "_count",
175 "options": {
176 "def": {
177 "fields": [
178 "foo"
179 ]
180 }
181 }
182 }
183 }
184 });
185 });
186 });
187
188 it('should create ddocs automatically 2', function () {
189 var db = context.db;
190 var index = {
191 "index": {
192 "fields": [{"foo": "asc"}]
193 },
194 "name": "foo-index",
195 "type": "json"
196 };
197 var ddocId;
198 return db.createIndex(index).then(function () {
199 return db.getIndexes();
200 }).then(function (resp) {
201 ddocId = resp.indexes[1].ddoc;
202 return db.get(ddocId);
203 }).then(function (ddoc) {
204 ddoc._id.should.equal(ddocId);
205 should.exist(ddoc._rev);
206 delete ddoc._id;
207 delete ddoc._rev;
208 delete ddoc.views['foo-index'].options.w; // wtf is this?
209 ddoc.should.deep.equal({
210 "language": "query",
211 "views": {
212 "foo-index": {
213 "map": {
214 "fields": {
215 "foo": "asc"
216 }
217 },
218 "reduce": "_count",
219 "options": {
220 "def": {
221 "fields": [
222 {"foo": "asc"}
223 ]
224 }
225 }
226 }
227 }
228 });
229 });
230 });
231
232 it('should create ddocs automatically 3', function () {
233 var db = context.db;
234 var index = {
235 "index": {
236 "fields": [
237 {"foo": "asc"},
238 "bar"
239 ]
240 },
241 "name": "foo-index",
242 "type": "json"
243 };
244 var ddocId;
245 return db.createIndex(index).then(function () {
246 return db.getIndexes();
247 }).then(function (resp) {
248 ddocId = resp.indexes[1].ddoc;
249 return db.get(ddocId);
250 }).then(function (ddoc) {
251 ddoc._id.should.equal(ddocId);
252 should.exist(ddoc._rev);
253 delete ddoc._id;
254 delete ddoc._rev;
255 delete ddoc.views['foo-index'].options.w; // wtf is this?
256 ddoc.should.deep.equal({
257 "language": "query",
258 "views": {
259 "foo-index": {
260 "map": {
261 "fields": {
262 "foo": "asc",
263 "bar": "asc"
264 }
265 },
266 "reduce": "_count",
267 "options": {
268 "def": {
269 "fields": [
270 {"foo": "asc"},
271 "bar"
272 ]
273 }
274 }
275 }
276 }
277 });
278 });
279 });
280
281 it('deletes indexes, callback style', function () {
282 var db = context.db;
283 var index = {
284 "index": {
285 "fields": ["foo"]
286 },
287 "name": "foo-index",
288 "type": "json"
289 };
290 return new Promise(function (resolve, reject) {
291 db.createIndex(index, function (err) {
292 if (err) {
293 return reject(err);
294 }
295 resolve();
296 });
297 }).then(function () {
298 return db.getIndexes();
299 }).then(function (resp) {
300 return new Promise(function (resolve, reject) {
301 db.deleteIndex(resp.indexes[1], function (err, resp) {
302 if (err) {
303 return reject(err);
304 }
305 resolve(resp);
306 });
307 });
308 }).then(function (resp) {
309 resp.should.deep.equal({ok: true});
310 return db.getIndexes();
311 }).then(function (resp) {
312 resp.should.deep.equal({
313 "total_rows": 1,
314 "indexes":[{
315 "ddoc": null,
316 "name":"_all_docs",
317 "type":"special",
318 "def":{ "fields": [{"_id": "asc"}] }
319 }]
320 });
321 });
322 });
323
324 it('deletes indexes', function () {
325 var db = context.db;
326 var index = {
327 "index": {
328 "fields": ["foo"]
329 },
330 "name": "foo-index",
331 "type": "json"
332 };
333 return db.createIndex(index).then(function () {
334 return db.getIndexes();
335 }).then(function (resp) {
336 return db.deleteIndex(resp.indexes[1]);
337 }).then(function (resp) {
338 resp.should.deep.equal({ok: true});
339 return db.getIndexes();
340 }).then(function (resp) {
341 resp.should.deep.equal({
342 "total_rows": 1,
343 "indexes":[{
344 "ddoc": null,
345 "name":"_all_docs",
346 "type":"special",
347 "def":{ "fields": [{"_id": "asc"}] }
348 }]
349 });
350 });
351 });
352
353 it('deletes indexes, no type', function () {
354 var db = context.db;
355 var index = {
356 "index": {
357 "fields": ["foo"]
358 },
359 "name": "foo-index"
360 };
361 return db.createIndex(index).then(function () {
362 return db.getIndexes();
363 }).then(function (resp) {
364 delete resp.indexes[1].type;
365 return db.deleteIndex(resp.indexes[1]);
366 }).then(function (resp) {
367 resp.should.deep.equal({ok: true});
368 return db.getIndexes();
369 }).then(function (resp) {
370 resp.should.deep.equal({
371 "total_rows": 1,
372 "indexes":[{
373 "ddoc": null,
374 "name":"_all_docs",
375 "type":"special",
376 "def":{ "fields": [{"_id": "asc"}] }
377 }]
378 });
379 });
380 });
381
382 it('deletes indexes, no ddoc', function () {
383 var db = context.db;
384 var index = {
385 "index": {
386 "fields": ["foo"]
387 },
388 "name": "foo-index"
389 };
390 return db.createIndex(index).then(function () {
391 return db.getIndexes();
392 }).then(function (resp) {
393 delete resp.indexes[1].ddoc;
394 return db.deleteIndex(resp.indexes[1]);
395 }).then(function () {
396 throw new Error('expected an error due to no ddoc');
397 }, function (err) {
398 should.exist(err);
399 });
400 });
401
402 it('deletes indexes, no name', function () {
403 var db = context.db;
404 var index = {
405 "index": {
406 "fields": ["foo"]
407 },
408 "name": "foo-index"
409 };
410 return db.createIndex(index).then(function () {
411 return db.getIndexes();
412 }).then(function (resp) {
413 delete resp.indexes[1].name;
414 return db.deleteIndex(resp.indexes[1]);
415 }).then(function () {
416 throw new Error('expected an error due to no name');
417 }, function (err) {
418 should.exist(err);
419 });
420 });
421
422 it('deletes indexes, one name per ddoc', function () {
423 var db = context.db;
424 var index = {
425 "index": {
426 "fields": ["foo"]
427 },
428 "name": "myname",
429 "ddoc": "myddoc"
430 };
431 return db.createIndex(index).then(function () {
432 return db.getIndexes();
433 }).then(function (resp) {
434 return db.deleteIndex(resp.indexes[1]);
435 }).then(function () {
436 return db.get('_design/myddoc');
437 }).then(function () {
438 throw new Error('expected an error');
439 }, function (err) {
440 should.exist(err);
441 });
442 });
443
444 it('deletes indexes, many names per ddoc', function () {
445 var db = context.db;
446 var index = {
447 "index": {
448 "fields": ["foo"]
449 },
450 "name": "myname",
451 "ddoc": "myddoc"
452 };
453 var index2 = {
454 "index": {
455 "fields": ["bar"]
456 },
457 "name": "myname2",
458 "ddoc": "myddoc"
459 };
460 return db.createIndex(index).then(function () {
461 return db.createIndex(index2);
462 }).then(function () {
463 return db.getIndexes();
464 }).then(function (resp) {
465 return db.deleteIndex(resp.indexes[1]);
466 }).then(function () {
467 return db.get('_design/myddoc');
468 }).then(function (ddoc) {
469 Object.keys(ddoc.views).should.deep.equal(['myname2']);
470 });
471 });
472
473 });
474};