forked from chuckpreslar/codex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_visitor_test.go
More file actions
38 lines (32 loc) · 1.12 KB
/
postgres_visitor_test.go
File metadata and controls
38 lines (32 loc) · 1.12 KB
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
package codex
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestPostgresBinding(t *testing.T) {
sql, args, err := NewPostgresVisitor().Accept(Binding())
assert.Nil(t, err)
assert.Equal(t, "$1", sql)
assert.Equal(t, []interface{}(nil), args)
}
func TestPostgresLike(t *testing.T) {
sql, args, err := NewPostgresVisitor().Accept(Like(1, 2))
assert.Nil(t, err)
assert.Equal(t, "$1 ILIKE $2", sql)
assert.Equal(t, []interface{}{1, 2}, args)
}
func TestPostgresUnike(t *testing.T) {
sql, args, err := NewPostgresVisitor().Accept(Unlike(1, 2))
assert.Nil(t, err)
assert.Equal(t, "$1 NOT ILIKE $2", sql)
assert.Equal(t, []interface{}{1, 2}, args)
}
func TestPostgresSubSelect(t *testing.T) {
subTab := Table("sub_table")
sub := subTab.Select(Sum(Column("s"))).Where(Literal("group_id = ?", 77)).Group(Column("id"))
one := Table("").Select(Coalesce(sub, 0).As("total"))
sql, args, err := NewPostgresVisitor().Accept(one)
assert.Nil(t, err)
assert.Equal(t, `(SELECT COALESCE((SELECT SUM("s") FROM "sub_table" WHERE (group_id = $1) GROUP BY "id"),$2) AS "total")`, sql)
assert.Equal(t, []interface{}{77, 0}, args)
}