-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathcollection-aggregations-tab.test.ts
1783 lines (1469 loc) · 59 KB
/
collection-aggregations-tab.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import chai from 'chai';
import { promises as fs } from 'fs';
import type { CompassBrowser } from '../helpers/compass-browser';
import {
init,
cleanup,
screenshotIfFailed,
outputFilename,
serverSatisfies,
skipForWeb,
DEFAULT_CONNECTION_NAME_1,
} from '../helpers/compass';
import type { Compass } from '../helpers/compass';
import * as Selectors from '../helpers/selectors';
import {
createNestedDocumentsCollection,
createNumbersCollection,
} from '../helpers/insert-data';
import { saveAggregationPipeline } from '../helpers/commands/save-aggregation-pipeline';
import { Key } from 'webdriverio';
import type { ChainablePromiseElement } from 'webdriverio';
import { switchPipelineMode } from '../helpers/commands/switch-pipeline-mode';
const { expect } = chai;
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const OUT_STAGE_PREVIEW_TEXT =
'The $out operator will cause the pipeline to persist the results to the specified location (collection, S3, or Atlas). If the collection exists it will be replaced.';
const MERGE_STAGE_PREVIEW_TEXT =
'The $merge operator will cause the pipeline to persist the results to the specified location.';
const STAGE_WIZARD_GUIDE_CUE_STORAGE_KEY = 'has_seen_stage_wizard_guide_cue';
async function waitForAnyText(
browser: CompassBrowser,
element: ChainablePromiseElement
) {
await browser.waitUntil(async () => {
const text = await element.getText();
return text !== '';
});
}
async function goToRunAggregation(browser: CompassBrowser) {
if (await browser.$(Selectors.AggregationBuilderWorkspace).isDisplayed()) {
await browser.clickVisible(Selectors.RunPipelineButton);
}
const resultsWorkspace = browser.$(Selectors.AggregationResultsWorkspace);
await resultsWorkspace.waitForDisplayed();
}
async function goToEditPipeline(browser: CompassBrowser) {
if (await browser.$(Selectors.AggregationResultsWorkspace).isDisplayed()) {
await browser.clickVisible(Selectors.EditPipelineButton);
}
const builderWorkspace = browser.$(Selectors.AggregationBuilderWorkspace);
await builderWorkspace.waitForDisplayed();
}
async function getDocuments(browser: CompassBrowser) {
// Switch to JSON view so it's easier to get document value
await browser.clickVisible(Selectors.AggregationResultsJSONListSwitchButton);
const documents = await browser.getCodemirrorEditorTextAll(
Selectors.DocumentJSONEntry
);
return documents.map((text) => {
return JSON.parse(text);
});
}
async function waitForTab(browser: CompassBrowser, namespace: string) {
await browser.waitUntil(
async function () {
const ns = await browser.getActiveTabNamespace();
return ns === namespace;
},
{
timeoutMsg: `Expected \`${namespace}\` namespace tab to be visible`,
}
);
}
async function deleteStage(
browser: CompassBrowser,
index: number
): Promise<void> {
await browser.clickVisible(Selectors.stageMoreOptions(index));
const menuElement = browser.$(Selectors.StageMoreOptionsContent);
await menuElement.waitForDisplayed();
await browser.clickVisible(Selectors.StageDelete);
}
function getStageContainers(browser: CompassBrowser) {
return browser.$$(Selectors.StageCard);
}
async function addStage(browser: CompassBrowser, expectedStages: number) {
expect(await getStageContainers(browser).length).to.equal(expectedStages - 1);
await browser.clickVisible(Selectors.AddStageButton);
await browser.$(Selectors.stageEditor(expectedStages - 1)).waitForDisplayed();
expect(await getStageContainers(browser).length).to.equal(expectedStages);
}
describe('Collection aggregations tab', function () {
let compass: Compass;
let browser: CompassBrowser;
before(async function () {
compass = await init(this.test?.fullTitle());
browser = compass.browser;
await browser.setupDefaultConnections();
});
beforeEach(async function () {
await createNumbersCollection();
await createNestedDocumentsCollection('nestedDocs', 10);
await browser.disconnectAll();
await browser.connectToDefaults();
// set guide cue to not show up
await browser.execute((key) => {
// eslint-disable-next-line no-restricted-globals
localStorage.setItem(key, 'true');
}, STAGE_WIZARD_GUIDE_CUE_STORAGE_KEY);
// Some tests navigate away from the numbers collection aggregations tab
await browser.navigateToCollectionTab(
DEFAULT_CONNECTION_NAME_1,
'test',
'numbers',
'Aggregations'
);
// Get us back to the empty stage every time. Also test the Create New
// Pipeline flow while at it.
await browser.clickVisible(Selectors.CreateNewPipelineButton);
// This is kinda superfluous for the nested beforeEach hooks below where we
// immediately navigate away anyway, but most tests expect there to already
// be one stage.
await addStage(browser, 1);
});
after(async function () {
await cleanup(compass);
});
afterEach(async function () {
await screenshotIfFailed(compass, this.currentTest);
});
it('supports the right stages for the environment', async function () {
const options = await browser.getStageOperators(0);
const expectedAggregations = [
'$addFields',
'$bucket',
'$bucketAuto',
'$collStats',
'$count',
'$facet',
'$geoNear',
'$graphLookup',
'$group',
'$indexStats',
'$limit',
'$lookup',
'$match',
'$out',
'$project',
'$redact',
'$replaceRoot',
'$sample',
'$skip',
'$sort',
'$sortByCount',
'$unwind',
];
if (serverSatisfies('>= 4.1.11')) {
expectedAggregations.push('$search');
}
if (serverSatisfies('>= 4.2.0')) {
expectedAggregations.push('$merge', '$replaceWith', '$set', '$unset');
}
if (serverSatisfies('>= 4.4.0')) {
expectedAggregations.push('$unionWith');
}
if (serverSatisfies('>= 4.4.9')) {
expectedAggregations.push('$searchMeta');
}
if (serverSatisfies('>= 5.0.0')) {
expectedAggregations.push('$setWindowFields');
}
if (serverSatisfies('>= 5.1.0')) {
expectedAggregations.push('$densify');
}
if (serverSatisfies('>= 5.3.0')) {
expectedAggregations.push('$fill');
}
if (serverSatisfies('>=6.0.10 <7.0.0 || >=7.0.2')) {
expectedAggregations.push('$vectorSearch');
}
expectedAggregations.sort();
expect(options).to.deep.equal(expectedAggregations);
});
// TODO: we can probably remove this one now that there is a more advanced one. or merge that into here?
it('supports creating an aggregation', async function () {
await browser.selectStageOperator(0, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'{ i: 0 }'
);
await browser.waitUntil(async function () {
const textElement = browser.$(Selectors.stagePreviewToolbarTooltip(0));
const text = await textElement.getText();
return text === '(Sample of 1 document)';
});
});
it('shows atlas only stage preview', async function () {
if (serverSatisfies('< 4.1.11')) {
this.skip();
}
await browser.selectStageOperator(0, '$search');
await browser.waitUntil(async function () {
const textElement = browser.$(Selectors.stagePreview(0));
const text = await textElement.getText();
return text.includes(
'The $search stage is only available with MongoDB Atlas.'
);
});
});
it('shows $out stage preview', async function () {
await browser.selectStageOperator(0, '$out');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'"listings"'
);
const preview = browser.$(Selectors.stagePreview(0));
const text = await preview.getText();
expect(text).to.include('Documents will be saved to test.listings.');
expect(text).to.include(OUT_STAGE_PREVIEW_TEXT);
});
it('shows $merge stage preview', async function () {
// $merge operator is supported from 4.2.0
if (serverSatisfies('< 4.2.0')) {
return this.skip();
}
await browser.selectStageOperator(0, '$merge');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'"listings"'
);
const preview = browser.$(Selectors.stagePreview(0));
const text = await preview.getText();
expect(text).to.include('Documents will be saved to test.listings.');
expect(text).to.include(MERGE_STAGE_PREVIEW_TEXT);
});
it('shows empty preview', async function () {
await browser.selectStageOperator(0, '$addFields');
await browser.waitUntil(async function () {
const textElement = browser.$(Selectors.stagePreviewEmpty(0));
const text = await textElement.getText();
return text === 'No Preview Documents';
});
});
it('supports tweaking settings of an aggregation and saving aggregation as a view', async function () {
// set a collation
await browser.clickVisible(Selectors.AggregationAdditionalOptionsButton);
await browser.setValueVisible(
Selectors.AggregationCollationInput,
'{ locale: "af" }'
);
// select $match
await browser.selectStageOperator(0, '$match');
// check that it included the comment by default
const contentElement0 = browser.$(Selectors.stageContent(0));
// It starts out empty
await waitForAnyText(browser, contentElement0);
expect(await contentElement0.getText()).to.equal(`/**
* query: The query in MQL.
*/
{
query
}`);
//change $match
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'{ i: { $gt: 5 } }'
);
// TODO: click collapse and then expand again
// open settings
await browser.clickVisible(Selectors.AggregationSettingsButton);
// turn off comment mode
await browser.clickParent(Selectors.AggregationCommentModeCheckbox);
// set number of preview documents to 100
await browser.setValueVisible(Selectors.AggregationSampleSizeInput, '100');
// apply settings
await browser.clickVisible(Selectors.AggregationSettingsApplyButton);
// add a $project
await addStage(browser, 2);
await browser.selectStageOperator(1, '$project');
// delete it
await deleteStage(browser, 1);
// add a $project
await addStage(browser, 2);
await browser.selectStageOperator(1, '$project');
// check that it has no comment
const contentElement1 = browser.$(Selectors.stageContent(1));
// starts empty
await waitForAnyText(browser, contentElement1);
expect(await contentElement1.getText()).to.equal(`{
specification(s)
}`);
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(1),
'{ _id: 0 }'
);
// disable it
await browser.clickVisible(Selectors.stageToggle(1));
await browser.waitUntil(
async () => {
const stageToggle = browser.$(Selectors.stageToggle(1));
return (await stageToggle.getAttribute('aria-checked')) === 'false';
},
{ timeoutMsg: 'Expected stage toggle to be turned off' }
);
// export to language
await browser.clickVisible(Selectors.ExportAggregationToLanguage);
const text = await browser.exportToLanguage('Ruby');
expect(text).to.equal(`[
{
'$match' => {
'i' => {
'$gt' => 5
}
}
}
]`);
// check that the preview is using 100 docs
await browser.waitUntil(async function () {
const textElement = browser.$(Selectors.stagePreviewToolbarTooltip(0));
const text = await textElement.getText();
return text === '(Sample of 100 documents)';
});
// Wait until the isCreateViewAvailable prop is changed
// and the "Create view" action is available in the Save button menu.
await browser.waitUntil(async () => {
await browser.clickVisible(Selectors.SavePipelineMenuButton);
const savePipelineCreateViewAction = browser.$(
Selectors.SavePipelineCreateViewAction
);
const savePipelineCreateViewActionExisting =
await savePipelineCreateViewAction.isExisting();
return savePipelineCreateViewActionExisting;
});
await browser.clickVisible(Selectors.SavePipelineCreateViewAction);
// wait for the modal to appear
const createViewModal = browser.$(Selectors.CreateViewModal);
await createViewModal.waitForDisplayed();
// set view name
await browser.setValueVisible(
Selectors.CreateViewNameInput,
'my-view-from-pipeline'
);
// click create button
const createButton = browser
.$(Selectors.CreateViewModal)
.$('button=Create');
await createButton.click();
// wait until the active tab is the view that we just created
await waitForTab(browser, 'test.my-view-from-pipeline');
// choose Duplicate view
await browser.selectCollectionMenuItem(
DEFAULT_CONNECTION_NAME_1,
'test',
'my-view-from-pipeline',
'duplicate-view'
);
const duplicateModal = browser.$(Selectors.DuplicateViewModal);
// wait for the modal, fill out the modal, confirm
await duplicateModal.waitForDisplayed();
await browser.setValueVisible(
Selectors.DuplicateViewModalTextInput,
'duplicated-view'
);
const confirmDuplicateButton = browser.$(
Selectors.DuplicateViewModalConfirmButton
);
await confirmDuplicateButton.waitForEnabled();
await confirmDuplicateButton.click();
await duplicateModal.waitForDisplayed({ reverse: true });
// wait for the active tab to become the newly duplicated view
await waitForTab(browser, 'test.duplicated-view');
// now select modify view of the non-duplicate
await browser.selectCollectionMenuItem(
DEFAULT_CONNECTION_NAME_1,
'test',
'my-view-from-pipeline',
'modify-view'
);
// wait for the active tab to become the numbers collection (because that's what the pipeline representing the view is for)
await waitForTab(browser, 'test.numbers');
// make sure we're on the aggregations tab, in edit mode
const modifyBanner = browser.$(Selectors.ModifySourceBanner);
await modifyBanner.waitForDisplayed();
expect(await modifyBanner.getText()).to.equal(
'MODIFYING PIPELINE BACKING "TEST.MY-VIEW-FROM-PIPELINE"'
);
});
describe('maxTimeMS', function () {
before(function () {
skipForWeb(
this,
"we don't support getFeature() and setFeature() in compass-web yet"
);
});
let maxTimeMSBefore: any;
beforeEach(async function () {
maxTimeMSBefore = await browser.getFeature('maxTimeMS');
});
afterEach(async function () {
await browser.setFeature('maxTimeMS', maxTimeMSBefore);
});
for (const maxTimeMSMode of ['ui', 'preference'] as const) {
it(`supports maxTimeMS (set via ${maxTimeMSMode})`, async function () {
if (maxTimeMSMode === 'ui') {
// open settings
await browser.clickVisible(
Selectors.AggregationAdditionalOptionsButton
);
// set maxTimeMS
await browser.setValueVisible(
Selectors.AggregationMaxTimeMSInput,
'100'
);
}
if (maxTimeMSMode === 'preference') {
await browser.openSettingsModal();
const settingsModal = browser.$(Selectors.SettingsModal);
await settingsModal.waitForDisplayed();
await browser.clickVisible(Selectors.GeneralSettingsButton);
await browser.setValueVisible(
Selectors.SettingsInputElement('maxTimeMS'),
'1'
);
await browser.clickVisible(Selectors.SaveSettingsButton);
}
// run a projection that will take lots of time
await browser.selectStageOperator(0, '$match');
await browser.waitUntil(async function () {
const textElement = browser.$(
Selectors.stagePreviewToolbarTooltip(0)
);
const text = await textElement.getText();
return text === '(Sample of 0 documents)';
});
const syntaxMessageElement = browser.$(
Selectors.stageEditorSyntaxErrorMessage(0)
);
await syntaxMessageElement.waitForDisplayed();
// 100 x sleep(100) = 10s total execution time
// This works better than a $project with sleep(10000),
// where the DB may not interrupt the sleep() call if it
// has already started.
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
`{
$expr: {
$and: [${[...Array(100).keys()]
.map(
() =>
`{ $function: { body: 'function() { sleep(100) }', args: [], lang: 'js' } }`
)
.join(',')}]
}
}`
);
// make sure we got the timeout error
const messageElement = browser.$(Selectors.stageEditorErrorMessage(0));
await messageElement.waitForDisplayed();
// The exact error we get depends on the version of mongodb
/*
expect(await messageElement.getText()).to.include(
'operation exceeded time limit'
);
*/
});
}
});
it('supports $out as the last stage', async function () {
await browser.selectStageOperator(0, '$out');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
"'my-out-collection'"
);
await waitForAnyText(browser, browser.$(Selectors.stageContent(0)));
await addStage(browser, 2);
await browser.focusStageOperator(1);
await browser.selectStageOperator(1, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(1),
`{ i: 5 }`
);
await waitForAnyText(browser, browser.$(Selectors.stageContent(1)));
// delete the stage after $out
await deleteStage(browser, 1);
// run the $out stage
await browser.clickVisible(Selectors.RunPipelineButton);
// confirm the write operation
const writeOperationConfirmationModal = browser.$(
Selectors.AggregationWriteOperationConfirmationModal
);
await writeOperationConfirmationModal.waitForDisplayed();
const description = await browser
.$(Selectors.AggregationWriteOperationConfirmationModalDescription)
.getText();
expect(description).to.contain('creating');
expect(description).to.contain('test.my-out-collection');
await browser.clickVisible(
Selectors.AggregationWriteOperationConfirmButton
);
await writeOperationConfirmationModal.waitForDisplayed({ reverse: true });
// go to the new collection
const goToCollectionButton = browser.$(Selectors.GoToCollectionButton);
await goToCollectionButton.waitForDisplayed();
await browser.clickVisible(Selectors.GoToCollectionButton);
await browser.waitUntil(
async function () {
const ns = await browser.getActiveTabNamespace();
return ns === 'test.my-out-collection';
},
{
timeoutMsg:
'Expected `test.my-out-collection` namespace tab to be visible',
}
);
});
it('cancels pipeline with $out as the last stage', async function () {
await browser.selectStageOperator(0, '$out');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
"'numbers'"
);
await waitForAnyText(browser, browser.$(Selectors.stageContent(0)));
await addStage(browser, 2);
await browser.focusStageOperator(1);
await browser.selectStageOperator(1, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(1),
`{ i: 5 }`
);
await waitForAnyText(browser, browser.$(Selectors.stageContent(1)));
// delete the stage after $out
await deleteStage(browser, 1);
// run the $out stage
await browser.clickVisible(Selectors.RunPipelineButton);
// confirm the write operation
const writeOperationConfirmationModal = browser.$(
Selectors.AggregationWriteOperationConfirmationModal
);
await writeOperationConfirmationModal.waitForDisplayed();
const description = await browser
.$(Selectors.AggregationWriteOperationConfirmationModalDescription)
.getText();
expect(description).to.contain('overwriting');
expect(description).to.contain('test.numbers');
await browser.clickVisible(Selectors.AggregationWriteOperationCancelButton);
await writeOperationConfirmationModal.waitForDisplayed({ reverse: true });
// the pipeline can be futher edited
await waitForAnyText(browser, browser.$(Selectors.stageContent(0)));
});
it('supports $merge as the last stage', async function () {
if (serverSatisfies('< 4.2.0')) {
return this.skip();
}
await browser.selectStageOperator(0, '$merge');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
`{
into: 'my-merge-collection'
}`
);
await waitForAnyText(browser, browser.$(Selectors.stageContent(0)));
await browser.clickVisible(Selectors.AddStageButton);
await browser.focusStageOperator(1);
await browser.selectStageOperator(1, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(1),
`{ i: 5 }`
);
await waitForAnyText(browser, browser.$(Selectors.stageContent(1)));
// delete the stage after $out
await deleteStage(browser, 1);
// run the $merge stage
await browser.clickVisible(Selectors.RunPipelineButton);
// confirm the write operation
const writeOperationConfirmationModal = browser.$(
Selectors.AggregationWriteOperationConfirmationModal
);
await writeOperationConfirmationModal.waitForDisplayed();
const description = await browser
.$(Selectors.AggregationWriteOperationConfirmationModalDescription)
.getText();
expect(description).to.contain('altering');
expect(description).to.contain('test.my-merge-collection');
await browser.clickVisible(
Selectors.AggregationWriteOperationConfirmButton
);
await writeOperationConfirmationModal.waitForDisplayed({ reverse: true });
// go to the new collection
const goToCollectionButton = browser.$(Selectors.GoToCollectionButton);
await goToCollectionButton.waitForDisplayed();
await browser.clickVisible(Selectors.GoToCollectionButton);
await browser.waitUntil(
async function () {
const ns = await browser.getActiveTabNamespace();
return ns === 'test.my-merge-collection';
},
{
timeoutMsg:
'Expected `test.my-merge-collection` namespace tab to be visible',
}
);
});
it('cancels pipeline with $merge as the last stage', async function () {
if (serverSatisfies('< 4.2.0')) {
return this.skip();
}
await browser.selectStageOperator(0, '$merge');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
"'numbers'"
);
await waitForAnyText(browser, browser.$(Selectors.stageContent(0)));
await browser.clickVisible(Selectors.AddStageButton);
await browser.focusStageOperator(1);
await browser.selectStageOperator(1, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(1),
`{ i: 5 }`
);
await waitForAnyText(browser, browser.$(Selectors.stageContent(1)));
// delete the stage after $out
await deleteStage(browser, 1);
// run the $out stage
await browser.clickVisible(Selectors.RunPipelineButton);
// confirm the write operation
const writeOperationConfirmationModal = browser.$(
Selectors.AggregationWriteOperationConfirmationModal
);
await writeOperationConfirmationModal.waitForDisplayed();
const description = await browser
.$(Selectors.AggregationWriteOperationConfirmationModalDescription)
.getText();
expect(description).to.contain('altering');
expect(description).to.contain('test.numbers');
await browser.clickVisible(Selectors.AggregationWriteOperationCancelButton);
await writeOperationConfirmationModal.waitForDisplayed({ reverse: true });
// the pipeline can be futher edited
await waitForAnyText(browser, browser.$(Selectors.stageContent(0)));
});
it('supports running and editing aggregation', async function () {
// Set first stage to match
await browser.selectStageOperator(0, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'{ i: 5 }'
);
// Run and wait for results
await goToRunAggregation(browser);
// Get all documents from the current results page
const docs = await getDocuments(browser);
expect(docs).to.have.lengthOf(1);
expect(docs[0]).to.have.property('_id');
expect(docs[0]).to.have.property('i', 5);
expect(docs[0]).to.have.property('j', 0);
// Go back to the pipeline builder
await goToEditPipeline(browser);
// Change match filter
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'{ i: { $gte: 5, $lte: 10 } }'
);
// Run and wait for results
await goToRunAggregation(browser);
// Get all documents from the current results page
const updatedDocs = await getDocuments(browser);
// Check that the documents are matching pipeline
expect(updatedDocs).to.have.lengthOf(6);
expect(updatedDocs[0]).to.have.property('i', 5);
expect(updatedDocs[1]).to.have.property('i', 6);
expect(updatedDocs[2]).to.have.property('i', 7);
expect(updatedDocs[3]).to.have.property('i', 8);
expect(updatedDocs[4]).to.have.property('i', 9);
expect(updatedDocs[5]).to.have.property('i', 10);
});
it('supports paginating aggregation results', async function () {
// Set first stage to $match
await browser.selectStageOperator(0, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'{ i: { $gte: 5 } }'
);
// Add second $limit stage
await browser.clickVisible(Selectors.AddStageButton);
await browser.focusStageOperator(1);
await browser.selectStageOperator(1, '$limit');
await browser.setCodemirrorEditorValue(Selectors.stageEditor(1), '25');
// Run and wait for results
await goToRunAggregation(browser);
const page1 = await getDocuments(browser);
expect(page1).to.have.lengthOf(20);
expect(page1[0]).to.have.property('i', 5);
await browser.clickVisible(Selectors.AggregationRestultsNextPageButton);
await browser.waitUntil(async () => {
const paginationDescription = browser.$(
Selectors.AggregationRestultsPaginationDescription
);
return (await paginationDescription.getText()) === 'Showing 21 – 25';
});
const page2 = await getDocuments(browser);
expect(page2).to.have.lengthOf(5);
expect(page2[0]).to.have.property('i', 25);
});
it('supports cancelling long-running aggregations', async function () {
if (serverSatisfies('< 4.4.0')) {
// $function expression that we use to simulate slow aggregation is only
// supported since server 4.4
this.skip();
}
const slowQuery = `{
sleep: {
$function: {
body: function () {
return sleep(10000) || true;
},
args: [],
lang: "js",
},
},
}`;
// Set first stage to a very slow $addFields
await browser.selectStageOperator(0, '$addFields');
await browser.setCodemirrorEditorValue(Selectors.stageEditor(0), slowQuery);
// Run and wait for results
await goToRunAggregation(browser);
// Cancel aggregation run
await browser.clickVisible(Selectors.AggregationResultsCancelButton);
// Wait for the empty results banner (this is our indicator that we didn't
// load anything and dismissed "Loading" banner)
const emptyResultsBanner = browser.$(Selectors.AggregationEmptyResults);
await emptyResultsBanner.waitForDisplayed();
});
it('handles errors in aggregations', async function () {
// Disable autopreview so we can run an aggregation that will cause an error
await browser.clickVisible(Selectors.AggregationAutoPreviewToggle);
// Set first stage to an invalid $project stage to trigger server error
await browser.selectStageOperator(0, '$project');
await browser.setCodemirrorEditorValue(Selectors.stageEditor(0), '{}');
// Run and wait for results
await goToRunAggregation(browser);
const errorBanner = browser.$(Selectors.AggregationErrorBanner);
await errorBanner.waitForDisplayed();
const errorText = await errorBanner.getText();
expect(errorText).to.match(
/(\$project )?specification must have at least one field/
);
});
it('supports exporting aggregation results', async function () {
skipForWeb(this, 'export is not yet available in compass-web');
// Set first stage to $match.
await browser.selectStageOperator(0, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'{ i: 5 }'
);
// Open the modal.
await browser.clickVisible(Selectors.ExportAggregationResultsButton);
const exportModal = browser.$(Selectors.ExportModal);
await exportModal.waitForDisplayed();
// Make sure the aggregation is shown in the modal.
const exportModalAggregationTextElement = browser.$(
Selectors.ExportModalCodePreview
);
expect(await exportModalAggregationTextElement.getText()).to
.equal(`db.getCollection('numbers').aggregate(
[{ $match: { i: 5 } }],
{ maxTimeMS: 60000, allowDiskUse: true }
);`);
await browser.clickVisible(Selectors.ExportModalExportButton);
// Set the filename.
const filename = outputFilename('aggregated-numbers.json');
await browser.setExportFilename(filename);
// Wait for the modal to go away.
const exportModalElement = browser.$(Selectors.ExportModal);
await exportModalElement.waitForDisplayed({
reverse: true,
});
await browser.waitForExportToFinishAndCloseToast();
// Confirm that we exported what we expected to export
const text = await fs.readFile(filename, 'utf-8');
const docs = JSON.parse(text);
expect(docs).to.have.lengthOf(1);
expect(docs[0]).to.have.property('_id');
expect(docs[0]).to.have.property('i', 5);
expect(docs[0]).to.have.property('j', 0);
});
it('shows the explain for a pipeline', async function () {
// Set first stage to $match
await browser.selectStageOperator(0, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'{ i: 5 }'
);
await browser.clickVisible(Selectors.AggregationExplainButton);
await browser.waitForAnimations(Selectors.AggregationExplainModal);
const modal = browser.$(Selectors.AggregationExplainModal);
await modal.waitForDisplayed();
await browser.waitForAnimations(Selectors.AggregationExplainModal);
expect(await modal.getText()).to.contain('Query Performance Summary');
await browser.clickVisible(Selectors.AggregationExplainModalCloseButton);
await modal.waitForDisplayed({ reverse: true });
});
it('shows confirmation modal when create new pipeline is clicked and aggregation is modified', async function () {
await browser.selectStageOperator(0, '$match');
await browser.clickConfirmationAction(Selectors.CreateNewPipelineButton);
});
describe('aggregation builder in text mode', function () {
it('toggles pipeline mode', async function () {
// Select operator
await browser.selectStageOperator(0, '$match');
await browser.setCodemirrorEditorValue(
Selectors.stageEditor(0),
'{ i: 5 }'