aboutsummaryrefslogtreecommitdiff
path: root/hotline/transaction_test.go
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2022-07-03 15:36:08 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2022-07-03 15:36:08 -0700
commit854a92fc2755ace61c405df335ddf69b02a3d932 (patch)
treef8f5dc40e4d8f534358e72f62d5b67ead359d526 /hotline/transaction_test.go
parenta9bdccb79164a787b4f1a2c1579a95cf751aef40 (diff)
Implement io.Writer interface for Transaction
Diffstat (limited to 'hotline/transaction_test.go')
-rw-r--r--hotline/transaction_test.go151
1 files changed, 77 insertions, 74 deletions
diff --git a/hotline/transaction_test.go b/hotline/transaction_test.go
index 04bcde0..4b4f183 100644
--- a/hotline/transaction_test.go
+++ b/hotline/transaction_test.go
@@ -111,80 +111,6 @@ func TestReadFields(t *testing.T) {
}
}
-func TestReadTransaction(t *testing.T) {
- sampleTransaction := &Transaction{
- Flags: byte(0),
- IsReply: byte(0),
- Type: []byte{0x000, 0x93},
- ID: []byte{0x000, 0x00, 0x00, 0x01},
- ErrorCode: []byte{0x000, 0x00, 0x00, 0x00},
- TotalSize: []byte{0x000, 0x00, 0x00, 0x08},
- DataSize: []byte{0x000, 0x00, 0x00, 0x08},
- ParamCount: []byte{0x00, 0x01},
- Fields: []Field{
- {
- ID: []byte{0x00, 0x01},
- FieldSize: []byte{0x00, 0x02},
- Data: []byte{0xff, 0xff},
- },
- },
- }
-
- type args struct {
- buf []byte
- }
- tests := []struct {
- name string
- args args
- want *Transaction
- want1 int
- wantErr bool
- }{
- {
- name: "when buf contains all bytes for a single transaction",
- args: args{
- buf: func() []byte {
- b, _ := sampleTransaction.MarshalBinary()
- return b
- }(),
- },
- want: sampleTransaction,
- want1: func() int {
- b, _ := sampleTransaction.MarshalBinary()
- return len(b)
- }(),
- wantErr: false,
- },
- {
- name: "when len(buf) is less than the length of the transaction",
- args: args{
- buf: func() []byte {
- b, _ := sampleTransaction.MarshalBinary()
- return b[:len(b)-1]
- }(),
- },
- want: nil,
- want1: 0,
- wantErr: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, got1, err := ReadTransaction(tt.args.buf)
- if (err != nil) != tt.wantErr {
- t.Errorf("ReadTransaction() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !assert.Equal(t, tt.want, got) {
- t.Errorf("ReadTransaction() got = %v, want %v", got, tt.want)
- }
- if got1 != tt.want1 {
- t.Errorf("ReadTransaction() got1 = %v, want %v", got1, tt.want1)
- }
- })
- }
-}
-
func Test_transactionScanner(t *testing.T) {
type args struct {
data []byte
@@ -372,3 +298,80 @@ func Test_transactionScanner(t *testing.T) {
})
}
}
+
+func TestTransaction_Write(t1 *testing.T) {
+ sampleTransaction := &Transaction{
+ Flags: byte(0),
+ IsReply: byte(0),
+ Type: []byte{0x000, 0x93},
+ ID: []byte{0x000, 0x00, 0x00, 0x01},
+ ErrorCode: []byte{0x000, 0x00, 0x00, 0x00},
+ TotalSize: []byte{0x000, 0x00, 0x00, 0x08},
+ DataSize: []byte{0x000, 0x00, 0x00, 0x08},
+ ParamCount: []byte{0x00, 0x01},
+ Fields: []Field{
+ {
+ ID: []byte{0x00, 0x01},
+ FieldSize: []byte{0x00, 0x02},
+ Data: []byte{0xff, 0xff},
+ },
+ },
+ }
+
+ type fields struct {
+ clientID *[]byte
+ Flags byte
+ IsReply byte
+ Type []byte
+ ID []byte
+ ErrorCode []byte
+ TotalSize []byte
+ DataSize []byte
+ ParamCount []byte
+ Fields []Field
+ }
+ type args struct {
+ p []byte
+ }
+ tests := []struct {
+ name string
+ fields fields
+ args args
+ wantN int
+ wantErr assert.ErrorAssertionFunc
+ }{
+ {
+ name: "when buf contains all bytes for a single transaction",
+ fields: fields{},
+ args: args{
+ p: func() []byte {
+ b, _ := sampleTransaction.MarshalBinary()
+ return b
+ }(),
+ },
+ wantN: 28,
+ wantErr: assert.NoError,
+ },
+ }
+ 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,
+ }
+ gotN, err := t.Write(tt.args.p)
+ if !tt.wantErr(t1, err, fmt.Sprintf("Write(%v)", tt.args.p)) {
+ return
+ }
+ assert.Equalf(t1, tt.wantN, gotN, "Write(%v)", tt.args.p)
+ })
+ }
+}