github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/server/conn_stmt_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package server 15 16 import ( 17 . "github.com/whtcorpsinc/check" 18 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 19 "github.com/whtcorpsinc/BerolinaSQL/terror" 20 "github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx" 21 "github.com/whtcorpsinc/milevadb/types" 22 ) 23 24 func (ts *ConnTestSuite) TestParseInterDircArgs(c *C) { 25 type args struct { 26 args []types.Causet 27 boundParams [][]byte 28 nullBitmap []byte 29 paramTypes []byte 30 paramValues []byte 31 } 32 tests := []struct { 33 args args 34 err error 35 expect interface{} 36 }{ 37 // Tests for int overflow 38 { 39 args{ 40 make([]types.Causet, 1), 41 [][]byte{nil}, 42 []byte{0x0}, 43 []byte{1, 0}, 44 []byte{0xff}, 45 }, 46 nil, 47 int64(-1), 48 }, 49 { 50 args{ 51 make([]types.Causet, 1), 52 [][]byte{nil}, 53 []byte{0x0}, 54 []byte{2, 0}, 55 []byte{0xff, 0xff}, 56 }, 57 nil, 58 int64(-1), 59 }, 60 { 61 args{ 62 make([]types.Causet, 1), 63 [][]byte{nil}, 64 []byte{0x0}, 65 []byte{3, 0}, 66 []byte{0xff, 0xff, 0xff, 0xff}, 67 }, 68 nil, 69 int64(-1), 70 }, 71 // Tests for date/datetime/timestamp 72 { 73 args{ 74 make([]types.Causet, 1), 75 [][]byte{nil}, 76 []byte{0x0}, 77 []byte{12, 0}, 78 []byte{0x0b, 0xda, 0x07, 0x0a, 0x11, 0x13, 0x1b, 0x1e, 0x01, 0x00, 0x00, 0x00}, 79 }, 80 nil, 81 "2010-10-17 19:27:30.000001", 82 }, 83 { 84 args{ 85 make([]types.Causet, 1), 86 [][]byte{nil}, 87 []byte{0x0}, 88 []byte{10, 0}, 89 []byte{0x04, 0xda, 0x07, 0x0a, 0x11}, 90 }, 91 nil, 92 "2010-10-17", 93 }, 94 { 95 args{ 96 make([]types.Causet, 1), 97 [][]byte{nil}, 98 []byte{0x0}, 99 []byte{7, 0}, 100 []byte{0x0b, 0xda, 0x07, 0x0a, 0x11, 0x13, 0x1b, 0x1e, 0x01, 0x00, 0x00, 0x00}, 101 }, 102 nil, 103 "2010-10-17 19:27:30.000001", 104 }, 105 { 106 args{ 107 make([]types.Causet, 1), 108 [][]byte{nil}, 109 []byte{0x0}, 110 []byte{7, 0}, 111 []byte{0x07, 0xda, 0x07, 0x0a, 0x11, 0x13, 0x1b, 0x1e}, 112 }, 113 nil, 114 "2010-10-17 19:27:30", 115 }, 116 { 117 args{ 118 make([]types.Causet, 1), 119 [][]byte{nil}, 120 []byte{0x0}, 121 []byte{7, 0}, 122 []byte{0x00}, 123 }, 124 nil, 125 types.ZeroDatetimeStr, 126 }, 127 // Tests for time 128 { 129 args{ 130 make([]types.Causet, 1), 131 [][]byte{nil}, 132 []byte{0x0}, 133 []byte{11, 0}, 134 []byte{0x0c, 0x01, 0x78, 0x00, 0x00, 0x00, 0x13, 0x1b, 0x1e, 0x01, 0x00, 0x00, 0x00}, 135 }, 136 nil, 137 "-120 19:27:30.000001", 138 }, 139 { 140 args{ 141 make([]types.Causet, 1), 142 [][]byte{nil}, 143 []byte{0x0}, 144 []byte{11, 0}, 145 []byte{0x08, 0x01, 0x78, 0x00, 0x00, 0x00, 0x13, 0x1b, 0x1e}, 146 }, 147 nil, 148 "-120 19:27:30", 149 }, 150 { 151 args{ 152 make([]types.Causet, 1), 153 [][]byte{nil}, 154 []byte{0x0}, 155 []byte{11, 0}, 156 []byte{0x00}, 157 }, 158 nil, 159 "0", 160 }, 161 // For error test 162 { 163 args{ 164 make([]types.Causet, 1), 165 [][]byte{nil}, 166 []byte{0x0}, 167 []byte{7, 0}, 168 []byte{10}, 169 }, 170 allegrosql.ErrMalformPacket, 171 nil, 172 }, 173 { 174 args{ 175 make([]types.Causet, 1), 176 [][]byte{nil}, 177 []byte{0x0}, 178 []byte{11, 0}, 179 []byte{10}, 180 }, 181 allegrosql.ErrMalformPacket, 182 nil, 183 }, 184 { 185 args{ 186 make([]types.Causet, 1), 187 [][]byte{nil}, 188 []byte{0x0}, 189 []byte{11, 0}, 190 []byte{8, 2}, 191 }, 192 allegrosql.ErrMalformPacket, 193 nil, 194 }, 195 } 196 for _, tt := range tests { 197 err := parseInterDircArgs(&stmtctx.StatementContext{}, tt.args.args, tt.args.boundParams, tt.args.nullBitmap, tt.args.paramTypes, tt.args.paramValues) 198 c.Assert(terror.ErrorEqual(err, tt.err), IsTrue, Commentf("err %v", err)) 199 c.Assert(tt.args.args[0].GetValue(), Equals, tt.expect) 200 } 201 } 202 203 func (ts *ConnTestSuite) TestParseStmtFetchCmd(c *C) { 204 tests := []struct { 205 arg []byte 206 stmtID uint32 207 fetchSize uint32 208 err error 209 }{ 210 {[]byte{3, 0, 0, 0, 50, 0, 0, 0}, 3, 50, nil}, 211 {[]byte{5, 0, 0, 0, 232, 3, 0, 0}, 5, 1000, nil}, 212 {[]byte{5, 0, 0, 0, 0, 8, 0, 0}, 5, maxFetchSize, nil}, 213 {[]byte{5, 0, 0}, 0, 0, allegrosql.ErrMalformPacket}, 214 {[]byte{1, 0, 0, 0, 3, 2, 0, 0, 3, 5, 6}, 0, 0, allegrosql.ErrMalformPacket}, 215 {[]byte{}, 0, 0, allegrosql.ErrMalformPacket}, 216 } 217 218 for _, t := range tests { 219 stmtID, fetchSize, err := parseStmtFetchCmd(t.arg) 220 c.Assert(stmtID, Equals, t.stmtID) 221 c.Assert(fetchSize, Equals, t.fetchSize) 222 c.Assert(err, Equals, t.err) 223 } 224 }