UNPKG

6.89 kBJavaScriptView Raw
1'use strict';
2
3var testUtils = require('../test-utils');
4var sortById = testUtils.sortById;
5
6module.exports = function (dbType, context) {
7
8 describe(dbType + ': basic3', function () {
9
10 beforeEach(function () {
11 return context.db.bulkDocs([
12 { name: 'Mario', _id: 'mario', rank: 5, series: 'Mario', debut: 1981, awesome: true },
13 { name: 'Jigglypuff', _id: 'puff', rank: 8, series: 'Pokemon', debut: 1996,
14 awesome: false },
15 { name: 'Link', rank: 10, _id: 'link', series: 'Zelda', debut: 1986, awesome: true },
16 { name: 'Donkey Kong', rank: 7, _id: 'dk', series: 'Mario', debut: 1981, awesome: false },
17 { name: 'Pikachu', series: 'Pokemon', _id: 'pikachu', rank: 1, debut: 1996, awesome: true },
18 { name: 'Captain Falcon', _id: 'falcon', rank: 4, series: 'F-Zero', debut: 1990,
19 awesome: true },
20 { name: 'Luigi', rank: 11, _id: 'luigi', series: 'Mario', debut: 1983, awesome: false },
21 { name: 'Fox', _id: 'fox', rank: 3, series: 'Star Fox', debut: 1993, awesome: true },
22 { name: 'Ness', rank: 9, _id: 'ness', series: 'Earthbound', debut: 1994, awesome: true },
23 { name: 'Samus', rank: 12, _id: 'samus', series: 'Metroid', debut: 1986, awesome: true },
24 { name: 'Yoshi', _id: 'yoshi', rank: 6, series: 'Mario', debut: 1990, awesome: true },
25 { name: 'Kirby', _id: 'kirby', series: 'Kirby', rank: 2, debut: 1992, awesome: true },
26 { name: 'Master Hand', _id: 'master_hand', series: 'Smash Bros', rank: 0, debut: 1999,
27 awesome: false }
28 ]);
29 });
30
31 it('should be able to search for numbers', function () {
32 var db = context.db;
33 var index = {
34 "index": {
35 "fields": ["rank"]
36 }
37 };
38 return db.createIndex(index).then(function () {
39 return db.find({
40 selector: {rank: 12},
41 fields: ['_id']
42 }).then(function (response) {
43 response.docs.should.deep.equal([
44 {"_id": "samus"}
45 ]);
46 });
47 });
48 });
49
50 it('should use $exists for an in-memory filter', function () {
51 var db = context.db;
52 var index = {
53 "index": {
54 "fields": ["rank"]
55 }
56 };
57 return db.createIndex(index).then(function () {
58 return db.find({
59 selector: {rank: 12, name: {$exists: true}},
60 fields: ['_id']
61 }).then(function (response) {
62 response.docs.should.deep.equal([
63 {"_id": "samus"}
64 ]);
65 });
66 });
67 });
68
69 it('should be able to search for 0', function () {
70 var db = context.db;
71 var index = {
72 "index": {
73 "fields": ["rank"]
74 }
75 };
76 return db.createIndex(index).then(function () {
77 return db.find({
78 selector: {rank: 0},
79 fields: ['_id']
80 }).then(function (response) {
81 response.docs.should.deep.equal([
82 {"_id": "master_hand"}
83 ]);
84 });
85 });
86 });
87
88 it('should be able to search for boolean true', function () {
89 var db = context.db;
90 var index = {
91 "index": {
92 "fields": ["awesome"]
93 }
94 };
95 return db.createIndex(index).then(function () {
96 return db.find({
97 selector: {awesome: true},
98 fields: ['_id']
99 }).then(function (response) {
100 response.docs.sort(sortById);
101 response.docs.should.deep.equal([{"_id":"falcon"},{"_id":"fox"},{"_id":"kirby"},
102 {"_id":"link"},{"_id":"mario"},{"_id":"ness"},{"_id":"pikachu"},
103 {"_id":"samus"},{"_id":"yoshi"}]);
104 });
105 });
106 });
107
108 it('should be able to search for boolean true', function () {
109 var db = context.db;
110 var index = {
111 "index": {
112 "fields": ["awesome"]
113 }
114 };
115 return db.createIndex(index).then(function () {
116 return db.find({
117 selector: {awesome: false},
118 fields: ['_id']
119 }).then(function (response) {
120 response.docs.sort(sortById);
121 response.docs.should.deep.equal([{"_id":"dk"},{"_id":"luigi"},
122 {"_id":"master_hand"},{"_id":"puff"}]);
123 });
124 });
125 });
126
127 it('#73 should be able to create a custom index name', function () {
128 var db = context.db;
129 var index = {
130 index: {
131 fields: ["awesome"],
132 name: 'myindex',
133 ddoc: 'mydesigndoc'
134 }
135 };
136 return db.createIndex(index).then(function () {
137 return db.getIndexes();
138 }).then(function (res) {
139 var indexes = res.indexes.map(function (index) {
140 return {
141 name: index.name,
142 ddoc: index.ddoc,
143 type: index.type
144 };
145 });
146 indexes.should.deep.equal([
147 {
148 name: '_all_docs',
149 type: 'special',
150 ddoc: null
151 },
152 {
153 name: 'myindex',
154 ddoc: '_design/mydesigndoc',
155 type: 'json'
156 }
157 ]);
158 return db.get('_design/mydesigndoc');
159 });
160 });
161
162 it('#73 should be able to create a custom index, alt style', function () {
163 var db = context.db;
164 var index = {
165 index: {
166 fields: ["awesome"],
167 },
168 name: 'myindex',
169 ddoc: 'mydesigndoc'
170 };
171 return db.createIndex(index).then(function () {
172 return db.getIndexes();
173 }).then(function (res) {
174 var indexes = res.indexes.map(function (index) {
175 return {
176 name: index.name,
177 ddoc: index.ddoc,
178 type: index.type
179 };
180 });
181 indexes.should.deep.equal([
182 {
183 name: '_all_docs',
184 type: 'special',
185 ddoc: null
186 },
187 {
188 name: 'myindex',
189 ddoc: '_design/mydesigndoc',
190 type: 'json'
191 }
192 ]);
193 return db.get('_design/mydesigndoc');
194 });
195 });
196
197 it('#73 should be able to create a custom index, alt style 2', function () {
198 var db = context.db;
199 var index = {
200 name: 'myindex',
201 ddoc: 'mydesigndoc',
202 fields: ["awesome"]
203 };
204 return db.createIndex(index).then(function () {
205 return db.getIndexes();
206 }).then(function (res) {
207 var indexes = res.indexes.map(function (index) {
208 return {
209 name: index.name,
210 ddoc: index.ddoc,
211 type: index.type
212 };
213 });
214 indexes.should.deep.equal([
215 {
216 name: '_all_docs',
217 type: 'special',
218 ddoc: null
219 },
220 {
221 name: 'myindex',
222 ddoc: '_design/mydesigndoc',
223 type: 'json'
224 }
225 ]);
226 return db.get('_design/mydesigndoc');
227 });
228 });
229
230 });
231};
\No newline at end of file