description: "estimatedDocumentCount"

schemaVersion: "1.0"

createEntities:
  - client:
      id: &client0 client0
      useMultipleMongoses: false # Avoid setting fail points with multiple mongoses
      uriOptions: { retryReads: false } # Avoid retrying fail points with closeConnection
      observeEvents: [ commandStartedEvent ]
  - database:
      id: &database0 database0
      client: *client0
      databaseName: &database0Name edc-tests
  - collection:
      id: &collection0 collection0
      database: *database0
      collectionName: &collection0Name coll0
  - collection:
      # Nonexistent collection intentionally omitted from initialData
      id: &collection1 collection1
      database: *database0
      collectionName: &collection1Name coll1
  - collection:
      id: &collection0View collection0View
      database: *database0
      collectionName: &collection0ViewName coll0view

initialData:
  - collectionName: *collection0Name
    databaseName: *database0Name
    documents:
      - { _id: 1, x: 11 }
      - { _id: 2, x: 22 }
      - { _id: 3, x: 33 }

tests:
  - description: "estimatedDocumentCount always uses count"
    operations:
      - name: estimatedDocumentCount
        object: *collection0
        expectResult: 3
    expectEvents:
      - client: *client0
        events:
          - commandStartedEvent:
              command:
                count: *collection0Name
              commandName: count
              databaseName: *database0Name

  - description: "estimatedDocumentCount with maxTimeMS"
    operations:
      - name: estimatedDocumentCount
        object: *collection0
        arguments:
          maxTimeMS: 6000
        expectResult: 3
    expectEvents:
      - client: *client0
        events:
          - commandStartedEvent:
              command:
                count: *collection0Name
                maxTimeMS: 6000
              commandName: count
              databaseName: *database0Name

  - description: "estimatedDocumentCount on non-existent collection"
    operations:
      - name: estimatedDocumentCount
        object: *collection1
        expectResult: 0
    expectEvents:
      - client: *client0
        events:
          - commandStartedEvent:
              command:
                count: *collection1Name
              commandName: count
              databaseName: *database0Name

  - description: "estimatedDocumentCount errors correctly--command error"
    runOnRequirements:
      - minServerVersion: "4.0.0"
        topologies: [ single, replicaset ]
      - minServerVersion: "4.2.0"
        topologies: [ sharded ]
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *client0
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 1 }
            data:
              failCommands: [ count ]
              errorCode: 8 # UnknownError
      - name: estimatedDocumentCount
        object: *collection0
        expectError:
          errorCode: 8 # UnknownError
    expectEvents:
      - client: *client0
        events:
          - commandStartedEvent:
              command:
                count: *collection0Name
              commandName: count
              databaseName: *database0Name

  - description: "estimatedDocumentCount errors correctly--socket error"
    runOnRequirements:
      - minServerVersion: "4.0.0"
        topologies: [ single, replicaset ]
      - minServerVersion: "4.2.0"
        topologies: [ sharded ]
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *client0
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 1 }
            data:
              failCommands: [ count ]
              closeConnection: true
      - name: estimatedDocumentCount
        object: *collection0
        expectError:
          isError: true
    expectEvents:
      - client: *client0
        events:
          - commandStartedEvent:
              command:
                count: *collection0Name
              commandName: count
              databaseName: *database0Name

  - description: "estimatedDocumentCount works correctly on views"
    # viewOn option was added to the create command in 3.4
    runOnRequirements:
      - minServerVersion: "3.4.0"
    operations:
      - name: dropCollection
        object: *database0
        arguments:
          collection: *collection0ViewName
      - name: createCollection
        object: *database0
        arguments:
          collection: *collection0ViewName
          viewOn: *collection0Name
          pipeline: &pipeline
            - { $match: { _id: { $gt: 1 } } }
      - name: estimatedDocumentCount
        object: *collection0View
        expectResult: 2
    expectEvents:
      - client: *client0
        events:
          - commandStartedEvent:
              command:
                drop: *collection0ViewName
              commandName: drop
              databaseName: *database0Name
          - commandStartedEvent:
              command:
                create: *collection0ViewName
                viewOn: *collection0Name
                pipeline: *pipeline
              commandName: create
              databaseName: *database0Name
          - commandStartedEvent:
              command:
                count: *collection0ViewName
              commandName: count
              databaseName: *database0Name
