bulk_index_request_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "time"
  8. )
  9. func TestBulkIndexRequestSerialization(t *testing.T) {
  10. tests := []struct {
  11. Request BulkableRequest
  12. Expected []string
  13. }{
  14. // #0
  15. {
  16. Request: NewBulkIndexRequest().Index("index1").Type("tweet").Id("1").
  17. Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
  18. Expected: []string{
  19. `{"index":{"_id":"1","_index":"index1","_type":"tweet"}}`,
  20. `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
  21. },
  22. },
  23. // #1
  24. {
  25. Request: NewBulkIndexRequest().OpType("create").Index("index1").Type("tweet").Id("1").
  26. Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
  27. Expected: []string{
  28. `{"create":{"_id":"1","_index":"index1","_type":"tweet"}}`,
  29. `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
  30. },
  31. },
  32. // #2
  33. {
  34. Request: NewBulkIndexRequest().OpType("index").Index("index1").Type("tweet").Id("1").
  35. Doc(tweet{User: "olivere", Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}),
  36. Expected: []string{
  37. `{"index":{"_id":"1","_index":"index1","_type":"tweet"}}`,
  38. `{"user":"olivere","message":"","retweets":0,"created":"2014-01-18T23:59:58Z"}`,
  39. },
  40. },
  41. }
  42. for i, test := range tests {
  43. lines, err := test.Request.Source()
  44. if err != nil {
  45. t.Fatalf("case #%d: expected no error, got: %v", i, err)
  46. }
  47. if lines == nil {
  48. t.Fatalf("case #%d: expected lines, got nil", i)
  49. }
  50. if len(lines) != len(test.Expected) {
  51. t.Fatalf("case #%d: expected %d lines, got %d", i, len(test.Expected), len(lines))
  52. }
  53. for j, line := range lines {
  54. if line != test.Expected[j] {
  55. t.Errorf("case #%d: expected line #%d to be %s, got: %s", i, j, test.Expected[j], line)
  56. }
  57. }
  58. }
  59. }