decoder_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. "bytes"
  7. "context"
  8. "encoding/json"
  9. "sync/atomic"
  10. "testing"
  11. )
  12. type decoder struct {
  13. dec json.Decoder
  14. N int64
  15. }
  16. func (d *decoder) Decode(data []byte, v interface{}) error {
  17. atomic.AddInt64(&d.N, 1)
  18. dec := json.NewDecoder(bytes.NewReader(data))
  19. dec.UseNumber()
  20. return dec.Decode(v)
  21. }
  22. func TestDecoder(t *testing.T) {
  23. dec := &decoder{}
  24. client := setupTestClientAndCreateIndex(t, SetDecoder(dec), SetMaxRetries(0))
  25. tweet := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  26. // Add a document
  27. indexResult, err := client.Index().
  28. Index(testIndexName).
  29. Type("tweet").
  30. Id("1").
  31. BodyJson(&tweet).
  32. Do(context.TODO())
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if indexResult == nil {
  37. t.Errorf("expected result to be != nil; got: %v", indexResult)
  38. }
  39. if dec.N == 0 {
  40. t.Errorf("expected at least 1 call of decoder; got: %d", dec.N)
  41. }
  42. }