Skip to content

Commit 7a137ba

Browse files
authored
test: add unit tests for tuple data decoding logic (#87)
* test: add unit tests for tuple data decoding logic Signed-off-by: Haluk Muhsin Karakoç <haluk_karakoc@hotmail.com> * fix(test): resolve linter errors (require usage & imports) Signed-off-by: Haluk Muhsin KARAKOÇ <h.karakoc@simeranya.com.tr> Signed-off-by: Haluk Muhsin Karakoç <haluk_karakoc@hotmail.com> --------- Signed-off-by: Haluk Muhsin Karakoç <haluk_karakoc@hotmail.com> Signed-off-by: Haluk Muhsin KARAKOÇ <h.karakoc@simeranya.com.tr>
1 parent d9d6786 commit 7a137ba

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

pq/message/tuple/data_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package tuple
2+
3+
import (
4+
"encoding/binary"
5+
"testing"
6+
7+
"github.com/jackc/pgx/v5/pgtype"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestNewData(t *testing.T) {
13+
tupleDataType := uint8('D')
14+
15+
// Payload construction
16+
buf := []byte{}
17+
// Skip bytes (simulated garbage or other headers)
18+
skipBytes := []byte{0x00}
19+
buf = append(buf, skipBytes...)
20+
21+
// Tuple Data Type
22+
buf = append(buf, tupleDataType)
23+
24+
// Column count (2)
25+
colCount := make([]byte, 2)
26+
binary.BigEndian.PutUint16(colCount, 2)
27+
buf = append(buf, colCount...)
28+
29+
// Col 1: Text 't'
30+
buf = append(buf, DataTypeText)
31+
// Col 1 Length: 5 ("value")
32+
lenBuf := make([]byte, 4)
33+
binary.BigEndian.PutUint32(lenBuf, 5)
34+
buf = append(buf, lenBuf...)
35+
// Col 1 Data
36+
buf = append(buf, []byte("value")...)
37+
38+
// Col 2: Null 'n'
39+
buf = append(buf, DataTypeNull)
40+
41+
t.Run("should parse valid data", func(t *testing.T) {
42+
d, err := NewData(buf, tupleDataType, len(skipBytes))
43+
require.NoError(t, err)
44+
assert.NotNil(t, d)
45+
assert.Equal(t, uint16(2), d.ColumnNumber)
46+
assert.Len(t, d.Columns, 2)
47+
48+
assert.Equal(t, DataTypeText, d.Columns[0].DataType)
49+
assert.Equal(t, uint32(5), d.Columns[0].Length)
50+
assert.Equal(t, []byte("value"), d.Columns[0].Data)
51+
52+
assert.Equal(t, DataTypeNull, d.Columns[1].DataType)
53+
})
54+
55+
t.Run("should return error for invalid tuple data type", func(t *testing.T) {
56+
_, err := NewData(buf, 'X', len(skipBytes)) // Expecting 'X', but got 'D'
57+
require.Error(t, err)
58+
assert.Contains(t, err.Error(), "invalid tuple data type: D")
59+
})
60+
}
61+
62+
func TestData_DecodeWithColumn(t *testing.T) {
63+
tupleDataType := uint8('D')
64+
buf := []byte{}
65+
buf = append(buf, tupleDataType) // No skip bytes this time for simplicity
66+
67+
// Column count (2)
68+
colCount := make([]byte, 2)
69+
binary.BigEndian.PutUint16(colCount, 2)
70+
buf = append(buf, colCount...)
71+
72+
// Col 1: Text 't' - Int value "123"
73+
buf = append(buf, DataTypeText)
74+
lenBuf := make([]byte, 4)
75+
binary.BigEndian.PutUint32(lenBuf, 3)
76+
buf = append(buf, lenBuf...)
77+
buf = append(buf, []byte("123")...)
78+
79+
// Col 2: Null 'n'
80+
buf = append(buf, DataTypeNull)
81+
82+
d, _ := NewData(buf, tupleDataType, 0)
83+
84+
t.Run("should decode columns correctly", func(t *testing.T) {
85+
relationColumns := []RelationColumn{
86+
{Name: "id", DataType: pgtype.Int4OID},
87+
{Name: "description", DataType: pgtype.TextOID},
88+
}
89+
90+
decoded, err := d.DecodeWithColumn(relationColumns)
91+
require.NoError(t, err)
92+
assert.NotNil(t, decoded)
93+
assert.Equal(t, int32(123), decoded["id"])
94+
assert.Nil(t, decoded["description"])
95+
})
96+
97+
t.Run("should use string fallback for unknown types", func(t *testing.T) {
98+
relationColumns := []RelationColumn{
99+
{Name: "unknown_col", DataType: 999999}, // Unknown OID
100+
{Name: "description", DataType: pgtype.TextOID},
101+
}
102+
103+
decoded, err := d.DecodeWithColumn(relationColumns)
104+
require.NoError(t, err)
105+
assert.Equal(t, "123", decoded["unknown_col"]) // Fallback to string
106+
})
107+
}

0 commit comments

Comments
 (0)