@@ -5,6 +5,7 @@ import type Mongo from './mongo';
55import Database from './database' ;
66import { Streams } from './streams' ;
77import { InterruptFlag , MongoshInterruptedError } from './interruptor' ;
8+ import type { MongoshInvalidInputError } from '@mongosh/errors' ;
89
910describe ( 'Streams' , function ( ) {
1011 let mongo : Mongo ;
@@ -162,4 +163,112 @@ describe('Streams', function () {
162163 ) . to . be . true ;
163164 } ) ;
164165 } ) ;
166+
167+ describe ( 'modify' , function ( ) {
168+ it ( 'throws with invalid parameters' , async function ( ) {
169+ // Create the stream processor.
170+ const runCmdStub = sinon
171+ . stub ( mongo . _serviceProvider , 'runCommand' )
172+ . resolves ( { ok : 1 } ) ;
173+ const name = 'p1' ;
174+ const pipeline = [ { $match : { foo : 'bar' } } ] ;
175+ const processor = await streams . createStreamProcessor ( name , pipeline ) ;
176+ expect ( processor ) . to . eql ( streams . getProcessor ( name ) ) ;
177+ const cmd = { createStreamProcessor : name , pipeline } ;
178+ expect ( runCmdStub . calledOnceWithExactly ( 'admin' , cmd , { } ) ) . to . be . true ;
179+
180+ // No arguments to modify.
181+ const caught = await processor
182+ . modify ( )
183+ . catch ( ( e : MongoshInvalidInputError ) => e ) ;
184+ expect ( caught . message ) . to . contain (
185+ '[COMMON-10001] The first argument to modify must be an array or object.'
186+ ) ;
187+
188+ // A single numeric argument to modify.
189+ const caught2 = await processor
190+ . modify ( 1 )
191+ . catch ( ( e : MongoshInvalidInputError ) => e ) ;
192+ expect ( caught2 . message ) . to . contain (
193+ '[COMMON-10001] The first argument to modify must be an array or object.'
194+ ) ;
195+
196+ // Two object arguments to modify.
197+ const caught3 = await processor
198+ . modify (
199+ { resumeFromCheckpoint : false } ,
200+ { dlq : { connectionName : 'foo' } }
201+ )
202+ . catch ( ( e : MongoshInvalidInputError ) => e ) ;
203+ expect ( caught3 . message ) . to . contain (
204+ '[COMMON-10001] If the first argument to modify is an object, the second argument should not be specified.'
205+ ) ;
206+ } ) ;
207+
208+ it ( 'works with pipeline and options arguments' , async function ( ) {
209+ const runCmdStub = sinon
210+ . stub ( mongo . _serviceProvider , 'runCommand' )
211+ . resolves ( { ok : 1 } ) ;
212+
213+ // Create the stream processor.
214+ const name = 'p1' ;
215+ const pipeline = [ { $match : { foo : 'bar' } } ] ;
216+ const processor = await streams . createStreamProcessor ( name , pipeline ) ;
217+ expect ( processor ) . to . eql ( streams . getProcessor ( name ) ) ;
218+ const cmd = { createStreamProcessor : name , pipeline } ;
219+ expect ( runCmdStub . calledOnceWithExactly ( 'admin' , cmd , { } ) ) . to . be . true ;
220+
221+ // Start the stream processor.
222+ await processor . start ( ) ;
223+ expect (
224+ runCmdStub . calledWithExactly (
225+ 'admin' ,
226+ { startStreamProcessor : name } ,
227+ { }
228+ )
229+ ) . to . be . true ;
230+
231+ // Stop the stream processor.
232+ await processor . stop ( ) ;
233+ expect (
234+ runCmdStub . calledWithExactly ( 'admin' , { stopStreamProcessor : name } , { } )
235+ ) . to . be . true ;
236+
237+ // Modify the stream processor.
238+ const pipeline2 = [ { $match : { foo : 'baz' } } ] ;
239+ processor . modify ( pipeline2 ) ;
240+ expect (
241+ runCmdStub . calledWithExactly (
242+ 'admin' ,
243+ { modifyStreamProcessor : name , pipeline : pipeline2 } ,
244+ { }
245+ )
246+ ) . to . be . true ;
247+
248+ // Modify the stream processor with extra options.
249+ const pipeline3 = [ { $match : { foo : 'bat' } } ] ;
250+ processor . modify ( pipeline3 , { resumeFromCheckpoint : false } ) ;
251+ expect (
252+ runCmdStub . calledWithExactly (
253+ 'admin' ,
254+ {
255+ modifyStreamProcessor : name ,
256+ pipeline : pipeline3 ,
257+ resumeFromCheckpoint : false ,
258+ } ,
259+ { }
260+ )
261+ ) . to . be . true ;
262+
263+ // Modify the stream processor without changing pipeline.
264+ processor . modify ( { resumeFromCheckpoint : false } ) ;
265+ expect (
266+ runCmdStub . calledWithExactly (
267+ 'admin' ,
268+ { modifyStreamProcessor : name , resumeFromCheckpoint : false } ,
269+ { }
270+ )
271+ ) . to . be . true ;
272+ } ) ;
273+ } ) ;
165274} ) ;
0 commit comments