-
Notifications
You must be signed in to change notification settings - Fork 0
/
where_test.go
124 lines (104 loc) · 3.45 KB
/
where_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
114
115
116
117
118
119
120
121
122
123
124
package liqu
import (
"fmt"
"net/url"
"testing"
)
func TestWhereClause(t *testing.T) {
cb := NewConditionBuilder()
whereClause := cb.Column("name").
Condition(ILike, "%John%").
And("age", GreaterThanOrEqual, 18).
OrIsNull("age").
AndNested(func(n *ConditionBuilder) {
n.Column("country").Condition(Equal, "USA").
Or("city", Equal, "New York")
}).
Build()
expected := `name ~~* $1 AND age >= $2 OR age IS NULL AND (country = $3 OR city = $4)`
if expected != whereClause {
t.Errorf("expected:\n%s\ngot:\n%s", expected, whereClause)
}
}
func TestNewConditionBuilder(t *testing.T) {
cb := NewConditionBuilder()
whereClause := cb.Column("name").
Condition(ILike, "%John%").
And("age", GreaterThanOrEqual, 18).
OrIsNull("age").
AndNested(func(n *ConditionBuilder) {
n.In("country", "USA", "Canada").
OrNotIn("city", "New York", "Los Angeles")
}).
And("number_seven", Equal, 7).
Build()
expected := `name ~~* $1 AND age >= $2 OR age IS NULL AND (country IN ($3, $4) OR city NOT IN ($5, $6)) AND number_seven = $7`
if expected != whereClause {
t.Errorf("expected:\n%s\ngot:\n%s", expected, whereClause)
}
}
func TestParseURLQueryToConditionBuilder(t *testing.T) {
// Simulate a URL query with the "where" parameter
urlQuery := url.Values{}
urlQuery.Set("where", "name|ILIKE|%John%,age|>=|18,age|IS NULL,country|IN|USA--Canada,city|NOT IN|New York--Los Angeles")
whereQueryParam := urlQuery.Get("where")
cb, err := ParseURLQueryToConditionBuilder(whereQueryParam)
if err != nil {
t.Error(err)
return
}
whereClause := cb.Build()
expected := `name ILIKE $1 AND age >= $2 AND age IS NULL AND country IN ($3, $4) AND city NOT IN ($5, $6)`
if expected != whereClause {
t.Errorf("expected:\n%s\ngot:\n%s", expected, whereClause)
}
}
func TestParseURLQueryToConditionBuilderNestedOR(t *testing.T) {
// Simulate a URL query with the "where" parameter
urlQuery := url.Values{}
urlQuery.Set("where", "name|ILIKE|%John%,(OR,age|>=|18,age|IS NULL),country|IN|USA--Canada,city|NOT IN|New York--Los Angeles")
whereQueryParam := urlQuery.Get("where")
cb, err := ParseURLQueryToConditionBuilder(whereQueryParam)
if err != nil {
fmt.Println("Error:", err)
return
}
whereClause := cb.Build()
expected := `name ILIKE $1 AND (age >= $2 OR age IS NULL) AND country IN ($3, $4) AND city NOT IN ($5, $6)`
if expected != whereClause {
t.Errorf("expected:\n%s\ngot:\n%s", expected, whereClause)
}
whereArgs := cb.Args()
expectedArgs := []interface{}{"%John%", "18", "USA", "Canada", "New York", "Los Angeles"}
if len(whereArgs) != len(expectedArgs) {
t.Errorf("expected:\n%d\ngot:\n%d", len(expectedArgs), len(whereArgs))
return
}
for k, v := range expectedArgs {
if v != whereArgs[k] {
t.Errorf("expected:\n%s\ngot:\n%s", v, whereArgs[k])
}
}
}
func TestLikeWrapSearched(t *testing.T) {
cb := NewConditionBuilder()
whereClause := cb.Column("name").
Condition(ILike, "John").
And("age", GreaterThanOrEqual, 18).
OrIsNull("age").
AndNested(func(n *ConditionBuilder) {
n.Column("country").Condition(Equal, "USA").
Or("city", Equal, "New York")
}).
Build()
expected := `name ~~* $1 AND age >= $2 OR age IS NULL AND (country = $3 OR city = $4)`
if expected != whereClause {
t.Errorf("expected:\n%s\ngot:\n%s", expected, whereClause)
}
if len(cb.Args()) != 4 {
t.Errorf("expected:\n%d\ngot:\n%d", 4, len(cb.Args()))
}
if cb.Args()[0] != "%John%" {
t.Errorf("expected:\n%s\ngot:\n%s", "%John%", cb.Args()[0])
}
}