bulk_update_request_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2012-2015 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "testing"
  7. )
  8. func TestBulkUpdateRequestSerialization(t *testing.T) {
  9. tests := []struct {
  10. Request BulkableRequest
  11. Expected []string
  12. }{
  13. // #0
  14. {
  15. Request: NewBulkUpdateRequest().Index("index1").Type("tweet").Id("1").Doc(struct {
  16. Counter int64 `json:"counter"`
  17. }{
  18. Counter: 42,
  19. }),
  20. Expected: []string{
  21. `{"update":{"_id":"1","_index":"index1","_type":"tweet"}}`,
  22. `{"doc":{"counter":42}}`,
  23. },
  24. },
  25. // #1
  26. {
  27. Request: NewBulkUpdateRequest().Index("index1").Type("tweet").Id("1").
  28. RetryOnConflict(3).
  29. DocAsUpsert(true).
  30. Doc(struct {
  31. Counter int64 `json:"counter"`
  32. }{
  33. Counter: 42,
  34. }),
  35. Expected: []string{
  36. `{"update":{"_id":"1","_index":"index1","_retry_on_conflict":3,"_type":"tweet"}}`,
  37. `{"doc":{"counter":42},"doc_as_upsert":true}`,
  38. },
  39. },
  40. // #2
  41. {
  42. Request: NewBulkUpdateRequest().Index("index1").Type("tweet").Id("1").
  43. RetryOnConflict(3).
  44. Script(`ctx._source.retweets += param1`).
  45. ScriptLang("js").
  46. ScriptParams(map[string]interface{}{"param1": 42}).
  47. Upsert(struct {
  48. Counter int64 `json:"counter"`
  49. }{
  50. Counter: 42,
  51. }),
  52. Expected: []string{
  53. `{"update":{"_id":"1","_index":"index1","_retry_on_conflict":3,"_type":"tweet","upsert":{"counter":42}}}`,
  54. `{"lang":"js","params":{"param1":42},"script":"ctx._source.retweets += param1"}`,
  55. },
  56. },
  57. }
  58. for i, test := range tests {
  59. lines, err := test.Request.Source()
  60. if err != nil {
  61. t.Fatalf("case #%d: expected no error, got: %v", i, err)
  62. }
  63. if lines == nil {
  64. t.Fatalf("case #%d: expected lines, got nil", i)
  65. }
  66. if len(lines) != len(test.Expected) {
  67. t.Fatalf("case #%d: expected %d lines, got %d", i, len(test.Expected), len(lines))
  68. }
  69. for j, line := range lines {
  70. if line != test.Expected[j] {
  71. t.Errorf("case #%d: expected line #%d to be %s, got: %s", i, j, test.Expected[j], line)
  72. }
  73. }
  74. }
  75. }