---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    bulk:
      index: "test"
      refresh: true
      body:
      - index: {}
      - '@timestamp': "2022-01-01T00:00:00"
        user: 1
      - index: {}
      - '@timestamp': "2022-01-01T01:00:00"
        user: 2
      - index: {}
      - '@timestamp': "2022-01-01T02:00:00"
        user: 1
      - index: {}
      - '@timestamp': "2022-01-02T00:00:00"
        user: 1
      - index: {}
      - '@timestamp': "2022-01-02T01:00:00"
        user: 2
      - index: {}
      - '@timestamp': "2022-01-02T02:00:00"
        user: 3
      - index: {}
      - '@timestamp': "2022-01-03T00:00:00"
        user: 1
      - index: {}
      - '@timestamp': "2022-01-03T01:00:00"
        user: 2
      - index: {}
      - '@timestamp': "2022-01-03T03:00:00"
        user: 4
      - index: {}
      - '@timestamp': "2022-01-04T00:00:00"
        user: 1
      - index: {}
      - '@timestamp': "2022-01-04T01:00:00"
        user: 5
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
basic:
- do:
    search:
      index: "test"
      body:
        size: 0
        aggs:
          '@timestamp':
            date_histogram:
              field: "@timestamp"
              calendar_interval: "day"
            aggs:
              distinct_users:
                cardinality:
                  field: "user"
              total_users:
                cumulative_cardinality:
                  buckets_path: "distinct_users"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    hits.total.value: 11
- length:
    aggregations.@timestamp.buckets: 4
- match:
    aggregations.@timestamp.buckets.0.key_as_string: "2022-01-01T00:00:00.000Z"
- match:
    aggregations.@timestamp.buckets.0.distinct_users.value: 2
- match:
    aggregations.@timestamp.buckets.0.total_users.value: 2
- match:
    aggregations.@timestamp.buckets.1.key_as_string: "2022-01-02T00:00:00.000Z"
- match:
    aggregations.@timestamp.buckets.1.distinct_users.value: 3
- match:
    aggregations.@timestamp.buckets.1.total_users.value: 3
- match:
    aggregations.@timestamp.buckets.2.key_as_string: "2022-01-03T00:00:00.000Z"
- match:
    aggregations.@timestamp.buckets.2.distinct_users.value: 3
- match:
    aggregations.@timestamp.buckets.2.total_users.value: 4
- match:
    aggregations.@timestamp.buckets.3.key_as_string: "2022-01-04T00:00:00.000Z"
- match:
    aggregations.@timestamp.buckets.3.distinct_users.value: 2
- match:
    aggregations.@timestamp.buckets.3.total_users.value: 5
---
format:
- do:
    search:
      index: "test"
      body:
        size: 0
        aggs:
          '@timestamp':
            date_histogram:
              field: "@timestamp"
              calendar_interval: "day"
            aggs:
              distinct_users:
                cardinality:
                  field: "user"
              total_users:
                cumulative_cardinality:
                  buckets_path: "distinct_users"
                  format: "00"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    hits.total.value: 11
- length:
    aggregations.@timestamp.buckets: 4
- match:
    aggregations.@timestamp.buckets.0.key_as_string: "2022-01-01T00:00:00.000Z"
- match:
    aggregations.@timestamp.buckets.0.distinct_users.value: 2
- match:
    aggregations.@timestamp.buckets.0.total_users.value_as_string: "02"
- match:
    aggregations.@timestamp.buckets.1.key_as_string: "2022-01-02T00:00:00.000Z"
- match:
    aggregations.@timestamp.buckets.1.distinct_users.value: 3
- match:
    aggregations.@timestamp.buckets.1.total_users.value_as_string: "03"
- match:
    aggregations.@timestamp.buckets.2.key_as_string: "2022-01-03T00:00:00.000Z"
- match:
    aggregations.@timestamp.buckets.2.distinct_users.value: 3
- match:
    aggregations.@timestamp.buckets.2.total_users.value_as_string: "04"
- match:
    aggregations.@timestamp.buckets.3.key_as_string: "2022-01-04T00:00:00.000Z"
- match:
    aggregations.@timestamp.buckets.3.distinct_users.value: 2
- match:
    aggregations.@timestamp.buckets.3.total_users.value_as_string: "05"
---
no results:
- do:
    search:
      index: "test"
      body:
        size: 0
        query:
          match:
            missing_field: "not_found"
        aggs:
          '@timestamp':
            date_histogram:
              field: "@timestamp"
              calendar_interval: "day"
            aggs:
              distinct_users:
                cardinality:
                  field: "user"
              total_users:
                cumulative_cardinality:
                  buckets_path: "distinct_users"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    hits.total.value: 0
- length:
    aggregations.@timestamp.buckets: 0
---
bad path:
- do:
    catch: "/Validation Failed: 1: No aggregation found for path \\[missing\\];/"
    search:
      index: "test"
      body:
        size: 0
        aggs:
          '@timestamp':
            date_histogram:
              field: "@timestamp"
              calendar_interval: "day"
            aggs:
              distinct_users:
                cardinality:
                  field: "user"
              total_users:
                cumulative_cardinality:
                  buckets_path: "missing"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
