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 5package elastic 6 7import ( 8 "context" 9 "testing" 10) 11 12func TestIndexStatsBuildURL(t *testing.T) { 13 client := setupTestClientAndCreateIndex(t) 14 15 tests := []struct { 16 Indices []string 17 Metrics []string 18 Expected string 19 }{ 20 { 21 []string{}, 22 []string{}, 23 "/_stats", 24 }, 25 { 26 []string{"index1"}, 27 []string{}, 28 "/index1/_stats", 29 }, 30 { 31 []string{}, 32 []string{"metric1"}, 33 "/_stats/metric1", 34 }, 35 { 36 []string{"index1"}, 37 []string{"metric1"}, 38 "/index1/_stats/metric1", 39 }, 40 { 41 []string{"index1", "index2"}, 42 []string{"metric1"}, 43 "/index1%2Cindex2/_stats/metric1", 44 }, 45 { 46 []string{"index1", "index2"}, 47 []string{"metric1", "metric2"}, 48 "/index1%2Cindex2/_stats/metric1%2Cmetric2", 49 }, 50 } 51 52 for i, test := range tests { 53 path, _, err := client.IndexStats().Index(test.Indices...).Metric(test.Metrics...).buildURL() 54 if err != nil { 55 t.Fatalf("case #%d: %v", i+1, err) 56 } 57 if path != test.Expected { 58 t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path) 59 } 60 } 61} 62 63func TestIndexStats(t *testing.T) { 64 client := setupTestClientAndCreateIndexAndAddDocs(t) 65 66 stats, err := client.IndexStats(testIndexName).Do(context.TODO()) 67 if err != nil { 68 t.Fatalf("expected no error; got: %v", err) 69 } 70 if stats == nil { 71 t.Fatalf("expected response; got: %v", stats) 72 } 73 stat, found := stats.Indices[testIndexName] 74 if !found { 75 t.Fatalf("expected stats about index %q; got: %v", testIndexName, found) 76 } 77 if stat.Total == nil { 78 t.Fatalf("expected total to be != nil; got: %v", stat.Total) 79 } 80 if stat.Total.Docs == nil { 81 t.Fatalf("expected total docs to be != nil; got: %v", stat.Total.Docs) 82 } 83 if stat.Total.Docs.Count == 0 { 84 t.Fatalf("expected total docs count to be > 0; got: %d", stat.Total.Docs.Count) 85 } 86} 87