-
Notifications
You must be signed in to change notification settings - Fork 3
/
select_test.go
133 lines (111 loc) · 3.04 KB
/
select_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
package sqlbuilder
import (
"reflect"
"testing"
)
type customer struct {
ID int
Name string
Phone *string
}
func TestSimpleSelect(t *testing.T) {
c := customer{}
query, _, dest := Select().
Dialect(MySQL).
From("customers").
Map("id", &c.ID).
Map("name", &c.Name).
Map("phone", &c.Phone).
Map("1+1 AS two", nil).
Build()
expectedQuery := "SELECT id, name, phone, 1+1 AS two FROM customers"
if query != expectedQuery {
t.Errorf("bad query: %s", query)
}
expectedDest := []interface{}{&c.ID, &c.Name, &c.Phone, &nullDest}
if !reflect.DeepEqual(dest, expectedDest) {
t.Errorf("bad dest: %v", dest)
}
}
func TestSimpleSelectWithLimitOffset(t *testing.T) {
c := customer{}
query, _, dest := Select().
Dialect(MySQL).
From("customers").
Map("id", &c.ID).
Map("name", &c.Name).
Map("phone", &c.Phone).
Limit(5).
Offset(10).
Build()
expectedQuery := "SELECT id, name, phone FROM customers LIMIT 5 OFFSET 10"
if query != expectedQuery {
t.Errorf("bad query: %s", query)
}
expectedDest := []interface{}{&c.ID, &c.Name, &c.Phone}
if !reflect.DeepEqual(dest, expectedDest) {
t.Errorf("bad dest: %v", dest)
}
}
func TestSimpleSelectWithJoins(t *testing.T) {
c := customer{}
query, _, _ := Select().
Dialect(MySQL).
From("customers").
Map("id", &c.ID).
Map("name", &c.Name).
Map("phone", &c.Phone).
Join("INNER JOIN orders ON orders.customer_id = customers.id").
Join("LEFT JOIN items ON items.order_id = orders.id").
Build()
expectedQuery := "SELECT id, name, phone FROM customers INNER JOIN orders ON orders.customer_id = customers.id LEFT JOIN items ON items.order_id = orders.id"
if query != expectedQuery {
t.Errorf("bad query: %s", query)
}
}
func TestSelectWithWhereMySQL(t *testing.T) {
c := customer{}
query, args, _ := Select().
Dialect(MySQL).
From("customers").
Map("id", &c.ID).
Map("name", &c.Name).
Map("phone", &c.Phone).
Where("id = ? AND name IS NOT NULL", 9).
Build()
expectedQuery := "SELECT id, name, phone FROM customers WHERE (id = ? AND name IS NOT NULL)"
if query != expectedQuery {
t.Errorf("bad query: %s", query)
}
expectedArgs := []interface{}{9}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("bad args: %v", args)
}
}
func TestSelectWithGroupMySQL(t *testing.T) {
var count uint
query, _, _ := Select().Dialect(MySQL).From("customers").Map("COUNT(*)", &count).Group("city").Build()
expectedQuery := "SELECT COUNT(*) FROM customers GROUP BY city"
if query != expectedQuery {
t.Errorf("bad query: %s", query)
}
}
func TestSelectWithWherePostgres(t *testing.T) {
c := customer{}
query, args, _ := Select().
Dialect(Postgres).
From("customers").
Map("id", &c.ID).
Map("name", &c.Name).
Map("phone", &c.Phone).
Where("id = ? AND name IS NOT NULL", 9).
Build()
expectedQuery := `SELECT id, name, phone FROM customers WHERE (id = $1 AND name IS NOT NULL)`
if query != expectedQuery {
t.Errorf("bad query: %s", query)
}
expectedArgs := []interface{}{9}
if !reflect.DeepEqual(args, expectedArgs) {
t.Errorf("bad args: %v", args)
}
}