| 1 |
// Copyright 2012-present 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 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestBulkDeleteRequestSerialization(t *testing.T) { |
| 12 |
tests := []struct { |
| 13 |
Request BulkableRequest |
| 14 |
Expected []string |
| 15 |
}{ |
| 16 |
// #0 |
| 17 |
{ |
| 18 |
Request: NewBulkDeleteRequest().Index("index1").Type("tweet").Id("1"), |
| 19 |
Expected: []string{ |
| 20 |
`{"delete":{"_id":"1","_index":"index1","_type":"tweet"}}`, |
| 21 |
}, |
| 22 |
}, |
| 23 |
// #1 |
| 24 |
{ |
| 25 |
Request: NewBulkDeleteRequest().Index("index1").Type("tweet").Id("1").Parent("2"), |
| 26 |
Expected: []string{ |
| 27 |
`{"delete":{"_id":"1","_index":"index1","_parent":"2","_type":"tweet"}}`, |
| 28 |
}, |
| 29 |
}, |
| 30 |
// #2 |
| 31 |
{ |
| 32 |
Request: NewBulkDeleteRequest().Index("index1").Type("tweet").Id("1").Routing("3"), |
| 33 |
Expected: []string{ |
| 34 |
`{"delete":{"_id":"1","_index":"index1","_routing":"3","_type":"tweet"}}`, |
| 35 |
}, |
| 36 |
}, |
| 37 |
} |
| 38 |
|
| 39 |
for i, test := range tests { |
| 40 |
lines, err := test.Request.Source() |
| 41 |
if err != nil { |
| 42 |
t.Fatalf("case #%d: expected no error, got: %v", i, err) |
| 43 |
} |
| 44 |
if lines == nil { |
| 45 |
t.Fatalf("case #%d: expected lines, got nil", i) |
| 46 |
} |
| 47 |
if len(lines) != len(test.Expected) { |
| 48 |
t.Fatalf("case #%d: expected %d lines, got %d", i, len(test.Expected), len(lines)) |
| 49 |
} |
| 50 |
for j, line := range lines { |
| 51 |
if line != test.Expected[j] { |
| 52 |
t.Errorf("case #%d: expected line #%d to be %s, got: %s", i, j, test.Expected[j], line) |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
var bulkDeleteRequestSerializationResult string |
| 59 |
|
| 60 |
func BenchmarkBulkDeleteRequestSerialization(b *testing.B) { |
| 61 |
b.Run("stdlib", func(b *testing.B) { |
| 62 |
r := NewBulkDeleteRequest().Index(testIndexName).Type("doc").Id("1") |
| 63 |
benchmarkBulkDeleteRequestSerialization(b, r.UseEasyJSON(false)) |
| 64 |
}) |
| 65 |
b.Run("easyjson", func(b *testing.B) { |
| 66 |
r := NewBulkDeleteRequest().Index(testIndexName).Type("doc").Id("1") |
| 67 |
benchmarkBulkDeleteRequestSerialization(b, r.UseEasyJSON(true)) |
| 68 |
}) |
| 69 |
} |
| 70 |
|
| 71 |
func benchmarkBulkDeleteRequestSerialization(b *testing.B, r *BulkDeleteRequest) { |
| 72 |
var s string |
| 73 |
for n := 0; n < b.N; n++ { |
| 74 |
s = r.String() |
| 75 |
r.source = nil // Don't let caching spoil the benchmark |
| 76 |
} |
| 77 |
bulkDeleteRequestSerializationResult = s // ensure the compiler doesn't optimize |
| 78 |
b.ReportAllocs() |
| 79 |
} |