@@ -258,6 +258,62 @@ public void Aggregate_should_execute_an_AggregateToCollectionOperation_and_a_Fin
258258 findOperation. Sort . Should ( ) . BeNull ( ) ;
259259 }
260260
261+ [ Theory ]
262+ [ InlineData ( "{ $merge : \" outputcollection\" }" , null , "outputcollection" , false ) ]
263+ [ InlineData ( "{ $merge : \" outputcollection\" }" , null , "outputcollection" , true ) ]
264+ [ InlineData ( "{ $merge : { into : \" outputcollection\" } }" , null , "outputcollection" , false ) ]
265+ [ InlineData ( "{ $merge : { into : \" outputcollection\" } }" , null , "outputcollection" , true ) ]
266+ [ InlineData ( "{ $merge : { into : { coll : \" outputcollection\" } } }" , null , "outputcollection" , false ) ]
267+ [ InlineData ( "{ $merge : { into : { coll : \" outputcollection\" } } }" , null , "outputcollection" , true ) ]
268+ [ InlineData ( "{ $merge : { into : { db: \" outputdatabase\" , coll : \" outputcollection\" } } }" , "outputdatabase" , "outputcollection" , false ) ]
269+ [ InlineData ( "{ $merge : { into : { db: \" outputdatabase\" , coll : \" outputcollection\" } } }" , "outputdatabase" , "outputcollection" , true ) ]
270+ public void Aggregate_should_recognize_merge_collection_argument(
271+ string stageDefinitionString ,
272+ string expectedDatabaseName ,
273+ string expectedCollectionName ,
274+ bool async )
275+ {
276+ var subject = CreateSubject< BsonDocument > ( ) ;
277+ var stageDefinition = BsonDocument. Parse( stageDefinitionString ) ;
278+ var expectedCollectionNamespace = new CollectionNamespace (
279+ expectedDatabaseName ?? subject . CollectionNamespace . DatabaseNamespace . DatabaseName ,
280+ expectedCollectionName ) ;
281+
282+ var pipeline = new EmptyPipelineDefinition < BsonDocument > ( )
283+ . AppendStage < BsonDocument , BsonDocument , BsonDocument > ( stageDefinition ) ;
284+ var expectedPipeline = new List < BsonDocument > ( RenderPipeline ( subject , pipeline ) . Documents ) ;
285+
286+ IAsyncCursor < BsonDocument > result ;
287+ if ( async )
288+ {
289+ result = subject . AggregateAsync ( pipeline ) . GetAwaiter ( ) . GetResult ( ) ;
290+ }
291+ else
292+ {
293+ result = subject . Aggregate ( pipeline ) ;
294+ }
295+ var aggregateCall = _operationExecutor . GetWriteCall < BsonDocument > ( ) ;
296+
297+ var aggregateOperation = aggregateCall . Operation . Should ( ) . BeOfType < AggregateToCollectionOperation > ( ) . Subject ;
298+ aggregateOperation. CollectionNamespace . Should ( ) . Be ( subject . CollectionNamespace ) ;
299+ aggregateOperation. Pipeline . Should ( ) . Equal ( expectedPipeline ) ;
300+
301+ var mockCursor = new Mock < IAsyncCursor < BsonDocument > > ( ) ;
302+ _operationExecutor. EnqueueResult ( mockCursor . Object ) ;
303+ if ( async)
304+ {
305+ result . MoveNextAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
306+ }
307+ else
308+ {
309+ result . MoveNext ( ) ;
310+ }
311+ var findCall = _operationExecutor . GetReadCall < IAsyncCursor < BsonDocument > > ( ) ;
312+
313+ var findOperation = findCall . Operation . Should ( ) . BeOfType < FindOperation < BsonDocument > > ( ) . Subject ;
314+ findOperation. CollectionNamespace . Should ( ) . Be ( expectedCollectionNamespace ) ;
315+ }
316+
261317 [ Theory ]
262318 [ ParameterAttributeData ]
263319 public void AggregateToCollection_should_execute_an_AggregateToCollectionOperation(
0 commit comments