# Integration tests for the version field
#
---
setup:

  - requires:
      test_runner_features: headers
      cluster_features: ["gte_v7.10.1"]
      reason: "version field is added to 7.10"

  - do:
        indices.create:
          index: test_index
          body:
              mappings:
                properties:
                  version:
                    type: version

  - do:
        bulk:
          refresh: true
          body:
            - '{ "index" : { "_index" : "test_index", "_id" : "1" } }'
            - '{"version": "1.1.0" }'
            - '{ "index" : { "_index" : "test_index", "_id" : "2" } }'
            - '{"version": "2.0.0-beta" }'
            - '{ "index" : { "_index" : "test_index", "_id" : "3" } }'
            - '{"version": "3.1.0" }'

---
"Store malformed":
  - do:
        indices.create:
          index: test_malformed
          body:
              mappings:
                properties:
                  version:
                    type: version

  - do:
        bulk:
          refresh: true
          body:
            - '{ "index" : { "_index" : "test_malformed", "_id" : "1" } }'
            - '{"version": "1.1.0" }'
            - '{ "index" : { "_index" : "test_malformed", "_id" : "2" } }'
            - '{"version": "2.0.0-beta" }'
            - '{ "index" : { "_index" : "test_malformed", "_id" : "3" } }'
            - '{"version": "v3.1.0" }'
            - '{ "index" : { "_index" : "test_malformed", "_id" : "4" } }'
            - '{"version": "1.el6" }'

  - do:
        search:
          index: test_malformed
          body:
            query: { "match" : { "version" : "1.el6" } }

  - do:
        search:
          index: test_malformed
          body:
            query: { "match_all" : { } }
            sort:
              version: asc

  - match: { hits.total.value: 4 }
  - match: { hits.hits.0._source.version: "1.1.0" }
  - match: { hits.hits.1._source.version: "2.0.0-beta" }
  - match: { hits.hits.2._source.version: "1.el6" }
  - match: { hits.hits.3._source.version: "v3.1.0" }

---
"Basic ranges":
  - do:
        search:
          index: test_index
          body:
            query: { "range" : { "version" : { "gt" : "1.1.0", "lt" : "9999" } } }

  - match: { hits.total.value: 2 }

  - do:
        search:
          index: test_index
          body:
            query: { "range" : { "version" : { "gte" : "1.1.0", "lt" : "9999" } } }

  - match: { hits.total.value: 3 }

  - do:
        search:
          index: test_index
          body:
            query: { "range" : { "version" : { "gte" : "2.0.0", "lt" : "9999" } } }

  - match: { hits.total.value: 1 }

  - do:
        search:
          index: test_index
          body:
            query: { "range" : { "version" : { "gte" : "2.0.0-alpha", "lt" : "9999" } } }

  - match: { hits.total.value: 2 }
