github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_parse_duration_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 TestParseDuration(t *testing.T) { 18 cases := []struct { 19 name string 20 pl, in string 21 outKey string 22 expected interface{} 23 fail bool 24 }{ 25 { 26 name: "normal", 27 pl: "json(_, `str`)\n" + 28 "parse_duration(`str`)", 29 in: `{"str": "1s"}`, 30 outKey: "str", 31 expected: int64(time.Second), 32 fail: false, 33 }, 34 35 { 36 name: "normal", 37 pl: "json(_, `str`)\n" + 38 "parse_duration(`str`)", 39 in: `{"str": "1ms"}`, 40 outKey: "str", 41 expected: int64(time.Millisecond), 42 fail: false, 43 }, 44 45 { 46 name: "normal", 47 pl: "json(_, `str`)\n" + 48 "parse_duration(`str`)", 49 in: `{"str": "1us"}`, 50 outKey: "str", 51 expected: int64(time.Microsecond), 52 fail: false, 53 }, 54 55 { 56 name: "normal", 57 pl: "json(_, `str`)\n" + 58 "parse_duration(`str`)", 59 in: `{"str": "1µs"}`, 60 outKey: "str", 61 expected: int64(time.Microsecond), 62 fail: false, 63 }, 64 65 { 66 name: "normal", 67 pl: "json(_, `str`)\n" + 68 "parse_duration(`str`)", 69 in: `{"str": "1m"}`, 70 outKey: "str", 71 expected: int64(time.Minute), 72 fail: false, 73 }, 74 75 { 76 name: "normal", 77 pl: "json(_, `str`)\n" + 78 "parse_duration(`str`)", 79 in: `{"str": "1h"}`, 80 outKey: "str", 81 expected: int64(time.Hour), 82 fail: false, 83 }, 84 85 { 86 name: "normal", 87 pl: "json(_, `str`)\n" + 88 "parse_duration(`str`)", 89 in: `{"str": "-23h"}`, 90 outKey: "str", 91 expected: -23 * int64(time.Hour), 92 fail: false, 93 }, 94 95 { 96 name: "normal", 97 pl: "json(_, `str`)\n" + 98 "parse_duration(`str`)", 99 in: `{"str": "-23ns"}`, 100 outKey: "str", 101 expected: int64(-23), 102 fail: false, 103 }, 104 105 { 106 name: "normal", 107 pl: "json(_, `str`)\n" + 108 "parse_duration(`str`)", 109 in: `{"str": "-2.3s"}`, 110 outKey: "str", 111 expected: int64(time.Second*-2 - 300*time.Millisecond), 112 fail: false, 113 }, 114 115 { 116 name: "invalid input string", 117 pl: "json(_, `str`)\n" + 118 "parse_duration(`str`)", 119 in: `{"str": "1uuus"}`, 120 outKey: "str", 121 expected: "1uuus", 122 fail: false, 123 }, 124 125 { 126 name: "too many args", 127 pl: "json(_, `str`)\n" + 128 "parse_duration(`str`)", 129 in: `{"str": "1uuus"}`, 130 fail: true, 131 }, 132 133 { 134 name: "invalid input type", 135 pl: "json(_, `str`)\n" + 136 "parse_duration(`str`)", 137 in: `{"str": 1}`, 138 fail: true, 139 }, 140 } 141 142 for idx, tc := range cases { 143 t.Run(tc.name, func(t *testing.T) { 144 runner, err := NewTestingRunner(tc.pl) 145 if err != nil { 146 if tc.fail { 147 t.Logf("[%d]expect error: %s", idx, err) 148 } else { 149 t.Errorf("[%d] failed: %s", idx, err) 150 } 151 return 152 } 153 154 pt := ptinput.NewPlPoint( 155 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 156 errR := runScript(runner, pt) 157 158 if errR != nil { 159 t.Fatal(errR) 160 } 161 162 if v, _, err := pt.Get(tc.outKey); err != nil { 163 if !tc.fail { 164 t.Errorf("[%d]expect error: %s", idx, err) 165 } 166 } else { 167 tu.Equals(t, tc.expected, v) 168 } 169 }) 170 } 171 }