|
17 | 17 | package common |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "database/sql/driver" |
20 | 21 | "encoding/json" |
21 | | - |
22 | 22 | "math/big" |
| 23 | + "reflect" |
23 | 24 | "strings" |
24 | 25 | "testing" |
25 | 26 | ) |
@@ -193,3 +194,180 @@ func TestMixedcaseAccount_Address(t *testing.T) { |
193 | 194 | } |
194 | 195 |
|
195 | 196 | } |
| 197 | + |
| 198 | +func TestHash_Scan(t *testing.T) { |
| 199 | + type args struct { |
| 200 | + src interface{} |
| 201 | + } |
| 202 | + tests := []struct { |
| 203 | + name string |
| 204 | + args args |
| 205 | + wantErr bool |
| 206 | + }{ |
| 207 | + { |
| 208 | + name: "working scan", |
| 209 | + args: args{src: []byte{ |
| 210 | + 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, |
| 211 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, |
| 212 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, |
| 213 | + 0x10, 0x00, |
| 214 | + }}, |
| 215 | + wantErr: false, |
| 216 | + }, |
| 217 | + { |
| 218 | + name: "non working scan", |
| 219 | + args: args{src: int64(1234567890)}, |
| 220 | + wantErr: true, |
| 221 | + }, |
| 222 | + { |
| 223 | + name: "invalid length scan", |
| 224 | + args: args{src: []byte{ |
| 225 | + 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, |
| 226 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, |
| 227 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, |
| 228 | + }}, |
| 229 | + wantErr: true, |
| 230 | + }, |
| 231 | + } |
| 232 | + for _, tt := range tests { |
| 233 | + t.Run(tt.name, func(t *testing.T) { |
| 234 | + h := &Hash{} |
| 235 | + if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr { |
| 236 | + t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr) |
| 237 | + } |
| 238 | + |
| 239 | + if !tt.wantErr { |
| 240 | + for i := range h { |
| 241 | + if h[i] != tt.args.src.([]byte)[i] { |
| 242 | + t.Errorf( |
| 243 | + "Hash.Scan() didn't scan the %d src correctly (have %X, want %X)", |
| 244 | + i, h[i], tt.args.src.([]byte)[i], |
| 245 | + ) |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + }) |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +func TestHash_Value(t *testing.T) { |
| 254 | + b := []byte{ |
| 255 | + 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, |
| 256 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, |
| 257 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, |
| 258 | + 0x10, 0x00, |
| 259 | + } |
| 260 | + var usedH Hash |
| 261 | + usedH.SetBytes(b) |
| 262 | + tests := []struct { |
| 263 | + name string |
| 264 | + h Hash |
| 265 | + want driver.Value |
| 266 | + wantErr bool |
| 267 | + }{ |
| 268 | + { |
| 269 | + name: "Working value", |
| 270 | + h: usedH, |
| 271 | + want: b, |
| 272 | + wantErr: false, |
| 273 | + }, |
| 274 | + } |
| 275 | + for _, tt := range tests { |
| 276 | + t.Run(tt.name, func(t *testing.T) { |
| 277 | + got, err := tt.h.Value() |
| 278 | + if (err != nil) != tt.wantErr { |
| 279 | + t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr) |
| 280 | + return |
| 281 | + } |
| 282 | + if !reflect.DeepEqual(got, tt.want) { |
| 283 | + t.Errorf("Hash.Value() = %v, want %v", got, tt.want) |
| 284 | + } |
| 285 | + }) |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +func TestAddress_Scan(t *testing.T) { |
| 290 | + type args struct { |
| 291 | + src interface{} |
| 292 | + } |
| 293 | + tests := []struct { |
| 294 | + name string |
| 295 | + args args |
| 296 | + wantErr bool |
| 297 | + }{ |
| 298 | + { |
| 299 | + name: "working scan", |
| 300 | + args: args{src: []byte{ |
| 301 | + 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, |
| 302 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, |
| 303 | + }}, |
| 304 | + wantErr: false, |
| 305 | + }, |
| 306 | + { |
| 307 | + name: "non working scan", |
| 308 | + args: args{src: int64(1234567890)}, |
| 309 | + wantErr: true, |
| 310 | + }, |
| 311 | + { |
| 312 | + name: "invalid length scan", |
| 313 | + args: args{src: []byte{ |
| 314 | + 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, |
| 315 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, |
| 316 | + }}, |
| 317 | + wantErr: true, |
| 318 | + }, |
| 319 | + } |
| 320 | + for _, tt := range tests { |
| 321 | + t.Run(tt.name, func(t *testing.T) { |
| 322 | + a := &Address{} |
| 323 | + if err := a.Scan(tt.args.src); (err != nil) != tt.wantErr { |
| 324 | + t.Errorf("Address.Scan() error = %v, wantErr %v", err, tt.wantErr) |
| 325 | + } |
| 326 | + |
| 327 | + if !tt.wantErr { |
| 328 | + for i := range a { |
| 329 | + if a[i] != tt.args.src.([]byte)[i] { |
| 330 | + t.Errorf( |
| 331 | + "Address.Scan() didn't scan the %d src correctly (have %X, want %X)", |
| 332 | + i, a[i], tt.args.src.([]byte)[i], |
| 333 | + ) |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | + }) |
| 338 | + } |
| 339 | +} |
| 340 | + |
| 341 | +func TestAddress_Value(t *testing.T) { |
| 342 | + b := []byte{ |
| 343 | + 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, |
| 344 | + 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, |
| 345 | + } |
| 346 | + var usedA Address |
| 347 | + usedA.SetBytes(b) |
| 348 | + tests := []struct { |
| 349 | + name string |
| 350 | + a Address |
| 351 | + want driver.Value |
| 352 | + wantErr bool |
| 353 | + }{ |
| 354 | + { |
| 355 | + name: "Working value", |
| 356 | + a: usedA, |
| 357 | + want: b, |
| 358 | + wantErr: false, |
| 359 | + }, |
| 360 | + } |
| 361 | + for _, tt := range tests { |
| 362 | + t.Run(tt.name, func(t *testing.T) { |
| 363 | + got, err := tt.a.Value() |
| 364 | + if (err != nil) != tt.wantErr { |
| 365 | + t.Errorf("Address.Value() error = %v, wantErr %v", err, tt.wantErr) |
| 366 | + return |
| 367 | + } |
| 368 | + if !reflect.DeepEqual(got, tt.want) { |
| 369 | + t.Errorf("Address.Value() = %v, want %v", got, tt.want) |
| 370 | + } |
| 371 | + }) |
| 372 | + } |
| 373 | +} |
0 commit comments