package common import ( "database/sql/driver" "reflect" "testing" ) func TestBytesConversion(t *testing.T) { bytes := []byte{5} hash := BytesToHash(bytes) var exp Hash exp[31] = 5 if hash != exp { t.Errorf("expected %x got %x", exp, hash) } } func TestHash_Scan(t *testing.T) { type args struct { src interface{} } tests := []struct { name string args args wantErr bool }{ { name: "working scan", args: args{src: []byte{ 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 0x10, 0x00, }}, wantErr: false, }, { name: "non working scan", args: args{src: int64(1234567890)}, wantErr: true, }, { name: "invalid length scan", args: args{src: []byte{ 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, }}, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { h := &Hash{} if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr { t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr) } if !tt.wantErr { for i := range h { if h[i] != tt.args.src.([]byte)[i] { t.Errorf( "Hash.Scan() didn't scan the %d src correctly (have %X, want %X)", i, h[i], tt.args.src.([]byte)[i], ) } } } }) } } func TestHash_Value(t *testing.T) { b := []byte{ 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e, 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15, 0x10, 0x00, } var usedH Hash usedH.SetBytes(b) tests := []struct { name string h Hash want driver.Value wantErr bool }{ { name: "Working value", h: usedH, want: b, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := tt.h.Value() if (err != nil) != tt.wantErr { t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("Hash.Value() = %v, want %v", got, tt.want) } }) } }