-
Notifications
You must be signed in to change notification settings - Fork 1
/
goql_test.go
205 lines (183 loc) · 5.04 KB
/
goql_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
package goql
import (
"database/sql"
"fmt"
"strings"
"testing"
_ "github.com/mattn/go-sqlite3"
)
type User struct {
ID int64 `db:"id" pk:"true"`
Username string `db:"username"`
Password string `db:"password"`
Email string
Total string `db:"total" sql:"COUNT(col)"`
}
func dbSetup() *sql.DB {
Testing = true
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
fmt.Printf("%s", err)
}
db.Exec(`
CREATE TABLE user(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username CHAR(255),
password CHAR(255)
)`)
return db
}
func TestSelectWhenPassedString(t *testing.T) {
expected := `SELECT id FROM mytable`
qb := QueryBuilder{}
qb.IgnoreDynamic = true
qb.Select("id").From("mytable")
qb.Build()
if strings.Trim(qb.Sql, " ") != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, qb.Sql)
}
}
func TestSelectWithStructWhenUsingDbTag(t *testing.T) {
expected := `SELECT "id","username","password","total" FROM users`
user := User{}
qb := QueryBuilder{}
qb.IgnoreDynamic = true
qb.Select(user).From("users")
qb.Build()
if strings.Trim(qb.Sql, " ") != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, qb.Sql)
}
}
func testSelectWhenGuessingTableName(t *testing.T) {
expected := `SELECT "id","username","password","total" FROM user`
user := User{}
qb := QueryBuilder{}
qb.IgnoreDynamic = true
qb.Select(user)
qb.Build()
if strings.Trim(qb.Sql, " ") != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, qb.Sql)
}
}
func TestSelectWithoutIgnoringDynamic(t *testing.T) {
expected := `SELECT "id","username","password",(COUNT(col)) "total" FROM users`
user := User{}
qb := QueryBuilder{}
qb.Select(user).From("users")
qb.Build()
if strings.Trim(qb.Sql, " ") != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, qb.Sql)
}
}
func TestSelectWithoutInvalidStructAsArg(t *testing.T) {
defer func() {
if rec := recover(); rec == nil {
t.Error("Expected to panic")
}
}()
qb := QueryBuilder{}
qb.Select(123).From("users")
qb.Build()
t.Error("Expected to panic")
}
func TestSimpleWhere(t *testing.T) {
expected := `SELECT user FROM users WHERE id = $?`
qb := QueryBuilder{}
qb.Select("user").From("users").Where("id = $?")
qb.Build()
if strings.Trim(qb.Sql, " ") != expected {
t.Error("Expected: ", expected, " Got: ", qb.Sql)
}
}
func TestMultipleWhere(t *testing.T) {
expected := `SELECT user FROM users WHERE user = 'user' AND password = 'secret'`
qb := QueryBuilder{}
qb.Select("user").From("users").Where("user = 'user'").Where("password = 'secret'")
qb.Build()
if strings.Trim(qb.Sql, " ") != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, qb.Sql)
}
}
func TestSimpleInnerJoin(t *testing.T) {
expected := `SELECT user FROM users INNER JOIN config USING(id)`
qb := QueryBuilder{}
qb.Select("user").From("users").InnerJoin("config USING(id)")
qb.Build()
if strings.Trim(qb.Sql, " ") != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, qb.Sql)
}
}
func TestMulipleInnerJoin(t *testing.T) {
expected := `SELECT user FROM users INNER JOIN config USING(id) INNER JOIN other USING(other_id)`
qb := QueryBuilder{}
qb.Select("user").From("users").InnerJoin("config USING(id)").InnerJoin("other USING(other_id)")
qb.Build()
if strings.Trim(qb.Sql, " ") != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, qb.Sql)
}
}
func TestInsert(t *testing.T) {
db := dbSetup()
defer db.Close()
newuser := User{Username: "test", Password: "123"}
result, err := Insert(db, "user", newuser)
if err != nil {
t.Error("Insert error: ", err)
}
if rows, _ := result.RowsAffected(); rows <= 0 {
t.Error("Insert didn't product any affected rows")
}
var count int
err = db.QueryRow("SELECT COUNT(*) FROM user").Scan(&count)
if err != nil {
t.Error(err)
}
if count != 1 {
t.Error("Expected 1 row, got", count)
}
}
func TestUpdate(t *testing.T) {
db := dbSetup()
defer db.Close()
db.Exec(`INSERT INTO user(username, password) VALUES('john', 'doe')`)
newuser := User{ID: 1, Username: "NewUser", Password: "NewPassword"}
result, err := Update(db, "user", newuser)
if err != nil {
t.Error(err)
}
if rows, _ := result.RowsAffected(); rows <= 0 {
t.Error("No rows affected by the update")
}
var user, password string
err = db.QueryRow("SELECT username, password FROM user WHERE id = 1").Scan(&user, &password)
if err != nil {
t.Error(err)
}
if user != "NewUser" {
t.Errorf("Expected 'NewUser' got '%s'", user)
}
if password != "NewPassword" {
t.Errorf("Expected 'NewPassword' got '%s'", password)
}
}
func TestDelete(t *testing.T) {
db := dbSetup()
defer db.Close()
db.Exec(`INSERT INTO user(username, password) VALUES('john', 'doe')`)
newuser := User{ID: 1, Username: "NewUser", Password: "NewPassword"}
result, err := Delete(db, "user", newuser)
if err != nil {
t.Error(err)
}
if rows, _ := result.RowsAffected(); rows <= 0 {
t.Error("No rows affected by the delete")
}
var total int
err = db.QueryRow("SELECT COUNT(*) FROM user WHERE id = 1").Scan(&total)
if err != nil {
t.Error(err)
}
if total > 0 {
t.Error("Delete didn't delete the row")
}
}