-
Notifications
You must be signed in to change notification settings - Fork 0
/
aux_test.go
54 lines (41 loc) · 1.35 KB
/
aux_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
package sqext
import (
"testing"
sq "github.com/Masterminds/squirrel"
"github.com/stretchr/testify/assert"
)
func TestAuxBuilderToSql(t *testing.T) {
b := Aux(sq.Select("*").From("events").Where(sq.Eq{"is_completed": true})).
Alias("completed_events").
Columns("a", "b").
Recursive()
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "RECURSIVE completed_events(a, b) AS (SELECT * FROM events WHERE is_completed = ?)"
assert.Equal(t, expectedSql, sql)
assert.Equal(t, []interface{}{true}, args)
}
func TestAuxBuilderToSqlNoColumns(t *testing.T) {
b := Aux(sq.Select("*").From("a").Where(sq.Eq{"x": 20})).
Alias("a_data").
Recursive()
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "RECURSIVE a_data AS (SELECT * FROM a WHERE x = ?)"
assert.Equal(t, expectedSql, sql)
assert.Equal(t, []interface{}{20}, args)
}
func TestAuxBuilderToSqlBasicAux(t *testing.T) {
b := Aux(sq.Select("a", "b").From("x").Where(sq.Eq{"y": 1})).
Alias("a_data")
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "a_data AS (SELECT a, b FROM x WHERE y = ?)"
assert.Equal(t, expectedSql, sql)
assert.Equal(t, []interface{}{1}, args)
}
func TestAuxBuilderToSqlErr(t *testing.T) {
b := Aux(sq.Select("*").From("events").Where(sq.Gt{"is_completed": true})).Recursive()
_, _, err := b.ToSql()
assert.Error(t, err)
}