github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_group_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/cliutils/point" 14 tu "github.com/GuanceCloud/cliutils/testutil" 15 ) 16 17 func TestGroup(t *testing.T) { 18 cases := []struct { 19 name, pl, in string 20 expected interface{} 21 fail bool 22 outkey string 23 }{ 24 { 25 name: "normal", 26 pl: `json(_, status) 27 group_between(status, [200, 400], false, newkey)`, 28 in: `{"status": 200,"age":47}`, 29 expected: false, 30 outkey: "newkey", 31 }, 32 { 33 name: "normal", 34 pl: `json(_, status) 35 group_between(status, [200, 400], 10, newkey)`, 36 in: `{"status": 200,"age":47}`, 37 expected: int64(10), 38 outkey: "newkey", 39 }, 40 { 41 name: "normal", 42 pl: `json(_, status) 43 group_between(status, [200, 400], "ok", newkey)`, 44 in: `{"status": 200,"age":47}`, 45 expected: "ok", 46 outkey: "newkey", 47 }, 48 { 49 name: "normal", 50 pl: `json(_, status) 51 group_between(status, [200, 299], "ok")`, 52 in: `{"status": 200,"age":47}`, 53 expected: "ok", 54 outkey: "status", 55 }, 56 { 57 name: "normal", 58 pl: `json(_, status) 59 group_between(status, [200, 299], "ok", newkey)`, 60 in: `{"status": 200,"age":47}`, 61 expected: "ok", 62 outkey: "newkey", 63 }, 64 { 65 name: "normal", 66 pl: `json(_, status) 67 group_between(status, [300, 400], "ok", newkey)`, 68 in: `{"status": 200,"age":47}`, 69 expected: nil, 70 outkey: "newkey", 71 }, 72 } 73 74 for idx, tc := range cases { 75 t.Run(tc.name, func(t *testing.T) { 76 runner, err := NewTestingRunner(tc.pl) 77 if err != nil { 78 if tc.fail { 79 t.Logf("[%d]expect error: %s", idx, err) 80 } else { 81 t.Errorf("[%d] failed: %s", idx, err) 82 } 83 return 84 } 85 86 pt := ptinput.NewPlPoint( 87 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 88 errR := runScript(runner, pt) 89 90 if errR != nil { 91 t.Fatal(errR.Error()) 92 } 93 94 if tc.fail { 95 t.Logf("[%d]expect error: %s", idx, errR.Error()) 96 } 97 v, _, _ := pt.Get(tc.outkey) 98 tu.Equals(t, tc.expected, v) 99 t.Logf("[%d] PASS", idx) 100 }) 101 } 102 }