UNPKG

3.83 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 + ': set-operations', 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', 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: 1990}}
45 ]
46 },
47 fields: ['_id']
48 });
49 }).then(function (res) {
50 res.docs.sort(sortById);
51 res.docs.should.deep.equal([{_id: 'yoshi'}]);
52 });
53 });
54
55 it('should work with $and 2, same index', function () {
56 var db = context.db;
57 return db.createIndex({
58 "index": {
59 "fields": ["series", "debut"]
60 }
61 }).then(function() {
62 return db.find({
63 selector: {
64 $and: [
65 {series: 'Mario'},
66 {debut: {$gte: 1990}}
67 ]
68 },
69 fields: ['_id']
70 });
71 }).then(function (res) {
72 res.docs.sort(sortById);
73 res.docs.should.deep.equal([{_id: 'yoshi'}]);
74 });
75 });
76
77 it('should work with $and 3, index/no-index', function () {
78 var db = context.db;
79 return db.createIndex({
80 "index": {
81 "fields": ["series"]
82 }
83 }).then(function () {
84 return db.createIndex({
85 "index": {
86 "fields": ["rank"]
87 }
88 });
89 }).then(function() {
90 return db.find({
91 selector: {
92 $and: [
93 {series: 'Mario'},
94 {debut: {$gte: 1990}}
95 ]
96 },
97 fields: ['_id']
98 });
99 }).then(function (res) {
100 res.docs.sort(sortById);
101 res.docs.should.deep.equal([{_id: 'yoshi'}]);
102 });
103 });
104
105 it('should work with $and 4, wrong index', function () {
106 var db = context.db;
107 return db.createIndex({
108 "index": {
109 "fields": ["rank"]
110 }
111 }).then(function() {
112 return db.find({
113 selector: {
114 $and: [
115 {series: 'Mario'},
116 {debut: {$gte: 1990}}
117 ]
118 },
119 fields: ['_id']
120 }).then(function (resp) {
121 resp.should.deep.equal({
122 warning: 'no matching index found, create an index to optimize query time',
123 docs: [
124 {_id: 'yoshi'}
125 ]
126 });
127 });
128 });
129 });
130 });
131};