runOn:
    -
        minServerVersion: "4.0"
        topology: ["replicaset"]
    -
        minServerVersion: "4.1.8"
        topology: ["sharded"]

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

data: []

tests:
  -
    description: withTransaction and no transaction options set
    useMultipleMongoses: true
    operations: &operations
      -
        name: withTransaction
        object: session0
        arguments:
          callback:
            operations:
              -
                name: insertOne
                object: collection
                arguments:
                  session: session0
                  document: { _id: 1 }
                result:
                  insertedId: 1
    expectations:
      -
        command_started_event:
          command:
            insert: *collection_name
            documents:
              - { _id: 1 }
            ordered: true
            lsid: session0
            txnNumber: { $numberLong: "1" }
            startTransaction: true
            autocommit: false
            # omitted fields
            readConcern: ~
            writeConcern: ~
          command_name: insert
          database_name: *database_name
      -
        command_started_event:
          command:
            commitTransaction: 1
            lsid: session0
            txnNumber: { $numberLong: "1" }
            autocommit: false
            # omitted fields
            readConcern: ~
            startTransaction: ~
            writeConcern: ~
          command_name: commitTransaction
          database_name: admin
    outcome: &outcome
      collection:
        data:
          - { _id: 1 }
  -
    description: withTransaction inherits transaction options from client
    useMultipleMongoses: true
    clientOptions:
      readConcernLevel: local
      w: 1
    operations: *operations
    expectations:
      -
        command_started_event:
          command:
            insert: *collection_name
            documents:
              - { _id: 1 }
            ordered: true
            lsid: session0
            txnNumber: { $numberLong: "1" }
            startTransaction: true
            autocommit: false
            readConcern: { level: local }
            # omitted fields
            writeConcern: ~
          command_name: insert
          database_name: *database_name
      -
        command_started_event:
          command:
            commitTransaction: 1
            lsid: session0
            txnNumber: { $numberLong: "1" }
            autocommit: false
            writeConcern: { w: 1 }
            # omitted fields
            readConcern: ~
            startTransaction: ~
          command_name: commitTransaction
          database_name: admin
    outcome: *outcome
  -
    description: withTransaction inherits transaction options from defaultTransactionOptions
    useMultipleMongoses: true
    sessionOptions:
      session0:
        defaultTransactionOptions:
          readConcern: { level: snapshot }
          writeConcern: { w: 1 }
    operations: *operations
    expectations:
      -
        command_started_event:
          command:
            insert: *collection_name
            documents:
              - { _id: 1 }
            ordered: true
            lsid: session0
            txnNumber: { $numberLong: "1" }
            startTransaction: true
            autocommit: false
            readConcern: { level: snapshot }
            # omitted fields
            writeConcern: ~
          command_name: insert
          database_name: *database_name
      -
        command_started_event:
          command:
            commitTransaction: 1
            lsid: session0
            txnNumber: { $numberLong: "1" }
            autocommit: false
            writeConcern: { w: 1 }
            # omitted fields
            readConcern: ~
            startTransaction: ~
          command_name: commitTransaction
          database_name: admin
    outcome: *outcome
  -
    description: withTransaction explicit transaction options
    useMultipleMongoses: true
    operations: &operations_explicit_transactionOptions
      -
        name: withTransaction
        object: session0
        arguments:
          callback:
            operations:
              -
                name: insertOne
                object: collection
                arguments:
                  session: session0
                  document: { _id: 1 }
                result:
                  insertedId: 1
          options:
            readConcern: { level: snapshot }
            writeConcern: { w: 1 }
    expectations:
      -
        command_started_event:
          command:
            insert: *collection_name
            documents:
              - { _id: 1 }
            ordered: true
            lsid: session0
            txnNumber: { $numberLong: "1" }
            startTransaction: true
            autocommit: false
            readConcern: { level: snapshot }
            # omitted fields
            writeConcern: ~
          command_name: insert
          database_name: *database_name
      -
        command_started_event:
          command:
            commitTransaction: 1
            lsid: session0
            txnNumber: { $numberLong: "1" }
            autocommit: false
            writeConcern: { w: 1 }
            # omitted fields
            readConcern: ~
            startTransaction: ~
          command_name: commitTransaction
          database_name: admin
    outcome: *outcome
  -
    description: withTransaction explicit transaction options override defaultTransactionOptions
    useMultipleMongoses: true
    sessionOptions:
      session0:
        defaultTransactionOptions:
          readConcern: { level: majority }
          writeConcern: { w: majority }
    operations: *operations_explicit_transactionOptions
    expectations:
      -
        command_started_event:
          command:
            insert: *collection_name
            documents:
              - { _id: 1 }
            ordered: true
            lsid: session0
            txnNumber: { $numberLong: "1" }
            startTransaction: true
            autocommit: false
            readConcern: { level: snapshot }
            # omitted fields
            writeConcern: ~
          command_name: insert
          database_name: *database_name
      -
        command_started_event:
          command:
            commitTransaction: 1
            lsid: session0
            txnNumber: { $numberLong: "1" }
            autocommit: false
            writeConcern: { w: 1 }
            # omitted fields
            readConcern: ~
            startTransaction: ~
          command_name: commitTransaction
          database_name: admin
    outcome: *outcome
  -
    description: withTransaction explicit transaction options override client options
    useMultipleMongoses: true
    clientOptions:
      readConcernLevel: majority
      w: majority
    operations: *operations_explicit_transactionOptions
    expectations:
      -
        command_started_event:
          command:
            insert: *collection_name
            documents:
              - { _id: 1 }
            ordered: true
            lsid: session0
            txnNumber: { $numberLong: "1" }
            startTransaction: true
            autocommit: false
            readConcern: { level: snapshot }
            # omitted fields
            writeConcern: ~
          command_name: insert
          database_name: *database_name
      -
        command_started_event:
          command:
            commitTransaction: 1
            lsid: session0
            txnNumber: { $numberLong: "1" }
            autocommit: false
            writeConcern: { w: 1 }
            # omitted fields
            readConcern: ~
            startTransaction: ~
          command_name: commitTransaction
          database_name: admin
    outcome: *outcome