Debian Codegraph @

decoder_test.go

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