-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_builder_test.go
116 lines (99 loc) · 2.68 KB
/
query_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
package pagefilter
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestRetrieve(t *testing.T) {
mockDB := NewMockDB(t)
mockFilter := NewMockFilter(t)
defer func() {
mockDB.AssertExpectations(t)
mockFilter.AssertExpectations(t)
}()
mockFilter.On("Join").Return("JOIN table 1 ON table 1.id = table 2.id AND table 1.id = ?", []any{"1"})
mockFilter.On("Where").Return("t.age > ?", []any{18})
mockDB.On("Select", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything,
mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
p := &Paginator{
db: mockDB,
details: &PaginatorDetails{
SortBy: "id",
sortOperator: ">",
sortComparator: "ASC",
LastID: "1",
Limit: 10,
},
filter: mockFilter,
table: "test_table",
}
// Test cases
tests := []struct {
name string
pivot string
dest any
wantErr bool
}{
{"nil destination", "pivot", nil, true},
{"non-pointer destination", "pivot", []struct{}{}, true},
{"non-slice pointer destination", "pivot", &struct{}{}, true},
{"non-struct slice element", "pivot", &[]int{}, true},
{"valid destination", "pivot", &[]struct{ ID int }{}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := p.Retrieve(tt.pivot, tt.dest)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestCounts(t *testing.T) {
mockDB := NewMockDB(t)
mockFilter := NewMockFilter(t)
defer func() {
mockDB.AssertExpectations(t)
mockFilter.AssertExpectations(t)
}()
mockFilter.On("Join").Return("JOIN table 1 ON table 1.id = table 2.id AND table 1.id = ?", []any{"1"})
mockFilter.On("Where").Return("t.age > ?", []any{18})
mockDB.On("Get", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything,
mock.Anything, mock.Anything).Return(func(dest any, sql string, args ...any) error {
return nil
})
p := &Paginator{
db: mockDB,
details: &PaginatorDetails{
SortBy: "id",
sortOperator: ">",
sortComparator: "ASC",
LastID: "1",
Limit: 10,
},
filter: mockFilter,
table: "test_table",
}
var count int64 = 0
err := p.Counts(&count)
assert.NoError(t, err)
}
func TestTrimWherePrefix(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"with AND prefix", "AND condition", "condition"},
{"with OR prefix", "OR condition", "condition"},
{"without prefix", "condition", "condition"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := trimWherePrefix(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}