UNPKG

2.17 kBJavaScriptView Raw
1'use strict';
2
3module.exports = function (dbType, context) {
4
5 describe(dbType + ': $elemMatch', function () {
6 if (dbType === 'http') { return;}
7
8 beforeEach(function () {
9 return context.db.bulkDocs([
10 {'_id': 'peach', eats: ['cake', 'turnips', 'sweets'], results: [ 82, 85, 88 ]},
11 {'_id': 'sonic', eats: ['chili dogs'], results: [ 75, 88, 89 ]},
12 {'_id': 'fox', eats: []},
13 {'_id': 'mario', eats: ['cake', 'mushrooms']},
14 {'_id': 'samus', eats: ['pellets']},
15 {'_id': 'kirby', eats: 'anything', results: [ 82, 86, 10 ]}
16 ]);
17 });
18
19 it('basic test', function () {
20 var db = context.db;
21 return db.find({
22 selector: {
23 _id: {$gt: 'a'},
24 eats: {$elemMatch: {$eq: 'cake'}}
25 }
26 }).then(function (resp) {
27 resp.docs.map(function (doc) {
28 return doc._id;
29 }).sort().should.deep.equal(['mario', 'peach']);
30 });
31 });
32
33 it('basic test with two operators', function () {
34 var db = context.db;
35 return db.find({
36 selector: {
37 _id: {$gt: 'a'},
38 results: {$elemMatch: {$gte: 80, $lt: 85}}
39 }
40 }).then(function (resp) {
41 resp.docs.map(function (doc) {
42 return doc._id;
43 }).should.deep.equal(['kirby', 'peach']);
44 });
45 });
46
47 it('with object in array', function () {
48 var db = context.db;
49 var docs = [
50 {_id: '1', events: [{eventId: 1, status: 'completed'}, {eventId: 2, status: 'started'}]},
51 {_id: '2', events: [{eventId: 1, status: 'pending'}, {eventId: 2, status: 'finished'}]},
52 {_id: '3', events: [{eventId: 1, status: 'pending'}, {eventId: 2, status: 'started'}]},
53 ];
54
55 return db.bulkDocs(docs).then(function () {
56 return db.find({
57 selector: {
58 _id: {$gt: null},
59 events: {$elemMatch: {"status": {$eq: 'pending'}, "eventId": {$eq: 1}}},
60 },
61 fields: ['_id']
62 }).then(function (resp) {
63 resp.docs.map(function (doc) {
64 return doc._id;
65 }).should.deep.equal(['2', '3']);
66 });
67 });
68 });
69 });
70};