---
"Update Script":

  - do:
      index:
          index:  test_1
          type:   test
          id:     1
          body:
              foo:    bar
              count:  1

  - do:
      update:
          index:  test_1
          type:   test
          id:     1
          body:
            script:
              lang:   painless
              source: "ctx._source.foo = params.bar"
              params: { bar: 'xxx' }

  - match: { _index:   test_1 }
  - match: { _type:    test   }
  - match: { _id:      "1"    }
  - match: { _version: 2      }

  - do:
      get:
          index:  test_1
          type:   test
          id:     1

  - match: { _source.foo:        xxx }
  - match: { _source.count:      1   }

  - do:
      update:
          index:  test_1
          type:   test
          id:     1
          body:
            script:
              lang:   painless
              source: "ctx._source.foo = 'yyy'"

  - match: { _index:   test_1 }
  - match: { _type:    test   }
  - match: { _id:      "1"    }
  - match: { _version: 3      }

  - do:
      get:
          index:  test_1
          type:   test
          id:     1

  - match: { _source.foo:        yyy }
  - match: { _source.count:      1   }

  - do:
      update:
          index:  test_1
          type:   test
          id:     1
          body:
            script:
              lang:   painless
              source: "ctx._source.missing_length = ctx._source.missing?.length()"

  - match: { _index:   test_1 }
  - match: { _type:    test   }
  - match: { _id:      "1"    }
  - match: { _version: 4      }

  - do:
      get:
          index:  test_1
          type:   test
          id:     1

  - match: { _source.foo:        yyy }
  - match: { _source.count:      1   }
  - is_false: _source.missing
  - is_false: _source.missing_length

  - do:
      update:
          index:  test_1
          type:   test
          id:     1
          body:
            script:
              lang:   painless
              source: "ctx._source.foo_length = ctx._source.foo?.length()"

  - match: { _index:   test_1 }
  - match: { _type:    test   }
  - match: { _id:      "1"    }
  - match: { _version: 5      }

  - do:
      get:
          index:  test_1
          type:   test
          id:     1

  - match: { _source.foo:        yyy }
  - match: { _source.foo_length: 3 }
  - match: { _source.count:      1   }
  - is_false: _source.missing
  - is_false: _source.missing_length

---
"Update Script with script error":
  - do:
      index:
          index:  test_1
          type:   test
          id:     2
          body:
              foo:    bar
              count:  1

  - do:
      catch: bad_request
      update:
          index:  test_1
          type:   test
          id:     2
          body:
            script:
              lang:   painless
              source: "for (def key : params.keySet()) { ctx._source[key] = params[key]}"
              params: { bar: 'xxx' }

  - match: { error.root_cause.0.type: "remote_transport_exception" }
  - match: { error.type: "illegal_argument_exception" }
  - match: { error.reason: "Object has already been built and is self-referencing itself" }
