UNPKG

2.87 kBJavaScriptView Raw
1'use strict';
2
3var testUtils = require('../test-utils');
4var Promise = testUtils.Promise;
5
6module.exports = function (dbType, context) {
7
8 describe(dbType + ': basic', function () {
9
10 it('should create an index', function () {
11 var db = context.db;
12 var index = {
13 "index": {
14 "fields": ["foo"]
15 },
16 "name": "foo-index",
17 "type": "json"
18 };
19 return new Promise(function(resolve, reject) {
20 return db.createIndex(index, function (err, res) {
21 if (err) {
22 return reject(err);
23 }
24 return resolve(res);
25 });
26 }).then(function (response) {
27 response.id.should.match(/^_design\//);
28 response.name.should.equal('foo-index');
29 response.result.should.equal('created');
30 return new Promise(function (resolve, reject) {
31 db.createIndex(index, function (err, res) {
32 if (err) {
33 return reject(err);
34 }
35 return resolve(res);
36 });
37 });
38 }).then(function (response) {
39 response.id.should.match(/^_design\//);
40 response.name.should.equal('foo-index');
41 response.result.should.equal('exists');
42 });
43 });
44
45 it('should find existing indexes', function () {
46 var db = context.db;
47 return new Promise(function (resolve, reject) {
48 db.getIndexes(function (err, response) {
49 if (err) {
50 return reject(err);
51 }
52 resolve(response);
53 });
54 }).then(function (response) {
55 response.should.deep.equal({
56 "total_rows": 1,
57 indexes: [{
58 ddoc: null,
59 name: '_all_docs',
60 type: 'special',
61 def: {fields: [{_id: 'asc'}]}
62 }]
63 });
64 var index = {
65 "index": {
66 "fields": ["foo"]
67 },
68 "name": "foo-index",
69 "type": "json"
70 };
71 return db.createIndex(index);
72 }).then(function () {
73 return db.getIndexes();
74 }).then(function (resp) {
75 var ddoc = resp.indexes[1].ddoc;
76 ddoc.should.match(/_design\/.+/);
77 delete resp.indexes[1].ddoc;
78 resp.should.deep.equal({
79 "total_rows": 2,
80 "indexes": [
81 {
82 "ddoc": null,
83 "name": "_all_docs",
84 "type": "special",
85 "def": {
86 "fields": [
87 {
88 "_id": "asc"
89 }
90 ]
91 }
92 },
93 {
94 "name": "foo-index",
95 "type": "json",
96 "def": {
97 "fields": [
98 {
99 "foo": "asc"
100 }
101 ]
102 }
103 }
104 ]
105 });
106 });
107 });
108 });
109};