collection_name: &collection_name "test"
database_name: &database_name "change-stream-tests"
collection2_name: &collection2_name "test2"
database2_name: &database2_name "change-stream-tests-2"
tests:
  -
    description: "$changeStream must be the first stage in a change stream pipeline sent to the server"
    minServerVersion: "3.6.0"
    target: collection
    topology: 
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            x: 1
    expectations:
      -
        command_started_event:
          command:
            aggregate: *collection_name
            cursor: {}
            pipeline:
              -
                $changeStream: {}
          command_name: aggregate
          database_name: *database_name
    result:
      success:
        -
          _id: "42"
          documentKey: "42"
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            x:
              $numberInt: "1"
  -
    description: The server returns change stream responses in the specified server response format
    minServerVersion: "3.6.0"
    target: collection
    topology:
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            x: 1
    expectations: []
    result:
      success:
        -
          _id: "42"
          documentKey: "42"
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            x:
              $numberInt: "1"
  -
    description: Executing a watch helper on a Collection results in notifications for changes to the specified collection
    minServerVersion: "3.6.0"
    target: collection
    topology:
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection2_name
        name: insertOne
        arguments:
          document:
            x: 1
      -
        database: *database2_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            y: 2
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            z: 3
    expectations:
      - 
        command_started_event:
          command:
            aggregate: *collection_name
            cursor: {}
            pipeline:
              - 
                $changeStream: {}
          command_name: aggregate
          database_name: *database_name
    result:
      success:
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            z:
              $numberInt: "3"
  -
    description: Change Stream should allow valid aggregate pipeline stages
    minServerVersion: "3.6.0"
    target: collection
    topology:
      - replicaset
    changeStreamPipeline:
      -
        $match:
          "fullDocument.z": 3
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            y: 2
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            z: 3
    expectations:
      -
        command_started_event:
          command:
            aggregate: *collection_name
            cursor: {}
            pipeline:
              - 
                $changeStream: {}
              -
                $match:
                  "fullDocument.z":
                    $numberInt: "3"
          command_name: aggregate
          database_name: *database_name
    result:
      success:
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            z:
              $numberInt: "3"
  -
    description: Executing a watch helper on a Database results in notifications for changes to all collections in the specified database.
    minServerVersion: "3.8.0"
    target: database
    topology:
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection2_name
        name: insertOne
        arguments:
          document:
            x: 1
      -
        database: *database2_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            y: 2
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            z: 3
    expectations:
      -
        command_started_event:
          command:
            aggregate:
              $numberInt: "1"
            cursor: {}
            pipeline:
              - 
                $changeStream: {}
          command_name: aggregate
          database_name: *database_name
    result:
      success:
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection2_name
          fullDocument:
            x:
              $numberInt: "1"
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            z:
              $numberInt: "3"
  -
    description: Executing a watch helper on a MongoClient results in notifications for changes to all collections in all databases in the cluster.
    minServerVersion: "3.8.0"
    target: client
    topology:
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection2_name
        name: insertOne
        arguments:
          document:
            x: 1
      -
        database: *database2_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            y: 2
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            z: 3
    expectations:
      -
        command_started_event:
          command:
            aggregate:
              $numberInt: "1"
            cursor: {}
            pipeline:
              - 
                $changeStream:
                  allChangesForCluster: true
          command_name: aggregate
          database_name: admin
    result:
      success:
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection2_name
          fullDocument:
            x:
              $numberInt: "1"
        -
          operationType: insert
          ns:
            db: *database2_name
            coll: *collection_name
          fullDocument:
            y:
              $numberInt: "2"
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            z:
              $numberInt: "3"
  -
    description: Test insert, update, replace, and delete event types
    minServerVersion: "3.6.0"
    target: collection
    topology:
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            x: 1
      -
        database: *database_name
        collection: *collection_name
        name: updateOne
        arguments:
          filter:
            x: 1
          update:
            $set:
              x: 2
      -
        database: *database_name
        collection: *collection_name
        name: replaceOne
        arguments:
          filter:
            x: 2
          replacement:
            x: 3
      -
        database: *database_name
        collection: *collection_name
        name: deleteOne
        arguments:
          filter:
            x: 3
    expectations:
      -
        command_started_event:
          command:
            aggregate: *collection_name
            cursor: {}
            pipeline:
              -
                $changeStream: {}
          command_name: aggregate
          database_name: *database_name
    result:
      success:
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            x:
              $numberInt: "1"
        -
          operationType: update
          ns:
            db: *database_name
            coll: *collection_name
          updateDescription:
            updatedFields:
              x:
                $numberInt: "2"
        -
          operationType: replace
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            x:
              $numberInt: "3"
        -
          operationType: delete
          ns:
            db: *database_name
            coll: *collection_name
  -
    description: Test rename and invalidate event types
    minServerVersion: "4.0.1"
    target: collection
    topology:
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection_name
        name: rename
        arguments:
          to: *collection2_name
    expectations:
      -
        command_started_event:
          command:
            aggregate: *collection_name
            cursor: {}
            pipeline:
              -
                $changeStream: {}
          command_name: aggregate
          database_name: *database_name
    result:
      success:
        -
          operationType: rename
          ns:
            db: *database_name
            coll: *collection_name
          to:
            db: *database_name
            coll: *collection2_name
        -
          operationType: invalidate
  -
    description: Test drop and invalidate event types
    minServerVersion: "4.0.1"
    target: collection
    topology:
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: {}
    operations:
      -
        database: *database_name
        collection: *collection_name
        name: drop
    expectations:
      -
        command_started_event:
          command:
            aggregate: *collection_name
            cursor: {}
            pipeline:
              -
                $changeStream: {}
          command_name: aggregate
          database_name: *database_name
    result:
      success:
        -
          operationType: drop
          ns:
            db: *database_name
            coll: *collection_name
        -
          operationType: invalidate
  # Test that resume logic works correctly even after consecutive retryable failures of a getMore command,
  # with no intervening events.  This is ensured by setting the batch size of the change stream to 1,
  -
    description: Test consecutive resume
    minServerVersion: "4.1.7"
    target: collection
    topology:
      - replicaset
    changeStreamPipeline: []
    changeStreamOptions: { batchSize: 1}
    failPoint:
      configureFailPoint: failCommand
      mode: {times: 2}
      data:
          failCommands: ["getMore"]
          closeConnection: true
    operations:
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            x: 1
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            x: 2
      -
        database: *database_name
        collection: *collection_name
        name: insertOne
        arguments:
          document:
            x: 3
    expectations:
      -
        command_started_event:
          command:
            aggregate: *collection_name
            cursor: {batchSize: 1}
            pipeline:
              -
                $changeStream: {}
          command_name: aggregate
          database_name: *database_name
    result:
      success:
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            x:
              $numberInt: "1"
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            x:
              $numberInt: "2"
        -
          operationType: insert
          ns:
            db: *database_name
            coll: *collection_name
          fullDocument:
            x:
              $numberInt: "3"
