description: "Transaction handling"

schemaVersion: "1.3"

runOnRequirements:
  - minServerVersion: "4.9"
    topologies: [ replicaset, sharded, load-balanced ]

createEntities:
  - client:
      id: &client client
      observeEvents:
        - commandStartedEvent
      serverApi:
        version: "1"
  - database:
      id: &database database
      client: *client
      databaseName: &databaseName versioned-api-tests
  - collection:
      id: &collection collection
      database: *database
      collectionName: &collectionName test
  - session:
      id: &session session
      client: *client

_yamlAnchors:
  versions:
    - &expectedApiVersion
      apiVersion: "1"
      apiStrict: { $$unsetOrMatches: false }
      apiDeprecationErrors: { $$unsetOrMatches: false }


initialData:
  - collectionName: *collectionName
    databaseName: *databaseName
    documents:
      - { _id: 1, x: 11 }
      - { _id: 2, x: 22 }
      - { _id: 3, x: 33 }
      - { _id: 4, x: 44 }
      - { _id: 5, x: 55 }

tests:
  - description: "All commands in a transaction declare an API version"
    runOnRequirements:
      - topologies: [ replicaset, sharded, load-balanced ]
    operations:
      - name: startTransaction
        object: *session
      - name: insertOne
        object: *collection
        arguments:
          session: *session
          document: { _id: 6, x: 66 }
        expectResult: { $$unsetOrMatches: { insertedId: { $$unsetOrMatches: 6 } } }
      - name: insertOne
        object: *collection
        arguments:
          session: *session
          document: { _id: 7, x: 77 }
        expectResult: { $$unsetOrMatches: { insertedId: { $$unsetOrMatches: 7 } } }
      - name: commitTransaction
        object: *session
    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              command:
                insert: *collectionName
                documents: [ { _id: 6, x: 66 } ]
                lsid: { $$sessionLsid: *session }
                startTransaction: true
                <<: *expectedApiVersion
          - commandStartedEvent:
              command:
                insert: *collectionName
                documents: [ { _id: 7, x: 77 } ]
                lsid: { $$sessionLsid: *session }
                <<: *expectedApiVersion
          - commandStartedEvent:
              command:
                commitTransaction: 1
                lsid: { $$sessionLsid: *session }
                <<: *expectedApiVersion
  - description: "abortTransaction includes an API version"
    runOnRequirements:
      - topologies: [ replicaset, sharded, load-balanced ]
    operations:
      - name: startTransaction
        object: *session
      - name: insertOne
        object: *collection
        arguments:
          session: *session
          document: { _id: 6, x: 66 }
        expectResult: { $$unsetOrMatches: { insertedId: { $$unsetOrMatches: 6 } } }
      - name: insertOne
        object: *collection
        arguments:
          session: *session
          document: { _id: 7, x: 77 }
        expectResult: { $$unsetOrMatches: { insertedId: { $$unsetOrMatches: 7 } } }
      - name: abortTransaction
        object: *session
    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              command:
                insert: *collectionName
                documents: [ { _id: 6, x: 66 } ]
                lsid: { $$sessionLsid: *session }
                startTransaction: true
                <<: *expectedApiVersion
          - commandStartedEvent:
              command:
                insert: *collectionName
                documents: [ { _id: 7, x: 77 } ]
                lsid: { $$sessionLsid: *session }
                <<: *expectedApiVersion
          - commandStartedEvent:
              command:
                abortTransaction: 1
                lsid: { $$sessionLsid: *session }
                <<: *expectedApiVersion
