+
+func TestTransaction_Read(t1 *testing.T) {
+ type fields struct {
+ clientID *[]byte
+ Flags byte
+ IsReply byte
+ Type []byte
+ ID []byte
+ ErrorCode []byte
+ TotalSize []byte
+ DataSize []byte
+ ParamCount []byte
+ Fields []Field
+ readOffset int
+ }
+ type args struct {
+ p []byte
+ }
+ tests := []struct {
+ name string
+ fields fields
+ args args
+ want int
+ wantErr assert.ErrorAssertionFunc
+ wantBytes []byte
+ }{
+ {
+ name: "returns transaction bytes",
+ fields: fields{
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 0},
+ Fields: []Field{
+ NewField(FieldData, []byte("TEST")),
+ },
+ },
+ args: args{
+ p: make([]byte, 1024),
+ },
+ want: 30,
+ wantErr: assert.NoError,
+ wantBytes: []byte{0x0, 0x1, 0x0, 0x0, 0x9a, 0xcb, 0x4, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0xa, 0x0, 0x1, 0x0, 0x65, 0x0, 0x4, 0x54, 0x45, 0x53, 0x54},
+ },
+ {
+ name: "returns transaction bytes from readOffset",
+ fields: fields{
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 0},
+ Fields: []Field{
+ NewField(FieldData, []byte("TEST")),
+ },
+ readOffset: 20,
+ },
+ args: args{
+ p: make([]byte, 1024),
+ },
+ want: 10,
+ wantErr: assert.NoError,
+ wantBytes: []byte{0x0, 0x1, 0x0, 0x65, 0x0, 0x4, 0x54, 0x45, 0x53, 0x54},
+ },
+ {
+ name: "returns io.EOF when all bytes read",
+ fields: fields{
+ Flags: 0x00,
+ IsReply: 0x01,
+ Type: []byte{0, 0},
+ ID: []byte{0x9a, 0xcb, 0x04, 0x42},
+ ErrorCode: []byte{0, 0, 0, 0},
+ Fields: []Field{
+ NewField(FieldData, []byte("TEST")),
+ },
+ readOffset: 30,
+ },
+ args: args{
+ p: make([]byte, 1024),
+ },
+ want: 0,
+ wantErr: assert.Error,
+ wantBytes: []byte{},
+ },
+ }
+ for _, tt := range tests {
+ t1.Run(tt.name, func(t1 *testing.T) {
+ t := &Transaction{
+ clientID: tt.fields.clientID,
+ Flags: tt.fields.Flags,
+ IsReply: tt.fields.IsReply,
+ Type: tt.fields.Type,
+ ID: tt.fields.ID,
+ ErrorCode: tt.fields.ErrorCode,
+ TotalSize: tt.fields.TotalSize,
+ DataSize: tt.fields.DataSize,
+ ParamCount: tt.fields.ParamCount,
+ Fields: tt.fields.Fields,
+ readOffset: tt.fields.readOffset,
+ }
+ got, err := t.Read(tt.args.p)
+ if !tt.wantErr(t1, err, fmt.Sprintf("Read(%v)", tt.args.p)) {
+ return
+ }
+ assert.Equalf(t1, tt.want, got, "Read(%v)", tt.args.p)
+ assert.Equalf(t1, tt.wantBytes, tt.args.p[:got], "Read(%v)", tt.args.p)
+ })
+ }
+}