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 88func TestIndexStatsWithShards(t *testing.T) { 89 client := setupTestClientAndCreateIndexAndAddDocs(t) 90 91 stats, err := client.IndexStats(testIndexName).Level("shards").Do(context.TODO()) 92 if err != nil { 93 t.Fatalf("expected no error; got: %v", err) 94 } 95 if stats == nil { 96 t.Fatalf("expected response; got: %v", stats) 97 } 98 stat, found := stats.Indices[testIndexName] 99 if !found { 100 t.Fatalf("expected stats about index %q; got: %v", testIndexName, found) 101 } 102 if stat.Total == nil { 103 t.Fatalf("expected total to be != nil; got: %v", stat.Total) 104 } 105 if stat.Total.Docs == nil { 106 t.Fatalf("expected total docs to be != nil; got: %v", stat.Total.Docs) 107 } 108 if stat.Total.Docs.Count == 0 { 109 t.Fatalf("expected total docs count to be > 0; got: %d", stat.Total.Docs.Count) 110 } 111 if stat.Shards == nil { 112 t.Fatalf("expected shard level information to be != nil; got: %v", stat.Shards) 113 } 114 shard, found := stat.Shards["0"] 115 if !found || shard == nil { 116 t.Fatalf("expected shard level information for shard 0; got: %v (found=%v)", shard, found) 117 } 118 if len(shard) != 1 { 119 t.Fatalf("expected shard level information array to be == 1; got: %v", len(shard)) 120 } 121 if shard[0].Docs == nil { 122 t.Fatalf("expected docs to be != nil; got: %v", shard[0].Docs) 123 } 124} 125