github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_duration_precision_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 TestDurationPrecision(t *testing.T) { 18 cases := []struct { 19 name, pl, in string 20 outkey string 21 expect interface{} 22 fail bool 23 }{ 24 { 25 name: "cast int", 26 in: `{"ts":12345}`, 27 pl: ` 28 json(_, ts) 29 cast(ts, "int") 30 duration_precision(ts, "ms", "ns") 31 `, 32 outkey: "ts", 33 expect: int64(12345000000), 34 fail: false, 35 }, 36 { 37 name: "cast int", 38 in: `{"ts":12345000}`, 39 pl: ` 40 json(_, ts) 41 cast(ts, "int") 42 duration_precision(ts, "ms", "s") 43 `, 44 outkey: "ts", 45 expect: int64(12345), 46 fail: false, 47 }, 48 { 49 name: "cast int", 50 in: `{"ts":12345000}`, 51 pl: ` 52 json(_, ts) 53 cast(ts, "int") 54 duration_precision(ts, "s", "s") 55 `, 56 outkey: "ts", 57 expect: int64(12345000), 58 fail: false, 59 }, 60 { 61 name: "cast int", 62 in: `{"ts":12345000}`, 63 pl: ` 64 json(_, ts) 65 cast(ts, "int") 66 duration_precision(ts, "ns", "us") 67 `, 68 outkey: "ts", 69 expect: int64(12345), 70 fail: false, 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) 92 } 93 94 if tc.fail { 95 t.Logf("[%d]expect error: %s", idx, err) 96 } 97 v, _, _ := pt.Get(tc.outkey) 98 tu.Equals(t, tc.expect, v) 99 t.Logf("[%d] PASS", idx) 100 }) 101 } 102 }