UNPKG

5.03 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 + ': exists', function () {
9
10 it('does $exists queries - true', function () {
11 var db = context.db;
12 return db.bulkDocs([
13 {_id: 'a', foo: 'bar'},
14 {_id: 'b', foo: {yo: 'dude'}},
15 {_id: 'c', foo: null},
16 {_id: 'd'}
17 ]).then(function () {
18 return db.find({
19 selector: {
20 _id: { $gt: null},
21 'foo': {'$exists': true}
22 },
23 fields: ['_id']
24 });
25 }).then(function (res) {
26 res.docs.sort(sortById);
27 res.docs.should.deep.equal([
28 {"_id": "a"},
29 {"_id": "b"},
30 {"_id": "c"}
31 ]);
32 });
33 });
34
35 it('does $exists queries - false', function () {
36 var db = context.db;
37 return db.bulkDocs([
38 {_id: 'a', foo: 'bar'},
39 {_id: 'b', foo: {yo: 'dude'}},
40 {_id: 'c', foo: null},
41 {_id: 'd'}
42 ]).then(function () {
43 return db.find({
44 selector: {
45 _id: { $gt: null},
46 'foo': {'$exists': false}
47 },
48 fields: ['_id']
49 });
50 }).then(function (res) {
51 res.docs.sort(sortById);
52 res.docs.should.deep.equal([
53 {"_id": "d"}
54 ]);
55 });
56 });
57
58 it('does $exists queries - true/undef (multi-field)', function () {
59 var db = context.db;
60 return db.bulkDocs([
61 {_id: 'a', foo: 'bar', bar: 'baz'},
62 {_id: 'b', foo: {yo: 'dude'}},
63 {_id: 'c', foo: null, bar: 'quux'},
64 {_id: 'd'}
65 ]).then(function () {
66 return db.find({
67 selector: {
68 _id: { $gt: null},
69 'foo': {'$exists': true}
70 },
71 fields: ['_id']
72 });
73 }).then(function (res) {
74 res.docs.sort(sortById);
75 res.docs.should.deep.equal([
76 {"_id": "a"},
77 {"_id": "b"},
78 {"_id": "c"}
79 ]);
80 });
81 });
82
83 it('does $exists queries - $eq/true (multi-field)', function () {
84 var db = context.db;
85 var index = {
86 "index": {
87 "fields": ["foo"]
88 },
89 "name": "foo-index",
90 "type": "json"
91 };
92 return db.createIndex(index).then(function () {
93 return db.bulkDocs([
94 {_id: 'a', foo: 'bar', bar: 'baz'},
95 {_id: 'b', foo: 'bar', bar: {yo: 'dude'}},
96 {_id: 'c', foo: null, bar: 'quux'},
97 {_id: 'd'}
98 ]);
99 }).then(function () {
100 return db.find({
101 selector: {'foo': 'bar', bar: {$exists: true}},
102 fields: ['_id']
103 });
104 }).then(function (res) {
105 res.docs.sort(sortById);
106 res.docs.should.deep.equal([
107 {"_id": "a"},
108 {"_id": "b"}
109 ]);
110 });
111 });
112
113 it('does $exists queries - $eq/false (multi-field)', function () {
114 var db = context.db;
115 var index = {
116 "index": {
117 "fields": ["foo"]
118 },
119 "name": "foo-index",
120 "type": "json"
121 };
122 return db.createIndex(index).then(function () {
123 return db.bulkDocs([
124 {_id: 'a', foo: 'bar', bar: 'baz'},
125 {_id: 'b', foo: 'bar', bar: {yo: 'dude'}},
126 {_id: 'c', foo: 'bar', bar: 'yo'},
127 {_id: 'd', foo: 'bar'}
128 ]);
129 }).then(function () {
130 return db.find({
131 selector: {'foo': 'bar', bar: {$exists: false}},
132 fields: ['_id']
133 });
134 }).then(function (res) {
135 res.docs.sort(sortById);
136 res.docs.should.deep.equal([
137 {"_id": "d"}
138 ]);
139 });
140 });
141
142 it('does $exists queries - true/true (multi-field)', function () {
143 var db = context.db;
144 return db.bulkDocs([
145 {_id: 'a', foo: 'bar', bar: 'baz'},
146 {_id: 'b', foo: {yo: 'dude'}},
147 {_id: 'c', foo: null, bar: 'quux'},
148 {_id: 'd'}
149 ]).then(function () {
150 return db.find({
151 selector: {
152 _id: {$gt: null},
153 foo: {'$exists': true},
154 bar: {$exists: true}
155 },
156 fields: ['_id']
157 });
158 }).then(function (res) {
159 res.docs.sort(sortById);
160 res.docs.should.deep.equal([
161 {"_id": "a"},
162 {"_id": "c"}
163 ]);
164 });
165 });
166
167 it('does $exists queries - true/false (multi-field)', function () {
168 var db = context.db;
169 return db.bulkDocs([
170 {_id: 'a', foo: 'bar', bar: 'baz'},
171 {_id: 'b', foo: {yo: 'dude'}},
172 {_id: 'c', foo: null, bar: 'quux'},
173 {_id: 'd'}
174 ]).then(function () {
175 return db.find({
176 selector: {
177 _id: {$gt: null},
178 foo: {'$exists': true},
179 bar: {$exists: false}
180 },
181 fields: ['_id']
182 });
183 }).then(function (res) {
184 res.docs.sort(sortById);
185 res.docs.should.deep.equal([
186 {"_id": "b"}
187 ]);
188 });
189 });
190 });
191};