github.com/go-graphite/carbonapi@v0.17.0/pkg/parser/define_test.go (about) 1 package parser 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestDefineExpand(t *testing.T) { 10 assert := assert.New(t) 11 12 defer defineCleanUp() 13 14 assert.NoError(Define("constMetric", "metric.name")) 15 assert.NoError(Define("perMinute", "perSecond({{.argString}})|scale(60)")) 16 assert.NoError(Define("funcAlias", "funcOrig({{index .args 0}},{{index .args 1}})")) 17 assert.NoError(Define("funcAlias2", "funcOrig2({{index .args 0}},{{index .kwargs \"key\"}})")) 18 assert.NoError(Define("object", "object.*.*.{{index .args 0}}")) 19 20 tests := []struct { 21 s string 22 e *expr 23 }{ 24 { 25 "func1(metric1,func2(metricA, metricB),metric3)", 26 &expr{ 27 target: "func1", 28 etype: EtFunc, 29 args: []*expr{ 30 {target: "metric1"}, 31 {target: "func2", 32 etype: EtFunc, 33 args: []*expr{{target: "metricA"}, {target: "metricB"}}, 34 argString: "metricA, metricB", 35 }, 36 {target: "metric3"}}, 37 argString: "metric1,func2(metricA, metricB),metric3", 38 }, 39 }, 40 { 41 "func1(metric1,constMetric(metricA, metricB),metric3)", 42 &expr{ 43 target: "func1", 44 etype: EtFunc, 45 args: []*expr{ 46 {target: "metric1"}, 47 {target: "metric.name"}, 48 {target: "metric3"}}, 49 argString: "metric1,constMetric(metricA, metricB),metric3", 50 }, 51 }, 52 { 53 "func1(metric1,perMinute(metricA),metric3)", 54 &expr{ 55 target: "func1", 56 etype: EtFunc, 57 args: []*expr{ 58 {target: "metric1"}, 59 {target: "scale", 60 etype: EtFunc, 61 args: []*expr{ 62 {target: "perSecond", 63 etype: EtFunc, 64 args: []*expr{ 65 {target: "metricA"}, 66 }, 67 argString: "metricA", 68 }, 69 {etype: EtConst, 70 val: 60.000000, 71 valStr: "60", 72 }, 73 }, 74 argString: "perSecond(metricA),60", 75 }, 76 {target: "metric3"}}, 77 argString: "metric1,perMinute(metricA),metric3", 78 }, 79 }, 80 { 81 "funcAlias(metricA,metricB)", 82 &expr{ 83 target: "funcOrig", 84 etype: EtFunc, 85 args: []*expr{ 86 {target: "metricA"}, 87 {target: "metricB"}, 88 }, 89 argString: "metricA,metricB", 90 }, 91 }, 92 { 93 "funcAlias2(metricA,key=\"42\")", 94 &expr{ 95 target: "funcOrig2", 96 etype: EtFunc, 97 args: []*expr{ 98 {target: "metricA"}, 99 {valStr: "42", etype: EtString}, 100 }, 101 argString: "metricA,'42'", 102 }, 103 }, 104 { 105 "object(9554433)", 106 &expr{ 107 target: "object.*.*.9554433", 108 }, 109 }, 110 } 111 112 for _, tt := range tests { 113 e, _, err := ParseExpr(tt.s) 114 if err != nil { 115 t.Errorf("parse for %+v failed: err=%v", tt.s, err) 116 continue 117 } 118 119 assert.Equal(tt.e, e, tt.s) 120 } 121 }