1 package pgproto3
2
3 import (
4 "encoding/binary"
5 "reflect"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9 )
10
11 func TestFunctionCall_EncodeDecode(t *testing.T) {
12 type fields struct {
13 Function uint32
14 ArgFormatCodes []uint16
15 Arguments [][]byte
16 ResultFormatCode uint16
17 }
18 tests := []struct {
19 name string
20 fields fields
21 wantErr bool
22 }{
23 {"valid", fields{uint32(123), []uint16{0, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(1)}, false},
24 {"invalid format code", fields{uint32(123), []uint16{2, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(0)}, true},
25 {"invalid result format code", fields{uint32(123), []uint16{1, 1, 0, 1}, [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}, uint16(2)}, true},
26 }
27 for _, tt := range tests {
28 t.Run(tt.name, func(t *testing.T) {
29 src := &FunctionCall{
30 Function: tt.fields.Function,
31 ArgFormatCodes: tt.fields.ArgFormatCodes,
32 Arguments: tt.fields.Arguments,
33 ResultFormatCode: tt.fields.ResultFormatCode,
34 }
35 encoded, err := src.Encode([]byte{})
36 require.NoError(t, err)
37 dst := &FunctionCall{}
38
39 msgTypeCode := encoded[0]
40 if msgTypeCode != 'F' {
41 t.Errorf("msgTypeCode %v should be 'F'", msgTypeCode)
42 return
43 }
44
45 l := binary.BigEndian.Uint32(encoded[1:5])
46 if int(l) != (len(encoded) - 1) {
47 t.Errorf("Incorrect message length, got = %v, wanted = %v", l, len(encoded))
48 }
49
50 err = dst.Decode(encoded[5:])
51 if err != nil {
52 if !tt.wantErr {
53 t.Errorf("FunctionCall.Decode() error = %v, wantErr %v", err, tt.wantErr)
54 }
55 return
56 }
57
58 if !reflect.DeepEqual(src, dst) {
59 t.Error("difference after encode / decode cycle")
60 t.Errorf("src = %v", src)
61 t.Errorf("dst = %v", dst)
62 }
63 })
64 }
65 }
66
View as plain text