github.com/go-graphite/carbonapi@v0.17.0/cmd/carbonapi/http/expand_handler_test.go (about) 1 package http 2 3 import ( 4 "testing" 5 6 pbv3 "github.com/go-graphite/protocol/carbonapi_v3_pb" 7 ) 8 9 func TestExpandEncoder(t *testing.T) { 10 var tests = []struct { 11 name string 12 metricIn pbv3.MultiGlobResponse 13 metricOut string 14 leavesOnly string 15 groupByExpr string 16 }{ 17 { 18 name: "test1", 19 metricIn: pbv3.MultiGlobResponse{ 20 Metrics: []pbv3.GlobResponse{ 21 { 22 Name: "foo.ba*", 23 Matches: []pbv3.GlobMatch{ 24 {Path: "foo.bar", IsLeaf: false}, 25 {Path: "foo.bat", IsLeaf: true}, 26 }, 27 }, 28 }, 29 }, 30 metricOut: "{\"results\":[\"foo.bar\",\"foo.bat\"]}", 31 leavesOnly: "0", 32 groupByExpr: "0", 33 }, 34 { 35 name: "test2", 36 metricIn: pbv3.MultiGlobResponse{ 37 Metrics: []pbv3.GlobResponse{ 38 { 39 Name: "foo.ba*", 40 Matches: []pbv3.GlobMatch{ 41 {Path: "foo.bar", IsLeaf: false}, 42 {Path: "foo.bat", IsLeaf: true}, 43 }, 44 }, 45 }, 46 }, 47 metricOut: "{\"results\":[\"foo.bat\"]}", 48 leavesOnly: "1", 49 groupByExpr: "0", 50 }, 51 { 52 name: "test3", 53 metricIn: pbv3.MultiGlobResponse{ 54 Metrics: []pbv3.GlobResponse{ 55 { 56 Name: "foo.ba*", 57 Matches: []pbv3.GlobMatch{ 58 {Path: "foo.bar", IsLeaf: false}, 59 {Path: "foo.bat", IsLeaf: true}, 60 }, 61 }, 62 }, 63 }, 64 metricOut: "{\"results\":{\"foo.ba*\":[\"foo.bar\",\"foo.bat\"]}}", 65 leavesOnly: "0", 66 groupByExpr: "1", 67 }, 68 { 69 name: "test4", 70 metricIn: pbv3.MultiGlobResponse{ 71 Metrics: []pbv3.GlobResponse{ 72 { 73 Name: "foo.ba*", 74 Matches: []pbv3.GlobMatch{ 75 {Path: "foo.bar", IsLeaf: false}, 76 {Path: "foo.bat", IsLeaf: true}, 77 }, 78 }, 79 { 80 Name: "foo.ba*.*", 81 Matches: []pbv3.GlobMatch{ 82 {Path: "foo.bar", IsLeaf: false}, 83 {Path: "foo.bat", IsLeaf: true}, 84 {Path: "foo.bar.baz", IsLeaf: true}, 85 }, 86 }, 87 }, 88 }, 89 metricOut: "{\"results\":{\"foo.ba*\":[\"foo.bar\",\"foo.bat\"],\"foo.ba*.*\":[\"foo.bar.baz\"]}}", 90 leavesOnly: "0", 91 groupByExpr: "1", 92 }, 93 } 94 for _, tst := range tests { 95 tst := tst 96 t.Run(tst.name, func(t *testing.T) { 97 response, _ := expandEncoder(&tst.metricIn, tst.leavesOnly, tst.groupByExpr) 98 if tst.metricOut != string(response) { 99 t.Errorf("%v should be same as %v", tst.metricOut, string(response)) 100 } 101 }) 102 } 103 }