---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
---
Test neither job id nor body:
- do:
    catch: "/Please provide a job \\[id\\] or the config object/"
    ml.preview_data_frame_analytics:
      id: ""
    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"
---
Test both job id and body:
- do:
    catch: "/Please provide either a job \\[id\\] or the config object but not both/"
    ml.preview_data_frame_analytics:
      id: "foo"
      body:
        config:
          source:
            index: "index-source"
          analysis:
            outlier_detection: {}
    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"
---
Test missing job:
- do:
    catch: "missing"
    ml.preview_data_frame_analytics:
      id: "no_such_job"
    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"
---
Test id that matches multiple jobs:
- do:
    indices.create:
      index: "index-source"
    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"
- do:
    ml.put_data_frame_analytics:
      id: "foo-1"
      body: "{\n  \"source\": {\n    \"index\": \"index-source\"\n  },\n  \"dest\"\
        : {\n    \"index\": \"index-dest\"\n  },\n  \"analysis\": {\"outlier_detection\"\
        :{}}\n}\n"
    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"
- do:
    ml.put_data_frame_analytics:
      id: "foo-2"
      body: "{\n  \"source\": {\n    \"index\": \"index-source\"\n  },\n  \"dest\"\
        : {\n    \"index\": \"index-dest\"\n  },\n  \"analysis\": {\"outlier_detection\"\
        :{}}\n}\n"
    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"
- do:
    catch: "/expected only one config but matched \\[foo-1, foo-2\\]/"
    ml.preview_data_frame_analytics:
      id: "foo-*"
    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"
---
Test feature preview given body:
- do:
    indices.create:
      index: "index-source"
      body:
        mappings:
          properties:
            field_1:
              type: "integer"
            field_2:
              type: "double"
            field_3:
              type: "date"
    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"
- do:
    index:
      index: "index-source"
      refresh: true
      body:
        field_1: 3
        field_2: 3.14
        field_3: "2019-11-11T00:00:00"
        field_4: "blah"
    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:
    result: "created"
- do:
    ml.preview_data_frame_analytics:
      body:
        config:
          source:
            index: "index-source"
          analysis:
            regression:
              dependent_variable: "field_1"
              feature_processors:
              - n_gram_encoding:
                  field: "field_4"
                  feature_prefix: "f"
                  length: 2
                  n_grams:
                  - 1
    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:
    feature_values.0:
      field_2: "3.14"
      f.10: "b"
      f.11: "l"
      field_1: "3"
- is_false: "feature_values.0.field_4"
---
Test feature preview given job:
- do:
    indices.create:
      index: "index-source"
      body:
        mappings:
          properties:
            field_1:
              type: "integer"
            field_2:
              type: "double"
            field_3:
              type: "date"
    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"
- do:
    index:
      index: "index-source"
      refresh: true
      body:
        field_1: 3
        field_2: 3.14
        field_3: "2019-11-11T00:00:00"
        field_4: "blah"
    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:
    result: "created"
- do:
    ml.put_data_frame_analytics:
      id: "ngram-encoding-preview-job"
      body: "{\n  \"source\": {\n    \"index\": \"index-source\"\n  },\n  \"dest\"\
        : {\n    \"index\": \"index-dest\"\n  },\n  \"analysis\": { \"regression\"\
        : { \"dependent_variable\": \"field_1\", \"feature_processors\": [{\"n_gram_encoding\"\
        : {\"field\": \"field_4\", \"feature_prefix\": \"f\", \"length\": 2, \"n_grams\"\
        : [1]}}] } }\n}\n"
    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"
- do:
    ml.preview_data_frame_analytics:
      id: "ngram-encoding-preview-job"
    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:
    feature_values.0:
      field_2: "3.14"
      f.10: "b"
      f.11: "l"
      field_1: "3"
- is_false: "feature_values.0.field_4"
