-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_test.go
181 lines (173 loc) · 3.97 KB
/
builder_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package sqld
import (
"testing"
"github.com/stretchr/testify/assert"
)
type BuilderTestModel struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Age int `json:"age" db:"age"`
Email string `json:"email" db:"email"`
Active bool `json:"active" db:"active"`
Salary float64 `json:"salary" db:"salary"`
Nullable *string `json:"nullable" db:"nullable"`
}
func (BuilderTestModel) TableName() string {
return "test_models"
}
func TestBuildQuery(t *testing.T) {
// Register test model
if err := Register[BuilderTestModel](); err != nil {
t.Fatalf("Failed to register test model: %v", err)
}
tests := []struct {
name string
request QueryRequest
want string
wantErr bool
}{
{
name: "basic select",
request: QueryRequest{
Select: []string{"name", "age"},
},
want: "SELECT name, age FROM test_models",
},
{
name: "with where clause",
request: QueryRequest{
Select: []string{"name", "age"},
Where: []Condition{
{
Field: "age",
Operator: OpGreaterThan,
Value: 18,
},
},
},
want: "SELECT name, age FROM test_models WHERE age > $1",
},
{
name: "with multiple where conditions",
request: QueryRequest{
Select: []string{"name", "age", "email"},
Where: []Condition{
{
Field: "age",
Operator: OpGreaterThanOrEqual,
Value: 21,
},
{
Field: "email",
Operator: OpEqual,
Value: "[email protected]",
},
},
},
want: "SELECT name, age, email FROM test_models WHERE age >= $1 AND email = $2",
},
{
name: "with order by",
request: QueryRequest{
Select: []string{"name", "age"},
OrderBy: []OrderByClause{
{Field: "age", Desc: true},
},
},
want: "SELECT name, age FROM test_models ORDER BY age DESC",
},
{
name: "with pagination",
request: QueryRequest{
Select: []string{"name", "age"},
Limit: intPtr(10),
Offset: intPtr(0),
},
want: "SELECT name, age FROM test_models LIMIT 10 OFFSET 0",
},
{
name: "complex query",
request: QueryRequest{
Select: []string{"name", "age", "email"},
Where: []Condition{
{
Field: "age",
Operator: OpGreaterThan,
Value: 25,
},
{
Field: "email",
Operator: OpIn,
Value: []string{"[email protected]", "[email protected]"},
},
},
OrderBy: []OrderByClause{
{Field: "name", Desc: false},
{Field: "age", Desc: true},
},
Limit: intPtr(10),
Offset: intPtr(20),
},
want: "SELECT name, age, email FROM test_models WHERE age > $1 AND email IN ($2,$3) ORDER BY name ASC, age DESC LIMIT 10 OFFSET 20",
},
{
name: "with like operator",
request: QueryRequest{
Select: []string{"name", "email"},
Where: []Condition{
{
Field: "name",
Operator: OpLike,
Value: "%John%",
},
},
},
want: "SELECT name, email FROM test_models WHERE name LIKE $1",
},
{
name: "with ilike operator",
request: QueryRequest{
Select: []string{"name", "email"},
Where: []Condition{
{
Field: "name",
Operator: OpILike,
Value: "%john%",
},
},
},
want: "SELECT name, email FROM test_models WHERE name ILIKE $1",
},
{
name: "with null checks",
request: QueryRequest{
Select: []string{"name", "email"},
Where: []Condition{
{
Field: "email",
Operator: OpIsNull,
},
{
Field: "name",
Operator: OpIsNotNull,
},
},
},
want: "SELECT name, email FROM test_models WHERE email IS NULL AND name IS NOT NULL",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildQuery[BuilderTestModel](tt.request)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
// Convert the SelectBuilder to a string
sql, _, err := got.ToSql()
assert.NoError(t, err)
assert.Equal(t, tt.want, sql)
})
}
}