-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqlc_test.go
60 lines (51 loc) · 1.55 KB
/
sqlc_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
package sqlc
import (
"reflect"
"runtime"
"strings"
"testing"
)
func TestBasicComposition(t *testing.T) {
s := Statement{}
// These statements are deliberately out of order
s = s.Group("role").Order("id").Limit("30")
s = s.Where("name = 'Marge'")
s = s.Select("*").From("Employees")
s = s.Having("a=1")
s = s.Join("LEFT JOIN Companies").Join("INNER JOIN Roles")
expect(t, s.Args(), make([]interface{}, 0))
expect(t, s.SQL(), strings.TrimSpace(`
SELECT *
FROM Employees
LEFT JOIN Companies INNER JOIN Roles
WHERE (name = 'Marge')
GROUP BY role
HAVING (a=1)
ORDER BY id
LIMIT 30
`))
}
func TestArgumentComposition(t *testing.T) {
s := Statement{}
s = s.Where("name = ? OR name = ?", "Marge", "Alice").Where("role = ?", "Comptroller")
expect(t, s.Args(), []interface{}{"Marge", "Alice", "Comptroller"})
expect(t, s.SQL(), strings.TrimSpace("WHERE (name = ? OR name = ?) AND (role = ?)"))
// PostgreSQL argument composition
s.PostgreSQL = true
expect(t, s.SQL(), strings.TrimSpace("WHERE (name = $1 OR name = $2) AND (role = $3)"))
}
func TestImmutability(t *testing.T) {
orig := Statement{}
orig = orig.Select("apples")
expect(t, orig.SQL(), strings.TrimSpace("SELECT apples"))
modified := orig.Select("oranges")
expect(t, modified.SQL(), strings.TrimSpace("SELECT apples, oranges"))
expect(t, orig.SQL(), strings.TrimSpace("SELECT apples"))
}
/* Test Helpers */
func expect(t *testing.T, a interface{}, b interface{}) {
if !reflect.DeepEqual(a, b) {
_, _, line, _ := runtime.Caller(1)
t.Errorf("line %d: Got %#v, expected %#v", line, a, b)
}
}