github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/sql_wrappers_test.go (about) 1 // (c) Copyright IBM Corp. 2023 2 3 package instana 4 5 import ( 6 "context" 7 "database/sql/driver" 8 "fmt" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func Test_connAlreadyWrapped(t *testing.T) { 15 type args struct { 16 conn driver.Conn 17 } 18 var tests []struct { 19 name string 20 args args 21 want bool 22 } 23 24 pCons := []driver.Conn{ 25 &wConn{}, &w_conn_Execer_ExecerContext_Queryer_QueryerContext_ConnPrepareContext_NamedValueChecker{}, &w_conn_Execer_ExecerContext_Queryer_QueryerContext_ConnPrepareContext{}, 26 } 27 cons := []driver.Conn{ 28 w_conn_Execer_ExecerContext_Queryer_QueryerContext_ConnPrepareContext_NamedValueChecker{}, w_conn_Execer_ExecerContext_Queryer_QueryerContext_ConnPrepareContext{}, 29 } 30 31 for _, v := range pCons { 32 tests = append(tests, struct { 33 name string 34 args args 35 want bool 36 }{ 37 name: fmt.Sprintf("%T", v), 38 args: args{ 39 v, 40 }, 41 want: true, 42 }) 43 } 44 45 for _, v := range cons { 46 tests = append(tests, struct { 47 name string 48 args args 49 want bool 50 }{ 51 name: fmt.Sprintf("%T", v), 52 args: args{ 53 v, 54 }, 55 want: true, 56 }) 57 } 58 59 for _, tt := range tests { 60 t.Run(tt.name, func(t *testing.T) { 61 assert.Equalf(t, tt.want, connAlreadyWrapped(tt.args.conn), "connAlreadyWrapped(%v)", tt.args.conn) 62 }) 63 } 64 } 65 66 func Test_connAlreadyWrappedFalse(t *testing.T) { 67 var a driver.Conn 68 assert.False(t, connAlreadyWrapped(a), "connAlreadyWrapped(%v)") 69 } 70 71 func Test_stmtAlreadyWrapped(t *testing.T) { 72 type args struct { 73 conn driver.Stmt 74 } 75 var tests []struct { 76 name string 77 args args 78 want bool 79 } 80 81 pStmts := []driver.Stmt{ 82 &wStmt{}, &w_stmt_StmtExecContext_StmtQueryContext_NamedValueChecker_ColumnConverter{}, &w_stmt_StmtExecContext_StmtQueryContext_NamedValueChecker{}, 83 } 84 stmt := []driver.Stmt{w_stmt_StmtExecContext_StmtQueryContext_NamedValueChecker_ColumnConverter{}, w_stmt_StmtExecContext_StmtQueryContext_NamedValueChecker{}} 85 86 for _, v := range pStmts { 87 tests = append(tests, struct { 88 name string 89 args args 90 want bool 91 }{ 92 name: fmt.Sprintf("%T", v), 93 args: args{ 94 v, 95 }, 96 want: true, 97 }) 98 } 99 100 for _, v := range stmt { 101 tests = append(tests, struct { 102 name string 103 args args 104 want bool 105 }{ 106 name: fmt.Sprintf("%T", v), 107 args: args{ 108 v, 109 }, 110 want: true, 111 }) 112 } 113 114 for _, tt := range tests { 115 t.Run(tt.name, func(t *testing.T) { 116 assert.Equalf(t, tt.want, stmtAlreadyWrapped(tt.args.conn), "stmtAlreadyWrapped(%v)", tt.args.conn) 117 }) 118 } 119 } 120 121 func Test_stmtAlreadyWrappedFalse(t *testing.T) { 122 var a driver.Stmt 123 assert.False(t, stmtAlreadyWrapped(a), "stmtAlreadyWrapped(%v)") 124 } 125 126 func Test_stmtWrap(t *testing.T) { 127 type args struct { 128 stmt driver.Stmt 129 } 130 var tests []struct { 131 name string 132 args args 133 want interface{} 134 } 135 136 var stmts []driver.Stmt 137 for _, f := range _stmt_n { 138 stmts = append(stmts, f(nil, "", DbConnDetails{}, nil, nil, nil, nil, nil)) 139 } 140 141 for _, v := range stmts { 142 tests = append(tests, struct { 143 name string 144 args args 145 want interface{} 146 }{ 147 name: fmt.Sprintf("%T", v), 148 args: args{ 149 v, 150 }, 151 want: v, 152 }) 153 } 154 155 for _, tt := range tests { 156 t.Run(tt.name, func(t *testing.T) { 157 assert.IsType(t, tt.want, wrapStmt(tt.args.stmt, "", DbConnDetails{}, nil), tt.name) 158 }) 159 } 160 } 161 162 func Test_connWrap(t *testing.T) { 163 type args struct { 164 conn driver.Conn 165 } 166 var tests []struct { 167 name string 168 args args 169 want interface{} 170 } 171 172 var conns []driver.Conn 173 for _, f := range _conn_n { 174 conns = append(conns, f(DbConnDetails{}, nil, nil, nil, nil, nil, nil, nil, nil)) 175 } 176 177 for _, v := range conns { 178 tests = append(tests, struct { 179 name string 180 args args 181 want interface{} 182 }{ 183 name: fmt.Sprintf("%T", v), 184 args: args{ 185 v, 186 }, 187 want: v, 188 }) 189 } 190 191 for _, tt := range tests { 192 t.Run(tt.name, func(t *testing.T) { 193 assert.IsType(t, tt.want, wrapConn(DbConnDetails{}, tt.args.conn, nil), tt.name) 194 }) 195 } 196 } 197 198 type stmtAllInterfacesMock struct { 199 } 200 201 func (s *stmtAllInterfacesMock) CheckNamedValue(value *driver.NamedValue) error { 202 return nil 203 } 204 205 func (s *stmtAllInterfacesMock) ColumnConverter(idx int) driver.ValueConverter { 206 return nil 207 } 208 209 func (s *stmtAllInterfacesMock) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { 210 return nil, nil 211 } 212 213 func (s *stmtAllInterfacesMock) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { 214 return nil, nil 215 } 216 217 func (s *stmtAllInterfacesMock) Close() error { 218 return nil 219 } 220 221 func (s *stmtAllInterfacesMock) NumInput() int { 222 return 0 223 } 224 225 func (s *stmtAllInterfacesMock) Exec(args []driver.Value) (driver.Result, error) { 226 return nil, nil 227 } 228 229 func (s *stmtAllInterfacesMock) Query(args []driver.Value) (driver.Rows, error) { 230 return nil, nil 231 } 232 233 func Test_stmtAllInterfacesCase(t *testing.T) { 234 mock := &stmtAllInterfacesMock{} 235 236 d := wrapStmt(mock, "", DbConnDetails{}, nil) 237 238 assert.IsType(t, &w_stmt_StmtExecContext_StmtQueryContext_NamedValueChecker_ColumnConverter{}, d) 239 } 240 241 func Test_stmtWrapOnlyStmt(t *testing.T) { 242 var a driver.Stmt 243 244 d := wrapStmt(a, "", DbConnDetails{}, nil) 245 246 assert.IsType(t, &wStmt{}, d) 247 } 248 249 func Test_connWrapOnlyConn(t *testing.T) { 250 var a driver.Conn 251 252 d := wrapConn(DbConnDetails{}, a, nil) 253 254 assert.IsType(t, &wConn{}, d) 255 } 256 257 type connAllInterfacesMock struct { 258 } 259 260 func (c *connAllInterfacesMock) CheckNamedValue(value *driver.NamedValue) error { 261 return nil 262 } 263 264 func (c *connAllInterfacesMock) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { 265 return nil, nil 266 } 267 268 func (c *connAllInterfacesMock) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { 269 return nil, nil 270 } 271 272 func (c *connAllInterfacesMock) Query(query string, args []driver.Value) (driver.Rows, error) { 273 return nil, nil 274 } 275 276 func (c *connAllInterfacesMock) Exec(query string, args []driver.Value) (driver.Result, error) { 277 return nil, nil 278 } 279 280 func (c *connAllInterfacesMock) Prepare(query string) (driver.Stmt, error) { 281 return nil, nil 282 } 283 284 func (c *connAllInterfacesMock) Close() error { 285 return nil 286 } 287 288 func (c *connAllInterfacesMock) Begin() (driver.Tx, error) { 289 return nil, nil 290 } 291 292 func Test_connAllInterfacesCase(t *testing.T) { 293 mock := &connAllInterfacesMock{} 294 295 d := wrapConn(DbConnDetails{}, mock, nil) 296 297 assert.IsType(t, &w_conn_Execer_ExecerContext_Queryer_QueryerContext_NamedValueChecker{}, d) 298 } 299 300 func Test_convertBooleansToInt(t *testing.T) { 301 type args struct { 302 args []bool 303 } 304 tests := []struct { 305 name string 306 args args 307 want int 308 }{ 309 { 310 name: "true true true false", 311 args: args{args: []bool{true, true, true, false}}, 312 want: 0b1110, 313 }, 314 { 315 name: "true true true true", 316 args: args{args: []bool{true, true, true, true}}, 317 want: 0b1111, 318 }, 319 { 320 name: "false true false", 321 args: args{args: []bool{false, true, false}}, 322 want: 0b10, 323 }, 324 } 325 for _, tt := range tests { 326 t.Run(tt.name, func(t *testing.T) { 327 assert.Equalf(t, tt.want, convertBooleansToInt(tt.args.args...), "convertBooleansToInt(%v)") 328 }) 329 } 330 }