UNPKG

6.32 kBJavaScriptView Raw
1'use strict';
2
3var testUtils = require('../test-utils');
4var should = testUtils.should;
5
6module.exports = function (dbType, context) {
7
8 describe(dbType + ': ddoc', 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 ddoc: 'foo'
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 return db.getIndexes();
30 }).then(function (resp) {
31 resp.should.deep.equal({
32 "total_rows": 2,
33 "indexes": [
34 {
35 "ddoc": null,
36 "name": "_all_docs",
37 "type": "special",
38 "def": {
39 "fields": [
40 {
41 "_id": "asc"
42 }
43 ]
44 }
45 },
46 {
47 "ddoc": "_design/foo",
48 "name": "foo-index",
49 "type": "json",
50 "def": {
51 "fields": [
52 {
53 "foo": "asc"
54 }
55 ]
56 }
57 }
58 ]
59 });
60 });
61 });
62
63 it('should create an index, existing ddoc', function () {
64 var db = context.db;
65 var index = {
66 index: {
67 fields: ["foo"]
68 },
69 name: "foo-index",
70 type: "json",
71 ddoc: 'foo'
72 };
73 return db.put({
74 _id: '_design/foo',
75 "language": "query"
76 }).then(function () {
77 return db.createIndex(index);
78 }).then(function (response) {
79 response.id.should.match(/^_design\//);
80 response.name.should.equal('foo-index');
81 response.result.should.equal('created');
82 return db.createIndex(index);
83 }).then(function (response) {
84 response.id.should.match(/^_design\//);
85 response.name.should.equal('foo-index');
86 response.result.should.equal('exists');
87 return db.getIndexes();
88 }).then(function (resp) {
89 resp.should.deep.equal({
90 "total_rows": 2,
91 "indexes": [
92 {
93 "ddoc": null,
94 "name": "_all_docs",
95 "type": "special",
96 "def": {
97 "fields": [
98 {
99 "_id": "asc"
100 }
101 ]
102 }
103 },
104 {
105 "ddoc": "_design/foo",
106 "name": "foo-index",
107 "type": "json",
108 "def": {
109 "fields": [
110 {
111 "foo": "asc"
112 }
113 ]
114 }
115 }
116 ]
117 });
118 });
119 });
120
121 it('should create an index, reused ddoc', function () {
122 var db = context.db;
123 var index = {
124 index: {
125 fields: ["foo"]
126 },
127 name: "foo-index",
128 type: "json",
129 ddoc: 'myddoc'
130 };
131 var index2 = {
132 index: {
133 fields: ['bar']
134 },
135 name: "bar-index",
136 ddoc: 'myddoc'
137 };
138 return db.createIndex(index).then(function (response) {
139 response.id.should.match(/^_design\//);
140 response.name.should.equal('foo-index');
141 response.result.should.equal('created');
142 return db.createIndex(index);
143 }).then(function (response) {
144 response.id.should.match(/^_design\//);
145 response.name.should.equal('foo-index');
146 response.result.should.equal('exists');
147 return db.createIndex(index2);
148 }).then(function (response) {
149 response.id.should.match(/^_design\//);
150 response.name.should.equal('bar-index');
151 response.result.should.equal('created');
152 return db.createIndex(index2);
153 }).then(function (response) {
154 response.id.should.match(/^_design\//);
155 response.name.should.equal('bar-index');
156 response.result.should.equal('exists');
157 }).then(function () {
158 return db.getIndexes();
159 }).then(function (resp) {
160 resp.should.deep.equal({
161 "total_rows":3,
162 "indexes": [
163 {
164 "ddoc": null,
165 "name": "_all_docs",
166 "type": "special",
167 "def": {
168 "fields": [
169 {
170 "_id": "asc"
171 }
172 ]
173 }
174 },
175 {
176 "ddoc": "_design/myddoc",
177 "name": "bar-index",
178 "type": "json",
179 "def": {
180 "fields": [
181 {
182 "bar": "asc"
183 }
184 ]
185 }
186 },
187 {
188 "ddoc": "_design/myddoc",
189 "name": "foo-index",
190 "type": "json",
191 "def": {
192 "fields": [
193 {
194 "foo": "asc"
195 }
196 ]
197 }
198 }
199 ]
200 });
201 });
202 });
203
204 it('Error: invalid ddoc lang', function () {
205 var db = context.db;
206 var index = {
207 index: {
208 fields: ["foo"]
209 },
210 name: "foo-index",
211 type: "json",
212 ddoc: 'foo'
213 };
214 return db.put({
215 _id: '_design/foo'
216 }).then(function () {
217 return db.createIndex(index);
218 }).then(function () {
219 throw new Error('shouldnt be here');
220 }, function (err) {
221 should.exist(err);
222 });
223 });
224
225 it('handles ddoc with no views and ignores it', function () {
226 var db = context.db;
227
228 return db.put({
229 _id: '_design/missing-view',
230 language: 'query'
231 })
232 .then(function () {
233 return db.getIndexes();
234 })
235 .then(function (resp) {
236 resp.indexes.length.should.equal(1);
237 });
238
239 });
240 });
241};