Skip to content

Commit ed138b0

Browse files
CSHARP-2468: Resync change stream tests to test all new notification types.
1 parent 26c4a2b commit ed138b0

File tree

8 files changed

+532
-10
lines changed

8 files changed

+532
-10
lines changed

tests/MongoDB.Driver.Tests/Specifications/change-streams/ChangeStreamTestRunner.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private void AssertSuccess(List<ChangeStreamDocument<BsonDocument>> actualResult
354354

355355
private void AssertChangeStreamDocument(ChangeStreamDocument<BsonDocument> actualDocument, BsonDocument expectedDocument)
356356
{
357-
JsonDrivenHelper.EnsureAllFieldsAreValid(expectedDocument, "_id", "documentKey", "operationType", "ns", "fullDocument");
357+
JsonDrivenHelper.EnsureAllFieldsAreValid(expectedDocument, "_id", "documentKey", "operationType", "ns", "fullDocument", "updateDescription", "to");
358358

359359
AssertChangeStreamDocumentPropertyValuesAgainstBackingDocument(actualDocument);
360360

@@ -391,6 +391,29 @@ private void AssertChangeStreamDocument(ChangeStreamDocument<BsonDocument> actua
391391
var expectedFullDocument = expectedDocument["fullDocument"].AsBsonDocument;
392392
actualFullDocument.Should().Be(expectedFullDocument);
393393
}
394+
395+
if (expectedDocument.Contains("updateDescription"))
396+
{
397+
var expectedUpdateDescription = expectedDocument["updateDescription"].AsBsonDocument;
398+
JsonDrivenHelper.EnsureAllFieldsAreValid(expectedUpdateDescription, "updatedFields", "removedFields");
399+
var actualUpdateDescription = actualDocument.UpdateDescription;
400+
actualUpdateDescription.UpdatedFields.Should().Be(expectedUpdateDescription["updatedFields"].AsBsonDocument);
401+
if (expectedUpdateDescription.Contains("removedFields"))
402+
{
403+
var actualRemovedFields = new BsonArray(actualUpdateDescription.RemovedFields);
404+
actualRemovedFields.Should().Be(expectedUpdateDescription["removedFields"].AsBsonArray);
405+
}
406+
}
407+
408+
if (expectedDocument.Contains("to"))
409+
{
410+
var to = expectedDocument["to"].AsBsonDocument;
411+
JsonDrivenHelper.EnsureAllFieldsAreValid(to, "db", "coll");
412+
var expectedRenameToDatabaseName = to["db"].AsString;
413+
var expectedRenameToCollectionName = to["coll"].AsString;
414+
var expectedRenameToCollectionNamespace = new CollectionNamespace(new DatabaseNamespace(expectedRenameToDatabaseName), expectedRenameToCollectionName);
415+
actualDocument.RenameTo.Should().Be(expectedRenameToCollectionNamespace);
416+
}
394417
}
395418

396419
private void AssertChangeStreamDocumentPropertyValuesAgainstBackingDocument(ChangeStreamDocument<BsonDocument> actualDocument)
@@ -408,12 +431,27 @@ private void AssertChangeStreamDocumentPropertyValuesAgainstBackingDocument(Chan
408431
clusterTime.Should().BeNull();
409432
}
410433

434+
var operationType = actualDocument.OperationType;
435+
operationType.ToString().ToLowerInvariant().Should().Be(backingDocument["operationType"].AsString);
436+
437+
if (operationType == ChangeStreamOperationType.Invalidate)
438+
{
439+
return;
440+
}
441+
411442
var collectionNamespace = actualDocument.CollectionNamespace;
412443
collectionNamespace.DatabaseNamespace.DatabaseName.Should().Be(backingDocument["ns"]["db"].AsString);
413444
collectionNamespace.CollectionName.Should().Be(backingDocument["ns"]["coll"].AsString);
414445

415446
var documentKey = actualDocument.DocumentKey;
416-
documentKey.Should().Be(backingDocument["documentKey"].AsBsonDocument);
447+
if (operationType == ChangeStreamOperationType.Rename || operationType == ChangeStreamOperationType.Drop)
448+
{
449+
documentKey.Should().BeNull();
450+
}
451+
else
452+
{
453+
documentKey.Should().Be(backingDocument["documentKey"].AsBsonDocument);
454+
}
417455

418456
var fullDocument = actualDocument.FullDocument;
419457
if (backingDocument.Contains("fullDocument"))
@@ -425,16 +463,15 @@ private void AssertChangeStreamDocumentPropertyValuesAgainstBackingDocument(Chan
425463
fullDocument.Should().BeNull();
426464
}
427465

428-
var operationType = actualDocument.OperationType;
429-
operationType.ToString().ToLowerInvariant().Should().Be(backingDocument["operationType"].AsString);
430-
431466
var resumeToken = actualDocument.ResumeToken;
432467
resumeToken.Should().Be(backingDocument["_id"].AsBsonDocument);
433468

434469
var updateDescription = actualDocument.UpdateDescription;
435470
if (backingDocument.Contains("updateDescription"))
436471
{
437-
updateDescription.Should().Be(backingDocument["updateDescription"].AsBsonDocument);
472+
var removedFields = new BsonArray(updateDescription.RemovedFields);
473+
removedFields.Should().Be(backingDocument["updateDescription"]["removedFields"].AsBsonArray);
474+
updateDescription.UpdatedFields.Should().Be(backingDocument["updateDescription"]["updatedFields"].AsBsonDocument);
438475
}
439476
else
440477
{

tests/MongoDB.Driver.Tests/Specifications/change-streams/tests/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Each YAML file has the following keys:
4848
- ``database``: Database against which to run the operation
4949
- ``collection``: Collection against which to run the operation
5050
- ``name``: Name of the command to run
51-
- ``arguments``: Object of arguments for the command (ex: document to insert)
51+
- ``arguments`` (optional): Object of arguments for the command (ex: document to insert)
5252

5353
- ``expectations``: Optional list of command-started events in Extended JSON format
5454
- ``result``: Document with ONE of the following fields:

tests/MongoDB.Driver.Tests/Specifications/change-streams/tests/change-streams.json

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,241 @@
440440
}
441441
]
442442
}
443+
},
444+
{
445+
"description": "Test insert, update, replace, and delete event types",
446+
"minServerVersion": "3.6.0",
447+
"target": "collection",
448+
"topology": [
449+
"replicaset"
450+
],
451+
"changeStreamPipeline": [],
452+
"changeStreamOptions": {},
453+
"operations": [
454+
{
455+
"database": "change-stream-tests",
456+
"collection": "test",
457+
"name": "insertOne",
458+
"arguments": {
459+
"document": {
460+
"x": 1
461+
}
462+
}
463+
},
464+
{
465+
"database": "change-stream-tests",
466+
"collection": "test",
467+
"name": "updateOne",
468+
"arguments": {
469+
"filter": {
470+
"x": 1
471+
},
472+
"update": {
473+
"$set": {
474+
"x": 2
475+
}
476+
}
477+
}
478+
},
479+
{
480+
"database": "change-stream-tests",
481+
"collection": "test",
482+
"name": "replaceOne",
483+
"arguments": {
484+
"filter": {
485+
"x": 2
486+
},
487+
"replacement": {
488+
"x": 3
489+
}
490+
}
491+
},
492+
{
493+
"database": "change-stream-tests",
494+
"collection": "test",
495+
"name": "deleteOne",
496+
"arguments": {
497+
"filter": {
498+
"x": 3
499+
}
500+
}
501+
}
502+
],
503+
"expectations": [
504+
{
505+
"command_started_event": {
506+
"command": {
507+
"aggregate": "test",
508+
"cursor": {},
509+
"pipeline": [
510+
{
511+
"$changeStream": {
512+
"fullDocument": "default"
513+
}
514+
}
515+
]
516+
},
517+
"command_name": "aggregate",
518+
"database_name": "change-stream-tests"
519+
}
520+
}
521+
],
522+
"result": {
523+
"success": [
524+
{
525+
"operationType": "insert",
526+
"ns": {
527+
"db": "change-stream-tests",
528+
"coll": "test"
529+
},
530+
"fullDocument": {
531+
"x": {
532+
"$numberInt": "1"
533+
}
534+
}
535+
},
536+
{
537+
"operationType": "update",
538+
"ns": {
539+
"db": "change-stream-tests",
540+
"coll": "test"
541+
},
542+
"updateDescription": {
543+
"updatedFields": {
544+
"x": {
545+
"$numberInt": "2"
546+
}
547+
}
548+
}
549+
},
550+
{
551+
"operationType": "replace",
552+
"ns": {
553+
"db": "change-stream-tests",
554+
"coll": "test"
555+
},
556+
"fullDocument": {
557+
"x": {
558+
"$numberInt": "3"
559+
}
560+
}
561+
},
562+
{
563+
"operationType": "delete",
564+
"ns": {
565+
"db": "change-stream-tests",
566+
"coll": "test"
567+
}
568+
}
569+
]
570+
}
571+
},
572+
{
573+
"description": "Test rename and invalidate event types",
574+
"minServerVersion": "4.0.1",
575+
"target": "collection",
576+
"topology": [
577+
"replicaset"
578+
],
579+
"changeStreamPipeline": [],
580+
"changeStreamOptions": {},
581+
"operations": [
582+
{
583+
"database": "change-stream-tests",
584+
"collection": "test",
585+
"name": "rename",
586+
"arguments": {
587+
"to": "test2"
588+
}
589+
}
590+
],
591+
"expectations": [
592+
{
593+
"command_started_event": {
594+
"command": {
595+
"aggregate": "test",
596+
"cursor": {},
597+
"pipeline": [
598+
{
599+
"$changeStream": {
600+
"fullDocument": "default"
601+
}
602+
}
603+
]
604+
},
605+
"command_name": "aggregate",
606+
"database_name": "change-stream-tests"
607+
}
608+
}
609+
],
610+
"result": {
611+
"success": [
612+
{
613+
"operationType": "rename",
614+
"ns": {
615+
"db": "change-stream-tests",
616+
"coll": "test"
617+
},
618+
"to": {
619+
"db": "change-stream-tests",
620+
"coll": "test2"
621+
}
622+
},
623+
{
624+
"operationType": "invalidate"
625+
}
626+
]
627+
}
628+
},
629+
{
630+
"description": "Test drop and invalidate event types",
631+
"minServerVersion": "4.0.1",
632+
"target": "collection",
633+
"topology": [
634+
"replicaset"
635+
],
636+
"changeStreamPipeline": [],
637+
"changeStreamOptions": {},
638+
"operations": [
639+
{
640+
"database": "change-stream-tests",
641+
"collection": "test",
642+
"name": "drop"
643+
}
644+
],
645+
"expectations": [
646+
{
647+
"command_started_event": {
648+
"command": {
649+
"aggregate": "test",
650+
"cursor": {},
651+
"pipeline": [
652+
{
653+
"$changeStream": {
654+
"fullDocument": "default"
655+
}
656+
}
657+
]
658+
},
659+
"command_name": "aggregate",
660+
"database_name": "change-stream-tests"
661+
}
662+
}
663+
],
664+
"result": {
665+
"success": [
666+
{
667+
"operationType": "drop",
668+
"ns": {
669+
"db": "change-stream-tests",
670+
"coll": "test"
671+
}
672+
},
673+
{
674+
"operationType": "invalidate"
675+
}
676+
]
677+
}
443678
}
444679
]
445680
}

0 commit comments

Comments
 (0)