|
14 | 14 | */ |
15 | 15 |
|
16 | 16 | using System; |
| 17 | +using System.Collections.Generic; |
17 | 18 | using System.Linq; |
18 | 19 | using FluentAssertions; |
19 | 20 | using MongoDB.Bson; |
@@ -485,15 +486,55 @@ public void CreateCommand_should_return_expected_result_when_WriteConcern_is_set |
485 | 486 | [SkippableTheory] |
486 | 487 | [ParameterAttributeData] |
487 | 488 | public void Execute_should_return_expected_result( |
488 | | - [Values(false, true)] |
489 | | - bool async) |
| 489 | + [Values("$out", "$merge")] string lastStageName, |
| 490 | + [Values(false, true)] bool usingDifferentOutputDatabase, |
| 491 | + [Values(false, true)] bool async) |
490 | 492 | { |
491 | 493 | RequireServer.Check(); |
| 494 | + var pipeline = new List<BsonDocument> { BsonDocument.Parse("{ $match : { _id : 1 } }") }; |
| 495 | + var inputDatabaseName = _databaseNamespace.DatabaseName; |
| 496 | + var inputCollectionName = _collectionNamespace.CollectionName; |
| 497 | + var outputDatabaseName = usingDifferentOutputDatabase ? $"{inputDatabaseName}-outputdatabase" : inputDatabaseName; |
| 498 | + var outputCollectionName = $"{inputCollectionName}-outputcollection"; |
| 499 | + switch (lastStageName) |
| 500 | + { |
| 501 | + case "$out": |
| 502 | + RequireServer.Check().Supports(Feature.AggregateOut); |
| 503 | + if (usingDifferentOutputDatabase) |
| 504 | + { |
| 505 | + RequireServer.Check().Supports(Feature.AggregateOutToDifferentDatabase); |
| 506 | + pipeline.Add(BsonDocument.Parse($"{{ $out : {{ db : '{outputDatabaseName}', coll : '{outputCollectionName}' }} }}")); |
| 507 | + } |
| 508 | + else |
| 509 | + { |
| 510 | + pipeline.Add(BsonDocument.Parse($"{{ $out : '{outputCollectionName}' }}")); |
| 511 | + } |
| 512 | + break; |
| 513 | + |
| 514 | + case "$merge": |
| 515 | + RequireServer.Check().Supports(Feature.AggregateMerge); |
| 516 | + if (usingDifferentOutputDatabase) |
| 517 | + { |
| 518 | + pipeline.Add(BsonDocument.Parse($"{{ $merge : {{ into : {{ db : '{outputDatabaseName}', coll : '{outputCollectionName}' }} }} }}")); |
| 519 | + } |
| 520 | + else |
| 521 | + { |
| 522 | + pipeline.Add(BsonDocument.Parse($"{{ $merge : {{ into : '{outputDatabaseName}' }} }}")); |
| 523 | + } |
| 524 | + break; |
| 525 | + |
| 526 | + default: |
| 527 | + throw new Exception($"Unexpected lastStageName: \"{lastStageName}\"."); |
| 528 | + } |
492 | 529 | EnsureTestData(); |
493 | | - var subject = new AggregateToCollectionOperation(_collectionNamespace, __pipeline, _messageEncoderSettings); |
| 530 | + if (usingDifferentOutputDatabase) |
| 531 | + { |
| 532 | + EnsureDatabaseExists(outputDatabaseName); |
| 533 | + } |
| 534 | + var subject = new AggregateToCollectionOperation(_collectionNamespace, pipeline, _messageEncoderSettings); |
494 | 535 |
|
495 | 536 | ExecuteOperation(subject, async); |
496 | | - var result = ReadAllFromCollection(new CollectionNamespace(_databaseNamespace, "awesome"), async); |
| 537 | + var result = ReadAllFromCollection(new CollectionNamespace(new DatabaseNamespace(outputDatabaseName), outputCollectionName), async); |
497 | 538 |
|
498 | 539 | result.Should().NotBeNull(); |
499 | 540 | result.Should().HaveCount(1); |
|
0 commit comments