1 | 'use strict';
|
2 |
|
3 | var testUtils = require('../test-utils');
|
4 | var sortById = testUtils.sortById;
|
5 |
|
6 | module.exports = function (dbType, context) {
|
7 |
|
8 | describe(dbType + ': limit', 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 limit 0', 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 | return db.find({
|
41 | selector: {
|
42 | $and: [
|
43 | {series: 'Mario'},
|
44 | {debut: {$gte: 1982}}
|
45 | ]
|
46 | },
|
47 | fields: ['_id'],
|
48 | limit: 0
|
49 | });
|
50 | }).then(function (res) {
|
51 | res.docs.sort(sortById);
|
52 | res.docs.should.deep.equal([]);
|
53 | });
|
54 | });
|
55 |
|
56 | it('should work with $and 1 limit 1', function () {
|
57 | var db = context.db;
|
58 | return db.createIndex({
|
59 | "index": {
|
60 | "fields": ["series"]
|
61 | }
|
62 | }).then(function () {
|
63 | return db.createIndex({
|
64 | "index": {
|
65 | "fields": ["debut"]
|
66 | }
|
67 | });
|
68 | }).then(function() {
|
69 | return db.find({
|
70 | selector: {
|
71 | $and: [
|
72 | {series: 'Mario'},
|
73 | {debut: {$gte: 1982}}
|
74 | ]
|
75 | },
|
76 | fields: ['_id'],
|
77 | limit: 1
|
78 | });
|
79 | }).then(function (res) {
|
80 | res.docs.sort(sortById);
|
81 | res.docs.should.deep.equal([{_id: 'luigi'}]);
|
82 | });
|
83 | });
|
84 |
|
85 | it('should work with $and 1 limit 2', function () {
|
86 | var db = context.db;
|
87 | return db.createIndex({
|
88 | "index": {
|
89 | "fields": ["series"]
|
90 | }
|
91 | }).then(function () {
|
92 | return db.createIndex({
|
93 | "index": {
|
94 | "fields": ["debut"]
|
95 | }
|
96 | });
|
97 | }).then(function() {
|
98 | return db.find({
|
99 | selector: {
|
100 | $and: [
|
101 | {series: 'Mario'},
|
102 | {debut: {$gte: 1982}}
|
103 | ]
|
104 | },
|
105 | fields: ['_id'],
|
106 | limit: 2
|
107 | });
|
108 | }).then(function (res) {
|
109 | res.docs.sort(sortById);
|
110 | res.docs.should.deep.equal([{_id: 'luigi'}, {_id: 'yoshi'}]);
|
111 | });
|
112 | });
|
113 |
|
114 | it('should work with $and 2, same index limit 0', function () {
|
115 | var db = context.db;
|
116 | return db.createIndex({
|
117 | "index": {
|
118 | "fields": ["series", "debut"]
|
119 | }
|
120 | }).then(function() {
|
121 | return db.find({
|
122 | selector: {
|
123 | $and: [
|
124 | {series: 'Mario'},
|
125 | {debut: {$gte: 1982}}
|
126 | ]
|
127 | },
|
128 | fields: ['_id'],
|
129 | limit: 0
|
130 | });
|
131 | }).then(function (res) {
|
132 | res.docs.sort(sortById);
|
133 | res.docs.should.deep.equal([]);
|
134 | });
|
135 | });
|
136 |
|
137 | it('should work with $and 2, same index limit 1', function () {
|
138 | var db = context.db;
|
139 | return db.createIndex({
|
140 | "index": {
|
141 | "fields": ["series", "debut"]
|
142 | }
|
143 | }).then(function() {
|
144 | return db.find({
|
145 | selector: {
|
146 | $and: [
|
147 | {series: 'Mario'},
|
148 | {debut: {$gte: 1982}}
|
149 | ]
|
150 | },
|
151 | fields: ['_id'],
|
152 | limit: 1
|
153 | });
|
154 | }).then(function (res) {
|
155 | res.docs.sort(sortById);
|
156 | res.docs.should.deep.equal([{_id: 'luigi'}]);
|
157 | });
|
158 | });
|
159 |
|
160 | it('should work with $and 2, same index limit 2', function () {
|
161 | var db = context.db;
|
162 | return db.createIndex({
|
163 | "index": {
|
164 | "fields": ["series", "debut"]
|
165 | }
|
166 | }).then(function() {
|
167 | return db.find({
|
168 | selector: {
|
169 | $and: [
|
170 | {series: 'Mario'},
|
171 | {debut: {$gte: 1982}}
|
172 | ]
|
173 | },
|
174 | fields: ['_id'],
|
175 | limit: 2
|
176 | });
|
177 | }).then(function (res) {
|
178 | res.docs.sort(sortById);
|
179 | res.docs.should.deep.equal([{_id: 'luigi'}, {_id: 'yoshi'}]);
|
180 | });
|
181 | });
|
182 |
|
183 | it('should work with $and 3, index/no-index limit 0', function () {
|
184 | var db = context.db;
|
185 | return db.createIndex({
|
186 | "index": {
|
187 | "fields": ["series"]
|
188 | }
|
189 | }).then(function () {
|
190 | return db.createIndex({
|
191 | "index": {
|
192 | "fields": ["rank"]
|
193 | }
|
194 | });
|
195 | }).then(function() {
|
196 | return db.find({
|
197 | selector: {
|
198 | $and: [
|
199 | {series: 'Mario'},
|
200 | {debut: {$gte: 1982}}
|
201 | ]
|
202 | },
|
203 | fields: ['_id'],
|
204 | limit: 0
|
205 | });
|
206 | }).then(function (res) {
|
207 | res.docs.sort(sortById);
|
208 | res.docs.should.deep.equal([]);
|
209 | });
|
210 | });
|
211 |
|
212 | it('should work with $and 3, index/no-index limit 1', function () {
|
213 | var db = context.db;
|
214 | return db.createIndex({
|
215 | "index": {
|
216 | "fields": ["series"]
|
217 | }
|
218 | }).then(function () {
|
219 | return db.createIndex({
|
220 | "index": {
|
221 | "fields": ["rank"]
|
222 | }
|
223 | });
|
224 | }).then(function() {
|
225 | return db.find({
|
226 | selector: {
|
227 | $and: [
|
228 | {series: 'Mario'},
|
229 | {debut: {$gte: 1982}}
|
230 | ]
|
231 | },
|
232 | fields: ['_id'],
|
233 | limit: 1
|
234 | });
|
235 | }).then(function (res) {
|
236 | res.docs.sort(sortById);
|
237 | res.docs.should.deep.equal([{_id: 'luigi'}]);
|
238 | });
|
239 | });
|
240 |
|
241 | it('should work with $and 3, index/no-index limit 2', function () {
|
242 | var db = context.db;
|
243 | return db.createIndex({
|
244 | "index": {
|
245 | "fields": ["series"]
|
246 | }
|
247 | }).then(function () {
|
248 | return db.createIndex({
|
249 | "index": {
|
250 | "fields": ["rank"]
|
251 | }
|
252 | });
|
253 | }).then(function() {
|
254 | return db.find({
|
255 | selector: {
|
256 | $and: [
|
257 | {series: 'Mario'},
|
258 | {debut: {$gte: 1983}}
|
259 | ]
|
260 | },
|
261 | fields: ['_id'],
|
262 | limit: 2
|
263 | });
|
264 | }).then(function (res) {
|
265 | res.docs.sort(sortById);
|
266 | res.docs.should.deep.equal([{_id: 'luigi'}, {_id: 'yoshi'}]);
|
267 | });
|
268 | });
|
269 |
|
270 | it('should work with $and 4, wrong index', function () {
|
271 | var db = context.db;
|
272 | return db.createIndex({
|
273 | "index": {
|
274 | "fields": ["rank"]
|
275 | }
|
276 | }).then(function() {
|
277 | return db.find({
|
278 | selector: {
|
279 | $and: [
|
280 | {series: 'Mario'},
|
281 | {debut: {$gte: 1990}}
|
282 | ]
|
283 | },
|
284 | fields: ['_id'],
|
285 | limit: 1
|
286 | }).then(function (resp) {
|
287 | resp.should.deep.equal({
|
288 | warning: 'no matching index found, create an index to optimize query time',
|
289 | docs: [
|
290 | {_id: 'yoshi'}
|
291 | ]
|
292 | });
|
293 | });
|
294 | });
|
295 | });
|
296 | });
|
297 | };
|