decoder_test.go 1.0 KB

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