# This test doesn't check contents of command-started events.
runOn:
    -
        minServerVersion: "4.0"
        topology: ["replicaset"]
    -
        minServerVersion: "4.1.8"
        topology: ["sharded"]

database_name: &database_name "transaction-tests"
collection_name: &collection_name "test"

data: []

tests:
  - description: default readPreference

    operations:
      - name: startTransaction
        object: session0
      - name: insertMany
        object: collection
        arguments:
          documents: &insertedDocs
            - _id: 1
            - _id: 2
            - _id: 3
            - _id: 4
          session: session0
        result:
          insertedIds: {0: 1, 1: 2, 2: 3, 3: 4}
      - name: aggregate
        object: collection
        collectionOptions:
          # The driver overrides the collection's read pref with the
          # transaction's so count runs with Primary and succeeds.
          readPreference:
            mode: Secondary
        arguments:
          session: session0
          pipeline:
            - $match:
                _id: 1
            - $count: count
        result:
          - count: 1
      - name: find
        object: collection
        collectionOptions:
          readPreference:
            mode: Secondary
        arguments:
          session: session0
          batchSize: 3
        result: *insertedDocs
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Secondary
        arguments:
          pipeline:
            - $project:
                _id: 1
          batchSize: 3
          session: session0
        result: *insertedDocs
      - name: commitTransaction
        object: session0

    outcome:
      collection:
        data: *insertedDocs

  - description: primary readPreference

    operations:
      - name: startTransaction
        object: session0
        arguments:
          options:
            readPreference:
              mode: Primary
      - name: insertMany
        object: collection
        arguments:
          documents: &insertedDocs
            - _id: 1
            - _id: 2
            - _id: 3
            - _id: 4
          session: session0
        result:
          insertedIds: {0: 1, 1: 2, 2: 3, 3: 4}
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Secondary
        arguments:
          session: session0
          pipeline:
            - $match:
                _id: 1
            - $count: count
        result:
          - count: 1
      - name: find
        object: collection
        collectionOptions:
          readPreference:
            mode: Secondary
        arguments:
          session: session0
          batchSize: 3
        result: *insertedDocs
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Secondary
        arguments:
          pipeline:
            - $project:
                _id: 1
          batchSize: 3
          session: session0
        result: *insertedDocs
      - name: commitTransaction
        object: session0

    outcome:
      collection:
        data: *insertedDocs

  - description: secondary readPreference

    operations:
      - name: startTransaction
        object: session0
        arguments:
          options:
            readPreference:
              mode: Secondary
      - name: insertMany
        object: collection
        arguments:
          documents: &insertedDocs
            - _id: 1
            - _id: 2
            - _id: 3
            - _id: 4
          session: session0
        result:
          insertedIds: {0: 1, 1: 2, 2: 3, 3: 4}
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          session: session0
          pipeline:
            - $match:
                _id: 1
            - $count: count
        result:
          errorContains: read preference in a transaction must be primary
      - name: find
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          session: session0
          batchSize: 3
        result:
          errorContains: read preference in a transaction must be primary
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          pipeline:
            - $project:
                _id: 1
          batchSize: 3
          session: session0
        result:
          errorContains: read preference in a transaction must be primary
      - name: abortTransaction
        object: session0

    outcome:
      collection:
        data: []

  - description: primaryPreferred readPreference

    operations:
      - name: startTransaction
        object: session0
        arguments:
          options:
            readPreference:
              mode: PrimaryPreferred
      - name: insertMany
        object: collection
        arguments:
          documents: &insertedDocs
            - _id: 1
            - _id: 2
            - _id: 3
            - _id: 4
          session: session0
        result:
          insertedIds: {0: 1, 1: 2, 2: 3, 3: 4}
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          session: session0
          pipeline:
            - $match:
                _id: 1
            - $count: count
        result:
          errorContains: read preference in a transaction must be primary
      - name: find
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          session: session0
          batchSize: 3
        result:
          errorContains: read preference in a transaction must be primary
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          pipeline:
            - $project:
                _id: 1
          batchSize: 3
          session: session0
        result:
          errorContains: read preference in a transaction must be primary
      - name: abortTransaction
        object: session0

    outcome:
      collection:
        data: []

  - description: nearest readPreference

    operations:
      - name: startTransaction
        object: session0
        arguments:
          options:
            readPreference:
              mode: Nearest
      - name: insertMany
        object: collection
        arguments:
          documents: &insertedDocs
            - _id: 1
            - _id: 2
            - _id: 3
            - _id: 4
          session: session0
        result:
          insertedIds: {0: 1, 1: 2, 2: 3, 3: 4}
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          session: session0
          pipeline:
            - $match:
                _id: 1
            - $count: count
        result:
          errorContains: read preference in a transaction must be primary
      - name: find
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          session: session0
          batchSize: 3
        result:
          errorContains: read preference in a transaction must be primary
      - name: aggregate
        object: collection
        collectionOptions:
          readPreference:
            mode: Primary
        arguments:
          pipeline:
            - $project:
                _id: 1
          batchSize: 3
          session: session0
        result:
          errorContains: read preference in a transaction must be primary
      - name: abortTransaction
        object: session0

    outcome:
      collection:
        data: []

  - description: secondary write only

    operations:
      - name: startTransaction
        object: session0
        arguments:
          options:
            readPreference:
              mode: Secondary
      - name: insertOne
        object: collection
        arguments:
          session: session0
          document:
            _id: 1
        result:
          insertedId: 1
      - name: commitTransaction
        object: session0

    outcome:
      collection:
        data:
          - _id: 1
