github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_sql_cover_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 TestSqlCover(t *testing.T) { 18 cases := []struct { 19 name string 20 outKey string 21 pl, in string 22 expected interface{} 23 fail bool 24 }{ 25 { 26 name: "normal", 27 pl: `json(_, str_msg) 28 sql_cover(str_msg)`, 29 in: `{"str_msg": "select abc from def where x > 3 and y < 5"}`, 30 outKey: "str_msg", 31 expected: "select abc from def where x > ? and y < ?", 32 fail: false, 33 }, 34 35 { 36 name: "normal", 37 pl: `json(_, str_msg) 38 sql_cover(str_msg)`, 39 in: `{"str_msg": "SELECT $func$INSERT INTO table VALUES ('a', 1, 2)$func$ FROM users"}`, 40 fail: true, 41 }, 42 { 43 name: "normal", 44 pl: `json(_, str_msg) 45 sql_cover(str_msg)`, 46 in: `{"str_msg": "SELECT Codi , Nom_CA AS Nom, Descripció_CAT AS Descripció FROM ProtValAptitud WHERE Vigent=1 ORDER BY Ordre, Codi"}`, 47 outKey: "str_msg", 48 expected: "SELECT Codi, Nom_CA, Descripció_CAT FROM ProtValAptitud WHERE Vigent = ? ORDER BY Ordre, Codi", 49 fail: false, 50 }, 51 { 52 name: "normal", 53 pl: `sql_cover("SELECT Codi , Nom_CA AS Nom, Descripció_CAT AS Descripció FROM ProtValAptitud WHERE Vigent=1 ORDER BY Ordre, Codi")`, 54 fail: true, 55 }, 56 { 57 name: "normal", 58 pl: `json(_, str_msg) 59 sql_cover(str_msg)`, 60 in: `{"str_msg": "SELECT ('/uffd')"}`, 61 outKey: "str_msg", 62 expected: "SELECT ( ? )", 63 fail: false, 64 }, 65 { 66 name: "normal", 67 pl: `sql_cover(_)`, 68 in: `select abc from def where x > 3 and y < 5 69 SELECT ( ? )`, 70 outKey: "message", 71 expected: `select abc from def where x > ? and y < ? SELECT ( ? )`, 72 fail: false, 73 }, 74 { 75 name: "normal", 76 pl: `sql_cover(_)`, 77 in: `#test 78 select abc from def where x > 3 and y < 5`, 79 outKey: "message", 80 expected: `select abc from def where x > ? and y < ?`, 81 fail: false, 82 }, 83 } 84 85 for idx, tc := range cases { 86 t.Run(tc.name, func(t *testing.T) { 87 runner, err := NewTestingRunner(tc.pl) 88 if err != nil { 89 if tc.fail { 90 t.Logf("[%d]expect error: %s", idx, err) 91 } else { 92 t.Errorf("[%d] failed: %s", idx, err) 93 } 94 return 95 } 96 97 pt := ptinput.NewPlPoint( 98 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 99 errR := runScript(runner, pt) 100 101 if errR != nil { 102 t.Fatal(errR.Error()) 103 } 104 105 if v, _, err := pt.Get(tc.outKey); err != nil { 106 if !tc.fail { 107 t.Errorf("[%d]expect error: %s", idx, errR.Error()) 108 } 109 } else { 110 tu.Equals(t, tc.expected, v) 111 t.Logf("[%d] PASS", idx) 112 } 113 }) 114 } 115 }