github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_nullif_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 TestNullIf(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(_, a.first, a_first) 27 nullif(a_first, "1")`, 28 in: `{"a":{"first": 1,"second":2,"third":"aBC","forth":true},"age":47}`, 29 expected: float64(1), 30 outkey: "a_first", 31 }, 32 { 33 name: "normal", 34 pl: `json(_, a.first, a_first) 35 nullif(a_first, 1)`, 36 in: `{"a":{"first": "1","second":2,"third":"aBC","forth":true},"age":47}`, 37 expected: "1", 38 outkey: "a_first", 39 }, 40 { 41 name: "normal", 42 pl: `json(_, a.first, a_first) 43 nullif(a_first, "")`, 44 in: `{"a":{"first": "","second":2,"third":"aBC","forth":true},"age":47}`, 45 expected: nil, 46 outkey: "a_first", 47 }, 48 { 49 name: "normal", 50 pl: `json(_, a.first, a_first) 51 nullif(a_first, nil)`, 52 in: `{"a":{"first": null,"second":2,"third":"aBC","forth":true},"age":47}`, 53 expected: nil, 54 outkey: "a_first", 55 }, 56 { 57 name: "normal", 58 pl: `json(_, a.first, a_first) 59 nullif(a_first, true)`, 60 in: `{"a":{"first": true,"second":2,"third":"aBC","forth":true},"age":47}`, 61 expected: nil, 62 outkey: "a_first", 63 }, 64 { 65 name: "normal", 66 pl: `json(_, a.first, a_first) 67 nullif(a_first, 2.3)`, 68 in: `{"a":{"first": 2.3, "second":2,"third":"aBC","forth":true},"age":47}`, 69 expected: nil, 70 outkey: "a_first", 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 pt := ptinput.NewPlPoint( 86 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 87 errR := runScript(runner, pt) 88 89 if errR != nil { 90 t.Fatal(errR.Error()) 91 } 92 93 v, _, _ := pt.Get(tc.outkey) 94 tu.Equals(t, tc.expected, v) 95 96 t.Logf("[%d] PASS", idx) 97 }) 98 } 99 }