UNPKG

8.21 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 + ': limit-skip', function () {
9
10 beforeEach(function () {
11 return context.db.bulkDocs([
12 { name: 'Mario', _id: 'mario', rank: 5, series: 'Mario', debut: 1981 },
13 { name: 'Jigglypuff', _id: 'puff', rank: 8, series: 'Pokemon', debut: 1996 },
14 { name: 'Link', rank: 10, _id: 'link', series: 'Zelda', debut: 1986 },
15 { name: 'Donkey Kong', rank: 7, _id: 'dk', series: 'Mario', debut: 1981 },
16 { name: 'Pikachu', series: 'Pokemon', _id: 'pikachu', rank: 1, debut: 1996 },
17 { name: 'Captain Falcon', _id: 'falcon', rank: 4, series: 'F-Zero', debut: 1990 },
18 { name: 'Luigi', rank: 11, _id: 'luigi', series: 'Mario', debut: 1983 },
19 { name: 'Fox', _id: 'fox', rank: 3, series: 'Star Fox', debut: 1993 },
20 { name: 'Ness', rank: 9, _id: 'ness', series: 'Earthbound', debut: 1994 },
21 { name: 'Samus', rank: 12, _id: 'samus', series: 'Metroid', debut: 1986 },
22 { name: 'Yoshi', _id: 'yoshi', rank: 6, series: 'Mario', debut: 1990 },
23 { name: 'Kirby', _id: 'kirby', series: 'Kirby', rank: 2, debut: 1992 }
24 ]);
25 });
26
27 it('should work with $and 1-1', function () {
28 var db = context.db;
29 return db.createIndex({
30 "index": {
31 "fields": ["series"]
32 }
33 }).then(function () {
34 return db.createIndex({
35 "index": {
36 "fields": ["debut"]
37 }
38 });
39 }).then(function() {
40
41 return db.find({
42 selector: {
43 $and: [
44 {series: 'Mario'},
45 {debut: {$gte: 1982}}
46 ]
47 },
48 fields: ['_id'],
49 limit: 1,
50 skip: 1
51 });
52 }).then(function (res) {
53 res.docs.sort(sortById);
54 res.docs.should.deep.equal([{_id: 'yoshi'}]);
55 });
56 });
57
58 it('should work with $and 1 1-2', function () {
59 var db = context.db;
60 return db.createIndex({
61 "index": {
62 "fields": ["series"]
63 }
64 }).then(function () {
65 return db.createIndex({
66 "index": {
67 "fields": ["debut"]
68 }
69 });
70 }).then(function() {
71 return db.find({
72 selector: {
73 $and: [
74 {series: 'Mario'},
75 {debut: {$gte: 1982}}
76 ]
77 },
78 fields: ['_id'],
79 limit: 1,
80 skip: 2
81 });
82 }).then(function (res) {
83 res.docs.sort(sortById);
84 res.docs.should.deep.equal([]);
85 });
86 });
87
88 it('should work with $and 1 2-0', function () {
89 var db = context.db;
90 return db.createIndex({
91 "index": {
92 "fields": ["series"]
93 }
94 }).then(function () {
95 return db.createIndex({
96 "index": {
97 "fields": ["debut"]
98 }
99 });
100 }).then(function() {
101 return db.find({
102 selector: {
103 $and: [
104 {series: 'Mario'},
105 {debut: {$gte: 1982}}
106 ]
107 },
108 fields: ['_id'],
109 limit: 2,
110 skip: 0
111 });
112 }).then(function (res) {
113 res.docs.sort(sortById);
114 res.docs.should.deep.equal([{_id: 'luigi'}, {_id: 'yoshi'}]);
115 });
116 });
117
118 it('should work with $and 2, same index 0-1', function () {
119 var db = context.db;
120 return db.createIndex({
121 "index": {
122 "fields": ["series", "debut"]
123 }
124 }).then(function() {
125 return db.find({
126 selector: {
127 $and: [
128 {series: 'Mario'},
129 {debut: {$gte: 1982}}
130 ]
131 },
132 fields: ['_id'],
133 limit: 0,
134 skip: 1
135 });
136 }).then(function (res) {
137 res.docs.sort(sortById);
138 res.docs.should.deep.equal([]);
139 });
140 });
141
142 it('should work with $and 2, same index 4-2', function () {
143 var db = context.db;
144 return db.createIndex({
145 "index": {
146 "fields": ["series", "debut"]
147 }
148 }).then(function() {
149 return db.find({
150 selector: {
151 $and: [
152 {series: 'Mario'},
153 {debut: {$gte: 1970}}
154 ]
155 },
156 sort: ['series', 'debut'],
157 fields: ['_id'],
158 limit: 4,
159 skip: 2
160 });
161 }).then(function (res) {
162 res.docs.sort(sortById);
163 res.docs.should.deep.equal([
164 {_id: 'luigi'},
165 {_id: 'yoshi'}
166 ]);
167 });
168 });
169
170 it('should work with $and 2, same index 2-2', function () {
171 var db = context.db;
172 return db.createIndex({
173 "index": {
174 "fields": ["series", "debut"]
175 }
176 }).then(function() {
177 return db.find({
178 selector: {
179 $and: [
180 {series: 'Mario'},
181 {debut: {$gte: 1980}}
182 ]
183 },
184 fields: ['_id'],
185 limit: 2,
186 skip: 2
187 });
188 }).then(function (res) {
189 res.docs.sort(sortById);
190 res.docs.should.deep.equal([{_id: 'luigi'}, {_id: 'yoshi'}]);
191 });
192 });
193
194 it('should work with $and 3, index/no-index 10-0', function () {
195 var db = context.db;
196 return db.createIndex({
197 "index": {
198 "fields": ["series"]
199 }
200 }).then(function () {
201 return db.createIndex({
202 "index": {
203 "fields": ["rank"]
204 }
205 });
206 }).then(function() {
207 return db.find({
208 selector: {
209 $and: [
210 {series: 'Mario'},
211 {debut: {$lte: 1990}}
212 ]
213 },
214 fields: ['_id'],
215 limit: 10,
216 skip: 0
217 });
218 }).then(function (res) {
219 res.docs.sort(sortById);
220 res.docs.should.deep.equal([
221 {_id: 'dk'},
222 {_id: 'luigi'},
223 {_id: 'mario'},
224 {_id: 'yoshi'}
225 ]);
226 });
227 });
228
229 it('should work with $and 3, index/no-index 1-0', function () {
230 var db = context.db;
231 return db.createIndex({
232 "index": {
233 "fields": ["series"]
234 }
235 }).then(function () {
236 return db.createIndex({
237 "index": {
238 "fields": ["rank"]
239 }
240 });
241 }).then(function() {
242 return db.find({
243 selector: {
244 $and: [
245 {series: 'Star Fox'},
246 {debut: {$gte: 1982}}
247 ]
248 },
249 fields: ['_id'],
250 limit: 1,
251 skip: 0
252 });
253 }).then(function (res) {
254 res.docs.sort(sortById);
255 res.docs.should.deep.equal([{_id: 'fox'}]);
256 });
257 });
258
259 it('should work with $and 3, index/no-index 2-0', function () {
260 var db = context.db;
261 return db.createIndex({
262 "index": {
263 "fields": ["series"]
264 }
265 }).then(function () {
266 return db.createIndex({
267 "index": {
268 "fields": ["rank"]
269 }
270 });
271 }).then(function() {
272 return db.find({
273 selector: {
274 $and: [
275 {series: 'Mario'},
276 {debut: {$gte: 1983}}
277 ]
278 },
279 fields: ['_id'],
280 limit: 2,
281 skip: 0
282 });
283 }).then(function (res) {
284 res.docs.sort(sortById);
285 res.docs.should.deep.equal([{_id: 'luigi'}, {_id: 'yoshi'}]);
286 });
287 });
288
289 it('should work with $and 4, wrong index', function () {
290 var db = context.db;
291 return db.createIndex({
292 "index": {
293 "fields": ["rank"]
294 }
295 }).then(function() {
296 return db.find({
297 selector: {
298 $and: [
299 {series: 'Mario'},
300 {debut: {$gte: 1990}}
301 ]
302 },
303 fields: ['_id'],
304 limit: 1,
305 skip: 1
306 }).then(function (resp) {
307 resp.should.deep.equal({
308 warning: 'no matching index found, create an index to optimize query time',
309 docs: []
310 });
311 });
312 });
313 });
314 });
315};