bulk_delete_request_test.go 2.2 KB

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