-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_test.go
225 lines (198 loc) · 5.72 KB
/
raw_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package toki
import (
"database/sql"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
)
func TestRawQuery(t *testing.T) {
// Create a new mock database
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("Failed to create mock: %v", err)
}
defer db.Close()
// Test cases
tests := []struct {
name string
sql string
args []interface{}
setup func(sqlmock.Sqlmock)
validate func(*testing.T, *RawQuery)
wantErr bool
}{
{
name: "Simple SELECT query",
sql: "SELECT * FROM users WHERE id = $1",
args: []interface{}{1},
setup: func(mock sqlmock.Sqlmock) {
mock.ExpectQuery("SELECT \\* FROM users WHERE id = \\$1").
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).
AddRow(1, "zakirkun"))
},
validate: func(t *testing.T, q *RawQuery) {
rows, err := q.Query()
assert.NoError(t, err)
defer rows.Close()
var id int
var name string
assert.True(t, rows.Next())
err = rows.Scan(&id, &name)
assert.NoError(t, err)
assert.Equal(t, 1, id)
assert.Equal(t, "zakirkun", name)
},
},
{
name: "INSERT with returning",
sql: "INSERT INTO users(name, email) VALUES($1, $2) RETURNING id",
args: []interface{}{"zakirkun", "[email protected]"},
setup: func(mock sqlmock.Sqlmock) {
mock.ExpectQuery("INSERT INTO users.*RETURNING id").
WithArgs("zakirkun", "[email protected]").
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
},
validate: func(t *testing.T, q *RawQuery) {
var id int
err := q.QueryRow().Scan(&id)
assert.NoError(t, err)
assert.Equal(t, 1, id)
},
},
{
name: "UPDATE query",
sql: "UPDATE users SET last_login = $1 WHERE id = $2",
args: []interface{}{time.Now(), 1},
setup: func(mock sqlmock.Sqlmock) {
mock.ExpectExec("UPDATE users SET last_login = \\$1 WHERE id = \\$2").
WithArgs(sqlmock.AnyArg(), 1).
WillReturnResult(sqlmock.NewResult(0, 1))
},
validate: func(t *testing.T, q *RawQuery) {
result, err := q.Exec()
assert.NoError(t, err)
affected, err := result.RowsAffected()
assert.NoError(t, err)
assert.Equal(t, int64(1), affected)
},
},
{
name: "Complex query with joins",
sql: `
SELECT u.*, p.name as profile_name
FROM users u
LEFT JOIN profiles p ON p.user_id = u.id
WHERE u.created_at > $1 AND u.status = $2
`,
args: []interface{}{time.Now().Add(-24 * time.Hour), "active"},
setup: func(mock sqlmock.Sqlmock) {
mock.ExpectQuery("SELECT u.\\*, p\\.name as profile_name.*").
WithArgs(sqlmock.AnyArg(), "active").
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "profile_name"}).
AddRow(1, "zakirkun", "Developer"))
},
validate: func(t *testing.T, q *RawQuery) {
rows, err := q.Query()
assert.NoError(t, err)
defer rows.Close()
var id int
var name, profileName string
assert.True(t, rows.Next())
err = rows.Scan(&id, &name, &profileName)
assert.NoError(t, err)
assert.Equal(t, 1, id)
assert.Equal(t, "zakirkun", name)
assert.Equal(t, "Developer", profileName)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup mock expectations
tt.setup(mock)
// Create builder and execute raw query
builder := New()
query := builder.Raw(tt.sql, tt.args...).WithDB(db)
// Validate the query
tt.validate(t, query)
// Verify all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
t.Log("---- Pass ----")
})
}
}
func TestRawQueryWithTransaction(t *testing.T) {
// Create a new mock database
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("Failed to create mock: %v", err)
}
defer db.Close()
// Setup transaction expectations
mock.ExpectBegin()
mock.ExpectExec("INSERT INTO users").
WithArgs("zakirkun", "[email protected]").
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec("UPDATE profiles").
WithArgs(1).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
// Start transaction
tx, err := db.Begin()
if err != nil {
t.Fatalf("Failed to begin transaction: %v", err)
}
// Create builder
builder := New()
// Execute first query
_, err = builder.Raw("INSERT INTO users(name, email) VALUES($1, $2)",
"zakirkun", "[email protected]").
WithTx(tx).
Exec()
assert.NoError(t, err)
// Execute second query
_, err = builder.Raw("UPDATE profiles SET verified = true WHERE user_id = $1", 1).
WithTx(tx).
Exec()
assert.NoError(t, err)
// Commit transaction
err = tx.Commit()
assert.NoError(t, err)
// Verify all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
t.Log("---- Pass ----")
}
func TestRawQueryErrors(t *testing.T) {
// Create a new mock database
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("Failed to create mock: %v", err)
}
defer db.Close()
// Test query error
mock.ExpectQuery("SELECT").WillReturnError(sql.ErrNoRows)
builder := New()
rows, err := builder.Raw("SELECT * FROM users").WithDB(db).Query()
assert.Error(t, err)
assert.Equal(t, sql.ErrNoRows, err)
assert.Nil(t, rows)
// Test exec error
mock.ExpectExec("INSERT").WillReturnError(sql.ErrConnDone)
result, err := builder.Raw("INSERT INTO users(name) VALUES($1)", "zakirkun").
WithDB(db).
Exec()
assert.Error(t, err)
assert.Equal(t, sql.ErrConnDone, err)
assert.Nil(t, result)
// Verify all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
t.Log("---- Pass ----")
}