github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_group_in_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 TestGroupIn(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_in(status, [true], "ok", "newkey")`, 28 in: `{"status": true,"age":"47"}`, 29 expected: "ok", 30 outkey: "newkey", 31 }, 32 { 33 name: "normal", 34 pl: `json(_, status) 35 group_in(status, [true], "ok", "newkey")`, 36 in: `{"status": true,"age":"47"}`, 37 expected: "ok", 38 outkey: "newkey", 39 }, 40 { 41 name: "normal", 42 pl: `json(_, status) 43 group_in(status, [true], "ok", "newkey")`, 44 in: `{"status": "aa","age":"47"}`, 45 expected: "aa", 46 outkey: "status", 47 }, 48 { 49 name: "normal", 50 pl: `json(_, status) 51 group_in(status, ["aa"], "ok", "newkey")`, 52 in: `{"status": "aa","age":"47"}`, 53 expected: "ok", 54 outkey: "newkey", 55 }, 56 { 57 name: "normal", 58 in: `{"log_level": "test","age":"47"}`, 59 pl: `json(_, log_level) 60 group_in(log_level, [200, "test"], 119)`, 61 expected: int64(119), 62 outkey: "log_level", 63 }, 64 { 65 name: "normal", 66 in: `{"log_level": "test","age":"47"}`, 67 pl: `json(_, log_level) 68 group_in(log_level, [200, "test1"], 119)`, 69 expected: "test", 70 outkey: "log_level", 71 }, 72 { 73 name: "normal", 74 in: `{"log_level": "test","age":"47"}`, 75 pl: `json(_, log_level) 76 group_in(log_level, [200, "test"], 119, "hh")`, 77 expected: int64(119), 78 outkey: "hh", 79 }, 80 } 81 82 for idx, tc := range cases { 83 t.Run(tc.name, func(t *testing.T) { 84 runner, err := NewTestingRunner(tc.pl) 85 if err != nil { 86 if tc.fail { 87 t.Logf("[%d]expect error: %s", idx, err) 88 } else { 89 t.Errorf("[%d] failed: %s", idx, err) 90 } 91 return 92 } 93 94 pt := ptinput.NewPlPoint( 95 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 96 errR := runScript(runner, pt) 97 98 if errR != nil { 99 t.Fatal(errR.Error()) 100 } 101 102 if tc.fail { 103 t.Logf("[%d]expect error: %s", idx, errR.Error()) 104 } 105 v, _, _ := pt.Get(tc.outkey) 106 tu.Equals(t, tc.expected, v) 107 t.Logf("[%d] PASS", idx) 108 }) 109 } 110 }