forked from Masterminds/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr_test.go
113 lines (87 loc) · 2.27 KB
/
expr_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
package squirrel
import (
"database/sql"
"github.com/stretchr/testify/assert"
"testing"
)
func TestEqToSql(t *testing.T) {
b := Eq{"id": 1}
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "id = ?"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{1}
assert.Equal(t, expectedArgs, args)
}
func TestEqInToSql(t *testing.T) {
b := Eq{"id": []int{1, 2, 3}}
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "id IN (?,?,?)"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{1, 2, 3}
assert.Equal(t, expectedArgs, args)
}
func TestNotEqToSql(t *testing.T) {
b := NotEq{"id": 1}
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "id <> ?"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{1}
assert.Equal(t, expectedArgs, args)
}
func TestEqNotInToSql(t *testing.T) {
b := NotEq{"id": []int{1, 2, 3}}
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "id NOT IN (?,?,?)"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{1, 2, 3}
assert.Equal(t, expectedArgs, args)
}
func TestExprNilToSql(t *testing.T) {
var b Sqlizer
b = NotEq{"name": nil}
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Empty(t, args)
expectedSql := "name IS NOT NULL"
assert.Equal(t, expectedSql, sql)
b = Eq{"name": nil}
sql, args, err = b.ToSql()
assert.NoError(t, err)
assert.Empty(t, args)
expectedSql = "name IS NULL"
assert.Equal(t, expectedSql, sql)
}
func TestNullTypeString(t *testing.T) {
var b Sqlizer
var name sql.NullString
b = Eq{"name": name}
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Empty(t, args)
assert.Equal(t, "name IS NULL", sql)
name.Scan("Name")
b = Eq{"name": name}
sql, args, err = b.ToSql()
assert.NoError(t, err)
assert.Equal(t, []interface{}{"Name"}, args)
assert.Equal(t, "name = ?", sql)
}
func TestNullTypeInt64(t *testing.T) {
var userID sql.NullInt64
userID.Scan(nil)
b := Eq{"user_id": userID}
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Empty(t, args)
assert.Equal(t, "user_id IS NULL", sql)
userID.Scan(10)
b = Eq{"user_id": userID}
sql, args, err = b.ToSql()
assert.NoError(t, err)
assert.Equal(t, []interface{}{10}, args)
assert.Equal(t, "user_id = ?", sql)
}