-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_test.go
133 lines (123 loc) · 3.18 KB
/
filter_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
package golf
import (
"database/sql"
"regexp"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
type testModel struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Username string `json:"username"`
CreatedAt time.Time `json:"created_at"`
}
func (m *testModel) Field() map[string][]Filter {
return map[string][]Filter{
"ID": {Equal},
"UserID": {Equal, Gte},
"Username": {Equal, Gte},
"CreatedAt": {Lt, Gte},
}
}
func NewDB(db *sql.DB) (*gorm.DB, error) {
return gorm.Open(
postgres.New(postgres.Config{
PreferSimpleProtocol: true,
Conn: db,
}),
&gorm.Config{
Logger: logger.Default,
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
AllowGlobalUpdate: false,
})
}
func TestGolfFind(t *testing.T) {
db, mock, err := sqlmock.New()
assert.NoError(t, err)
gormDB, err := NewDB(db)
assert.NoError(t, err)
gormDB2, err := NewDB(db)
assert.NoError(t, err)
m := &testModel{}
cases := []struct {
Name string
query map[string][]string
golf *Golf
Except string
}{
{
query: map[string][]string{
"eq_id": {"1"},
},
golf: NewGolf(gormDB),
Except: regexp.QuoteMeta(`SELECT * FROM "test_model" WHERE id = $1 LIMIT 10`),
},
{
query: map[string][]string{
"eq_username": {"1"},
},
golf: NewGolf(gormDB2),
Except: regexp.QuoteMeta(`SELECT * FROM "test_model" WHERE username = $1 LIMIT 10`),
},
}
for _, tt := range cases {
t.Run(tt.Name, func(t *testing.T) {
assert.NoError(t, tt.golf.Build(m, tt.query).Error)
var testMod []testModel
// fixme SQL is not completely written because where conditional disorder
mock.ExpectQuery(tt.Except).
WillReturnRows(
sqlmock.NewRows([]string{"id"}).AddRow("1"))
assert.NoError(t, tt.golf.Find(&testMod).Error)
assert.NoError(t, mock.ExpectationsWereMet())
assert.Equal(t, len(testMod), 1)
})
}
}
func TestCheckFilter(t *testing.T) {
db, _, err := sqlmock.New()
assert.NoError(t, err)
gormDB, err := NewDB(db)
assert.NoError(t, err)
gol := NewGolf(gormDB)
query := map[string][]string{
"eq_id": {"1"},
"eq_created_user": {"1"},
}
lowerQ := map[string][]Filter{
"id": {Equal},
"created_user": {Equal},
}
gol.originalQuery = query
realQ, err := gol.checkAndBuildQuery(lowerQ)
assert.NoError(t, err)
t.Log(realQ)
}
func TestDoFilter(t *testing.T) {
db, mock, err := sqlmock.New()
assert.NoError(t, err)
gormDB, err := NewDB(db)
assert.NoError(t, err)
gol := NewGolf(gormDB)
query := map[string][]string{
"eq_id": {"1"},
"lt_created_at": {"2021-12-10"},
"gte_created_at": {"2021-12-10"},
}
// real query SELECT * FROM "test_model" WHERE created_at < $1 AND created_at >= $2 AND id = $3 LIMIT 10
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "test_model" WHERE `)).WillReturnRows(
sqlmock.NewRows([]string{"id"}).AddRow(1))
gol.originalQuery = query
var ret []testModel
assert.NoError(t, gol.Build(&testModel{}, query).Find(&ret).Error)
assert.NoError(t, err)
assert.NoError(t, mock.ExpectationsWereMet())
}