forked from ZbigniewTomanek/pgvertica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
112 lines (91 loc) · 2.48 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package pgvertica
import (
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jackc/pgtype"
)
func TestGetTypeOID(t *testing.T) {
tests := []struct {
name string
input string
expected uint32
}{
{"BOOL", "BOOL", pgtype.BoolOID},
{"INT4", "INT4", pgtype.Int8OID},
{"BYTEA", "BYTEA", pgtype.ByteaOID},
{"Invalid type", "INVALID_TYPE", pgtype.TextOID},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := getTypeOID(test.input)
if output != test.expected {
t.Errorf("expected %d, got %d", test.expected, output)
}
})
}
}
func TestGetCommandTag(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"INSERT", "INSERT INTO table VALUES (1, 'a')", "INSERT 0 1"},
{"DELETE", "DELETE FROM table WHERE id = 1", "DELETE 1"},
{"UPDATE", "UPDATE table SET name = 'b' WHERE id = 1", "UPDATE 1"},
{"SELECT", "SELECT * FROM table", "SELECT 1"},
{"Invalid command", "INVALID COMMAND", "INVALID"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := getCommandTag(test.input)
if output != test.expected {
t.Errorf("expected %s, got %s", test.expected, output)
}
})
}
}
func TestToRowDescription(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to open sqlmock database: %v", err)
}
defer db.Close()
rows := sqlmock.NewRows([]string{"id", "name", "email"}).
AddRow(1, "John", "[email protected]").
AddRow(2, "Doe", "[email protected]")
mock.ExpectQuery("SELECT").WillReturnRows(rows)
res, _ := db.Query("SELECT")
cols, _ := res.ColumnTypes()
desc := toRowDescription(cols)
if len(desc.Fields) != len(cols) {
t.Fatalf("expected %v fields, got %v", len(cols), len(desc.Fields))
}
}
func TestScanRow(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to open sqlmock database: %v", err)
}
defer db.Close()
rows := sqlmock.NewRows([]string{"id", "name", "email"}).
AddRow("1", "John", "[email protected]").
AddRow("2", "Doe", "[email protected]")
mock.ExpectQuery("SELECT").WillReturnRows(rows)
res, _ := db.Query("SELECT")
cols, _ := res.ColumnTypes()
for res.Next() {
row, err := scanRowToText(res, cols)
if err != nil {
t.Fatalf("failed to scan row: %v", err)
}
if len(row.Values) != len(cols) {
t.Fatalf("expected %v values, got %v", len(cols), len(row.Values))
}
for _, v := range row.Values {
if v == nil {
t.Fatalf("nil value in row")
}
}
}
}