Skip to content

Commit 322d9e8

Browse files
test(NODE-6099): add aggregation building unit coverage (#4088)
Co-authored-by: Aditi Khare <[email protected]>
1 parent a6882ec commit 322d9e8

File tree

2 files changed

+160
-64
lines changed

2 files changed

+160
-64
lines changed

test/integration/crud/aggregation.test.ts

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -939,68 +939,4 @@ describe('Aggregation', function () {
939939
.finally(() => client.close());
940940
}
941941
});
942-
943-
it('should return identical results for array aggregations and builder aggregations', async function () {
944-
const databaseName = this.configuration.db;
945-
const db = client.db(databaseName);
946-
const collection = db.collection(
947-
'shouldReturnIdenticalResultsForArrayAggregationsAndBuilderAggregations'
948-
);
949-
950-
const docs = [
951-
{
952-
title: 'this is my title',
953-
author: 'bob',
954-
posted: new Date(),
955-
pageViews: 5,
956-
tags: ['fun', 'good', 'fun'],
957-
other: { foo: 5 },
958-
comments: [
959-
{ author: 'joe', text: 'this is cool' },
960-
{ author: 'sam', text: 'this is bad' }
961-
]
962-
}
963-
];
964-
965-
await collection.insertMany(docs, { writeConcern: { w: 1 } });
966-
const arrayPipelineCursor = collection.aggregate([
967-
{
968-
$project: {
969-
author: 1,
970-
tags: 1
971-
}
972-
},
973-
{ $unwind: '$tags' },
974-
{
975-
$group: {
976-
_id: { tags: '$tags' },
977-
authors: { $addToSet: '$author' }
978-
}
979-
},
980-
{ $sort: { _id: -1 } }
981-
]);
982-
983-
const builderPipelineCursor = collection
984-
.aggregate()
985-
.project({ author: 1, tags: 1 })
986-
.unwind('$tags')
987-
.group({ _id: { tags: '$tags' }, authors: { $addToSet: '$author' } })
988-
.sort({ _id: -1 });
989-
990-
const builderGenericStageCursor = collection
991-
.aggregate()
992-
.addStage({ $project: { author: 1, tags: 1 } })
993-
.addStage({ $unwind: '$tags' })
994-
.addStage({ $group: { _id: { tags: '$tags' }, authors: { $addToSet: '$author' } } })
995-
.addStage({ $sort: { _id: -1 } });
996-
997-
const expectedResults = [
998-
{ _id: { tags: 'good' }, authors: ['bob'] },
999-
{ _id: { tags: 'fun' }, authors: ['bob'] }
1000-
];
1001-
1002-
expect(await arrayPipelineCursor.toArray()).to.deep.equal(expectedResults);
1003-
expect(await builderPipelineCursor.toArray()).to.deep.equal(expectedResults);
1004-
expect(await builderGenericStageCursor.toArray()).to.deep.equal(expectedResults);
1005-
});
1006942
});
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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.lookup({ lookup: true });
82+
expect(cursor.pipeline).to.have.deep.property('0', { $lookup: { 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

Comments
 (0)