|
| 1 | +const { expect } = require('chai'); |
| 2 | + |
| 3 | +const { build } = require('../src/lang/filter.js'), |
| 4 | + Fields = require('../src/lang/fields.js'), |
| 5 | + waterfall = require('./waterfall.js'); |
| 6 | + |
| 7 | +const db = new zango.Db(Math.random(), { col: ['x', 'g'] }); |
| 8 | +const col = db.collection('col'); |
| 9 | + |
| 10 | +const docs = [ |
| 11 | + { x: 2, g: 3 }, |
| 12 | + { x: 2, g: 8 }, |
| 13 | + { x: 2, g: 8, z: 10 }, |
| 14 | + { x: 3, z: 3 }, |
| 15 | + { x: 4, k: 8 }, |
| 16 | + { x: 6, g: 9 }, |
| 17 | + { x: 10, k: 4 }, |
| 18 | + { x: undefined }, |
| 19 | + { x: null }, |
| 20 | + { x: [{ k: 2 }, { k: 8 }] } |
| 21 | +]; |
| 22 | + |
| 23 | +before(() => col.insert(docs)); |
| 24 | +after(() => db.drop()); |
| 25 | + |
| 26 | +const query = (expr, done) => { |
| 27 | + const cur = col.findOne(expr); |
| 28 | + |
| 29 | + const pred = build(expr); |
| 30 | + const fn = doc => pred.run(new Fields(doc)); |
| 31 | + |
| 32 | + const expected_doc = docs.filter(fn)[0]; |
| 33 | + expect(expected_doc).to.exist; |
| 34 | + |
| 35 | + cur.then(doc => { |
| 36 | + delete doc._id; |
| 37 | + expect(expected_doc).to.deep.equal(doc); |
| 38 | + done(); |
| 39 | + }).catch((e) => console.log(e)); |
| 40 | +}; |
| 41 | + |
| 42 | +describe('equality (without $eq)', () => { |
| 43 | + it('should test for equality', (done) => { |
| 44 | + waterfall([ |
| 45 | + next => query({ x: 4 }, next), |
| 46 | + next => query({ x: undefined }, next), |
| 47 | + next => query({ x: null }, next) |
| 48 | + ], done); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('$eq', () => { |
| 53 | + it('should test for equality', (done) => { |
| 54 | + query({ x: { $eq: 4 } }, done); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('conjunction without $and', () => { |
| 59 | + it('should perform conjunction', (done) => { |
| 60 | + query({ x: 2, g: 3 }, done); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('$and', () => { |
| 65 | + it('should perform conjunction', (done) => { |
| 66 | + query({ $and: [{ x: 2 }, { g: 3 }] }, done); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe('$or', () => { |
| 71 | + it('should perform disjunction', (done) => { |
| 72 | + waterfall([ |
| 73 | + next => query({ $or: [{ x: 2 }, { g: 3 }] }, next), |
| 74 | + |
| 75 | + (next) => { |
| 76 | + query({ |
| 77 | + $or: [{ $or: [{ x: 2 }, { g: 3 }] }, { z: 8 }] |
| 78 | + }, next); |
| 79 | + } |
| 80 | + ], done); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('$gt', () => { |
| 85 | + it('should perform greater than comparison', (done) => { |
| 86 | + query({ x: { $gt: 3 } }, done); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('$gte', () => { |
| 91 | + it('should perform greater than or equal comparison', (done) => { |
| 92 | + query({ x: { $gte: 6 } }, done); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('$lt', () => { |
| 97 | + it('should perform less than comparison', (done) => { |
| 98 | + query({ x: { $lt: 8 } }, done); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('$lte', () => { |
| 103 | + it('should perform less than or equal comparison', (done) => { |
| 104 | + query({ x: { $lte: 6 } }, done); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('$gt and $lt', () => { |
| 109 | + it('should perform gt and lt comparison', (done) => { |
| 110 | + query({ x: { $gt: 3, $lt: 8 } }, done); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('$gte and $lt', () => { |
| 115 | + it('should perform gte and lt comparison', (done) => { |
| 116 | + query({ x: { $gte: 3, $lt: 8 } }, done); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('$gt and $lte', () => { |
| 121 | + it('should perform gt and lte comparison', (done) => { |
| 122 | + query({ x: { $gt: 3, $lte: 8 } }, done); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('$gte and $lte', () => { |
| 127 | + it('should perform gte and lte comparison', (done) => { |
| 128 | + query({ x: { $gte: 3, $lte: 8 } }, done); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe('$in', () => { |
| 133 | + it('should perform disjunction equality test', (done) => { |
| 134 | + query({ x: { $in: [2, 4, 8] } }, done); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('$nin', () => { |
| 139 | + it('should perform conjunction inequality test', (done) => { |
| 140 | + query({ x: { $nin: [2, 4, 8] } }, done); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('$elemMatch', () => { |
| 145 | + it('should test if any iterable elements satisify a predicate', (done) => { |
| 146 | + query({ x: { $elemMatch: { k: 8 } } }, done); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
| 150 | +describe('$exists', () => { |
| 151 | + it('should test if document contains a field', (done) => { |
| 152 | + query({ g: { $exists: 1 } }, done); |
| 153 | + }); |
| 154 | + |
| 155 | + it("should test if document doesn't contain a field", (done) => { |
| 156 | + query({ g: { $exists: 0 } }, done); |
| 157 | + }); |
| 158 | +}); |
0 commit comments