github.com/go-graphite/carbonapi@v0.17.0/zipper/protocols/irondb/irondb_helpers_test.go (about) 1 package irondb 2 3 import ( 4 "testing" 5 ) 6 7 func TestGraphiteExprListToIronDBTagQuery(t *testing.T) { 8 cases := []struct { 9 desc string 10 input []string 11 expectedOutput string 12 }{ 13 {"TestEmpty", []string{}, ""}, 14 {"TestNameEq", []string{"name=host"}, "and(__name:host)"}, 15 {"TestEq", []string{"tag=value"}, "and(tag:value)"}, 16 {"TestEqTilda", []string{"tag=~value.*"}, "and(tag:/value.*/)"}, 17 {"TestNotEq", []string{"tag!=value"}, "not(tag:value)"}, 18 {"TestNotEqTilda", []string{"tag!=~value.*"}, "not(tag:/value.*/)"}, 19 {"TestNotEqTildaWrong", []string{"tag!~value.*"}, ""}, 20 {"TestComplex", []string{`tag!=~value\..*`, "tag2=host", "tag!=cat3"}, `and(not(tag:/value\..*/),and(tag2:host),not(tag:cat3))`}, 21 } 22 for _, tc := range cases { 23 output := graphiteExprListToIronDBTagQuery(tc.input) 24 if output != tc.expectedOutput { 25 t.Fatalf("%s: expected value: %s got: %s for input: %s", 26 tc.desc, tc.expectedOutput, output, tc.input) 27 } 28 } 29 } 30 31 func TestConvertNameToGraphite(t *testing.T) { 32 cases := []struct { 33 desc string 34 input string 35 expectedOutput string 36 }{ 37 {"TestEmpty", "", ""}, 38 {"Test1", "name|ST[c1=v1,c2=v2,c3=v3]", "name;c1=v1;c2=v2;c3=v3"}, 39 {"Test1", "name|ST[c1=v1,c2=v2,c3=v3]|ST[a=b]", "name;c1=v1;c2=v2;c3=v3;a=b"}, 40 {"Test1", "name|ST[c1=v1,c2=v2,c3=v3]|MT{taag}", "name;c1=v1;c2=v2;c3=v3"}, 41 {"Test1", "name|MT{tag1}|ST[c1=v1,c2=v2,c3=v3]|MT{tag}", "name;c1=v1;c2=v2;c3=v3"}, 42 {"Test1", "name|ST[a=b]|ST[c1=v1,c2=v2,c3=v3]", "name;a=b;c1=v1;c2=v2;c3=v3"}, 43 } 44 for _, tc := range cases { 45 output := convertNameToGraphite(tc.input) 46 if output != tc.expectedOutput { 47 t.Fatalf("%s: expected value: %s got: %s for input: %s", 48 tc.desc, tc.expectedOutput, output, tc.input) 49 } 50 } 51 } 52 53 func BenchmarkConvertNameToGraphite(b *testing.B) { 54 for n := 0; n < b.N; n++ { 55 _ = convertNameToGraphite("name|MT{tag1}|ST[c1=v1,c2=v2,c3=v3]|MT{tag}") 56 } 57 } 58 59 func TestAdjustStep(t *testing.T) { 60 cases := []struct { 61 desc string 62 start, stop, maxPointsPerQuery, minStep int64 63 expectedOutput int64 64 }{ 65 {"TestZero", 0, 600, 0, 60, 60}, 66 {"Test10", 0, 600, 10, 10, 60}, 67 {"Test60", 0, 6000, 60, 10, 120}, 68 {"Test600", 0, 6000, 600, 10, 10}, 69 {"Test7200", 0, 60000, 10, 10, 7200}, 70 {"Test21600", 0, 120000, 10, 60, 21600}, 71 {"Test86400", 0, 120000, 1, 60, 86400}, 72 } 73 for _, tc := range cases { 74 t.Run(tc.desc, func(t *testing.T) { 75 output := adjustStep(tc.start, tc.stop, tc.maxPointsPerQuery, tc.minStep) 76 if output != tc.expectedOutput { 77 t.Fatalf("%s: expected value: %d got: %d for input: %d %d %d %d", 78 tc.desc, tc.expectedOutput, output, tc.start, tc.stop, tc.maxPointsPerQuery, tc.minStep) 79 } 80 }) 81 } 82 }