|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import { type AggregationCursor, MongoClient } from '../../mongodb'; |
| 4 | + |
| 5 | +describe('class AggregationCursor', () => { |
| 6 | + let client: MongoClient; |
| 7 | + let cursor: AggregationCursor; |
| 8 | + |
| 9 | + beforeEach(async function () { |
| 10 | + client = new MongoClient('mongodb://iLoveJavascript'); |
| 11 | + cursor = client.db().aggregate(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(async function () { |
| 15 | + await cursor.close(); |
| 16 | + await client.close(); |
| 17 | + }); |
| 18 | + |
| 19 | + context('get pipeline()', () => { |
| 20 | + it('returns the current aggregation pipeline', () => { |
| 21 | + expect(cursor.pipeline).to.deep.equal([]); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + context('clone()', () => { |
| 26 | + it('returns a new cursor with a different session', () => { |
| 27 | + const cloned = cursor.clone(); |
| 28 | + expect(cursor).to.not.equal(cloned); |
| 29 | + expect(cursor.session).to.not.equal(cloned.session); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + context('map()', () => { |
| 34 | + /* |
| 35 | + * map does not actually return a new cursor, |
| 36 | + * the method exists to allow typescript to redefine the output based on the transform |
| 37 | + */ |
| 38 | + it('returns the same cursor instance', () => { |
| 39 | + const mappedCursor = cursor.map(() => ({ blah: 1 })); |
| 40 | + expect(cursor).to.equal(mappedCursor); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + context('geoNear()', () => { |
| 45 | + it('adds a $geoNear stage', () => { |
| 46 | + cursor.geoNear({ lat: 1, lon: 1 }); |
| 47 | + expect(cursor.pipeline).to.have.deep.property('0', { $geoNear: { lat: 1, lon: 1 } }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + context('unwind()', () => { |
| 52 | + it('adds a $unwind stage', () => { |
| 53 | + cursor.unwind({ blah: 1 }); |
| 54 | + expect(cursor.pipeline).to.have.deep.property('0', { $unwind: { blah: 1 } }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + context('sort()', () => { |
| 59 | + it('adds a $sort stage', () => { |
| 60 | + cursor.sort({ _id: -1 }); |
| 61 | + expect(cursor.pipeline).to.have.deep.property('0', { $sort: { _id: -1 } }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + context('skip()', () => { |
| 66 | + it('adds a $skip stage', () => { |
| 67 | + cursor.skip(2); |
| 68 | + expect(cursor.pipeline).to.have.deep.property('0', { $skip: 2 }); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + context('redact()', () => { |
| 73 | + it('adds a $redact stage', () => { |
| 74 | + cursor.redact({ redact: true }); |
| 75 | + expect(cursor.pipeline).to.have.deep.property('0', { $redact: { redact: true } }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + context('lookup()', () => { |
| 80 | + it('adds a $lookup stage', () => { |
| 81 | + cursor.redact({ lookup: true }); |
| 82 | + expect(cursor.pipeline).to.have.deep.property('0', { $redact: { lookup: true } }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + context('project()', () => { |
| 87 | + it('adds a $project stage', () => { |
| 88 | + cursor.project({ project: true }); |
| 89 | + expect(cursor.pipeline).to.have.deep.property('0', { $project: { project: true } }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + context('out()', () => { |
| 94 | + it('adds a $out stage', () => { |
| 95 | + cursor.out({ db: 'a', coll: 'b' }); |
| 96 | + expect(cursor.pipeline).to.have.deep.property('0', { $out: { db: 'a', coll: 'b' } }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + context('match()', () => { |
| 101 | + it('adds a $match stage', () => { |
| 102 | + cursor.match({ match: true }); |
| 103 | + expect(cursor.pipeline).to.have.deep.property('0', { $match: { match: true } }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + context('limit()', () => { |
| 108 | + it('adds a $limit stage', () => { |
| 109 | + cursor.limit(2); |
| 110 | + expect(cursor.pipeline).to.have.deep.property('0', { $limit: 2 }); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + context('group()', () => { |
| 115 | + it('adds a $group stage', () => { |
| 116 | + cursor.group({ group: true }); |
| 117 | + expect(cursor.pipeline).to.have.deep.property('0', { $group: { group: true } }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + context('addStage()', () => { |
| 122 | + it('adds an arbitrary stage', () => { |
| 123 | + cursor.addStage({ $iLoveJavascriptStage: { yes: true } }); |
| 124 | + expect(cursor.pipeline).to.have.deep.property('0', { $iLoveJavascriptStage: { yes: true } }); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + context('when addStage, bespoke stage methods, or array is used to construct pipeline', () => { |
| 129 | + it('sets deeply identical aggregations pipelines', () => { |
| 130 | + const collection = client.db().collection('test'); |
| 131 | + |
| 132 | + const expectedPipeline = [ |
| 133 | + { $project: { author: 1, tags: 1 } }, |
| 134 | + { $unwind: '$tags' }, |
| 135 | + { $group: { _id: { tags: '$tags' }, authors: { $addToSet: '$author' } } }, |
| 136 | + { $sort: { _id: -1 } } |
| 137 | + ]; |
| 138 | + |
| 139 | + const arrayPipelineCursor = collection.aggregate(Array.from(expectedPipeline)); |
| 140 | + |
| 141 | + const builderPipelineCursor = collection |
| 142 | + .aggregate() |
| 143 | + .project({ author: 1, tags: 1 }) |
| 144 | + .unwind('$tags') |
| 145 | + .group({ _id: { tags: '$tags' }, authors: { $addToSet: '$author' } }) |
| 146 | + .sort({ _id: -1 }); |
| 147 | + |
| 148 | + const builderGenericStageCursor = collection |
| 149 | + .aggregate() |
| 150 | + .addStage({ $project: { author: 1, tags: 1 } }) |
| 151 | + .addStage({ $unwind: '$tags' }) |
| 152 | + .addStage({ $group: { _id: { tags: '$tags' }, authors: { $addToSet: '$author' } } }) |
| 153 | + .addStage({ $sort: { _id: -1 } }); |
| 154 | + |
| 155 | + expect(arrayPipelineCursor.pipeline).to.deep.equal(expectedPipeline); |
| 156 | + expect(builderPipelineCursor.pipeline).to.deep.equal(expectedPipeline); |
| 157 | + expect(builderGenericStageCursor.pipeline).to.deep.equal(expectedPipeline); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments