github.com/kaydxh/golang@v0.0.131/pkg/database/mysql/sql_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package mysql_test 23 24 import ( 25 "fmt" 26 "testing" 27 28 mysql_ "github.com/kaydxh/golang/pkg/database/mysql" 29 ) 30 31 func TestJoinNamedColumnsValuesWithOperator(t *testing.T) { 32 testCases := []struct { 33 cmp mysql_.SqlCompare 34 oper mysql_.SqlOperator 35 cols []string 36 }{ 37 { 38 cmp: mysql_.SqlCompareLike, 39 oper: mysql_.SqlOperatorAnd, 40 cols: []string{"task_name"}, 41 }, 42 } 43 44 for _, testCase := range testCases { 45 t.Run(string(testCase.cmp), func(t *testing.T) { 46 query := mysql_.JoinNamedColumnsValuesWithOperator(testCase.cmp, testCase.oper, testCase.cols...) 47 t.Logf("sql: %v", query) 48 }) 49 } 50 } 51 52 func TestInCondition(t *testing.T) { 53 testCases := []struct { 54 cond string 55 values []string 56 }{ 57 { 58 cond: "task_id", 59 values: []string{"task_id_1", "task_id_2"}, 60 }, 61 { 62 cond: "task_id", 63 values: []string{"", ""}, 64 }, 65 } 66 67 for _, testCase := range testCases { 68 t.Run(string(testCase.cond), func(t *testing.T) { 69 query := mysql_.InCondition(testCase.cond, testCase.values...) 70 t.Logf("sql: %v", query) 71 }) 72 } 73 } 74 75 func TestNamedInCondition(t *testing.T) { 76 77 testCases := []struct { 78 cols []string 79 arg interface{} 80 }{ 81 { 82 cols: []string{"task_id"}, 83 arg: struct { 84 TaskId []string `db:"task_id"` 85 }{ 86 TaskId: []string{"task_id_1", "task_id_2"}, 87 }, 88 }, 89 { 90 cols: []string{"task_id"}, 91 arg: struct { 92 TaskId []int `db:"task_id"` 93 }{ 94 TaskId: []int{0, 1}, 95 }, 96 }, 97 } 98 99 for i, testCase := range testCases { 100 t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { 101 query, err := mysql_.NamedInCondition(mysql_.SqlOperatorAnd, testCase.cols, testCase.arg) 102 if err != nil { 103 t.Fatalf("err: %v", err) 104 } 105 t.Logf("sql: %v", query) 106 }) 107 } 108 } 109 110 func TestOrderCondition(t *testing.T) { 111 112 testCases := []struct { 113 orders map[string]bool 114 }{ 115 116 { 117 orders: map[string]bool{ 118 "task_id": true, 119 }, 120 }, 121 { 122 orders: map[string]bool{ 123 "task_id": false, 124 "score": false, 125 }, 126 }, 127 } 128 129 for i, testCase := range testCases { 130 t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { 131 query := mysql_.OrderCondition(testCase.orders) 132 t.Logf("sql: %v", query) 133 }) 134 } 135 } 136 137 func TestJoinNamedColumnsValuesBatch(t *testing.T) { 138 testCases := []struct { 139 cols []string 140 batch int 141 }{ 142 { 143 cols: []string{"task_name", "task_id", "task_arg"}, 144 batch: 4, 145 }, 146 } 147 148 for i, testCase := range testCases { 149 t.Run(fmt.Sprintf("case-%v", i), func(t *testing.T) { 150 query := mysql_.JoinNamedColumnsValuesBatch(testCase.cols, testCase.batch) 151 t.Logf("sql: %v", query) 152 }) 153 } 154 } 155 156 /* 157 func TestGenerateSQL(t *testing.T) { 158 arg := struct { 159 TaskId string `db:"task_id"` 160 Name []string `db:"name"` 161 }{ 162 TaskId: "task-id", 163 Name: []string{"name1", "name2"}, 164 } 165 //sql := "SELECT *FROM t_task where task_id=:task_id and name In(:name)" 166 sql := "SELECT *FROM t_task where task_id=:task_id" 167 query, args, err := sqlx.Named(sql, arg) 168 if err != nil { 169 t.Fatalf("falied to named, err: %v", err) 170 } 171 query, args, err = sqlx.In(query, args...) 172 if err != nil { 173 t.Fatalf("falied to In, err: %v", err) 174 } 175 176 // ns, err := d.db.PrepareNamedContext(ctx, query) 177 t.Logf("query: %v, args: %v", query, args) 178 } 179 */