-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_test.go
104 lines (96 loc) · 2.87 KB
/
query_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
package nidhi_test
import (
"io"
"testing"
"github.com/akshayjshah/attest"
"github.com/srikrsna/nidhi"
)
func TestQueryer(t *testing.T) {
t.Parallel()
t.Run("where", func(t *testing.T) {
t.Parallel()
q := &nidhi.Query[testField]{}
gotSql, gotArgs, err := q.Where(testField{}, eqCond).ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `field = ?`)
attest.Equal(t, gotArgs, []any{0})
})
t.Run("not", func(t *testing.T) {
t.Parallel()
q := &nidhi.Query[testField]{}
gotSql, gotArgs, err := q.Not().Where(testField{}, eqCond).ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `NOT field = ?`)
attest.Equal(t, gotArgs, []any{0})
})
t.Run("paren", func(t *testing.T) {
t.Parallel()
q := &nidhi.Query[testField]{}
sq := &nidhi.Query[testField]{}
gotSql, gotArgs, err := q.Paren(sq.Where(testField{}, eqCond)).ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `(field = ?)`)
attest.Equal(t, gotArgs, []any{0})
})
t.Run("and", func(t *testing.T) {
t.Parallel()
q := &nidhi.Query[testField]{}
gotSql, gotArgs, err := q.Where(testField{}, ltCond).And().Where(testField{}, gtCond).ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `field < ? AND field > ?`)
attest.Equal(t, gotArgs, []any{0, 0})
})
t.Run("or", func(t *testing.T) {
t.Parallel()
q := &nidhi.Query[testField]{}
gotSql, gotArgs, err := q.Where(testField{}, ltCond).Or().Where(testField{}, gtCond).ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `field < ? OR field > ?`)
attest.Equal(t, gotArgs, []any{0, 0})
})
t.Run("reset", func(t *testing.T) {
t.Parallel()
q := &nidhi.Query[testField]{}
gotSql, gotArgs, err := q.Where(testField{}, eqCond).ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `field = ?`)
attest.Equal(t, gotArgs, []any{0})
q.Reset()
gotSql, gotArgs, err = q.Where(testField{}, ltCond).ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `field < ?`)
attest.Equal(t, gotArgs, []any{0})
})
t.Run("replace", func(t *testing.T) {
t.Parallel()
q := &nidhi.Query[testField]{}
conj := q.Where(testField{}, eqCond)
gotSql, gotArgs, err := conj.ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `field = ?`)
attest.Equal(t, gotArgs, []any{0})
rConj, err := conj.ReplaceArgs([]any{1})
attest.Ok(t, err)
gotSql, gotArgs, err = conj.ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `field = ?`)
attest.Equal(t, gotArgs, []any{0})
gotSql, gotArgs, err = rConj.ToSql()
attest.Ok(t, err)
attest.Equal(t, gotSql, `field = ?`)
attest.Equal(t, gotArgs, []any{1})
})
}
var (
eqCond nidhi.Cond = opCond("=")
gtCond nidhi.Cond = opCond(">")
ltCond nidhi.Cond = opCond("<")
)
type opCond string
func (c opCond) AppendCond(field string, w io.StringWriter, args *[]any) error {
w.WriteString(field + " " + string(c) + " ?")
*args = append(*args, 0)
return nil
}
type testField struct{}
func (testField) Selector() string { return "field" }