-
Notifications
You must be signed in to change notification settings - Fork 2
/
boolexprs_test.go
55 lines (52 loc) · 1.9 KB
/
boolexprs_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
package sqb
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_NotExpr(t *testing.T) {
var tests = []struct {
name string
sqb SQB
wantErr bool
expectedRawSQL string
expectedArgs []interface{}
}{
{
name: "not with eq",
expectedRawSQL: "SELECT * FROM users AS users WHERE (NOT (age=10))",
sqb: From(TableName("users").As("users")).Where(Not(Eq(Column("age"), Column("10")))),
},
{
name: "not with and",
expectedRawSQL: "SELECT * FROM users AS users WHERE (NOT ((age=10) AND (age=99)))",
sqb: From(TableName("users").As("users")).Where(Not(And(Eq(Column("age"), Column("10")), Eq(Column("age"), Column("99"))))),
},
{
name: "not with or",
expectedRawSQL: "SELECT * FROM users AS users WHERE (NOT ((age=10) OR (age=99)))",
sqb: From(TableName("users").As("users")).Where(Not(Or(Eq(Column("age"), Column("10")), Eq(Column("age"), Column("99"))))),
},
{
name: "not with exists",
expectedRawSQL: "SELECT * FROM users AS users WHERE (NOT (exists(SELECT * FROM statuses WHERE (statuses.active=?) AND (users.id=statuses.user_id))))",
expectedArgs: []interface{}{true},
sqb: From(TableName("users").As("users")).
Where(Not(ExistsStmt{
Select: From(TableName("statuses")).Where(Eq(Column("statuses.active"), Arg{V: true}), Eq(Column("users.id"), Column("statuses.user_id")))})),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sqb := tt.sqb
tsw := &DefaultSQLWriter{}
if err := sqb.WriteSQLTo(tsw); (err != nil) != tt.wantErr {
t.Errorf("WriteSQLTo() error = %v, wantErr %v", err, tt.wantErr)
}
builded := tsw.String()
if builded != tt.expectedRawSQL {
t.Errorf("WriteSQLTo() raw SQL expected = %v, actual = %v", tt.expectedRawSQL, builded)
}
assert.Equal(t, tt.expectedArgs, tsw.Args)
})
}
}