forked from go-catupiry/query_parser_to_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGORMAdapter_test.go
183 lines (128 loc) · 5.04 KB
/
GORMAdapter_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
182
183
package query_parser_to_db
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
func TestGormDBOperations(t *testing.T) {
assert := assert.New(t)
db := GetFakeGormDB()
err := db.AutoMigrate(&ContentModelStub{})
assert.Nil(err)
urlString := "https://example.com/example"
parsedURL, _ := url.Parse(urlString)
q := NewQuery(50)
err = q.ParseFromURLValues(parsedURL.Query())
assert.Nil(err)
t.Run("Should generate a valid equal query", func(t *testing.T) {
fieldName, value := "title", "Lov"
db.DryRun = true
queryI, err := gormDBOperations["equal"](fieldName, value, db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE title = ?", query.Statement.SQL.String())
assert.Equal([]interface{}{"Lov"}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid not-equal query", func(t *testing.T) {
fieldName, value := "body", "NotLovi"
db.DryRun = true
queryI, err := gormDBOperations["not-equal"](fieldName, value, db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE body != ?", query.Statement.SQL.String())
assert.Equal([]interface{}{"NotLovi"}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid is-null query", func(t *testing.T) {
fieldName := "title"
db.DryRun = true
queryI, err := gormDBOperations["is-null"](fieldName, "", db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE title IS NULL", query.Statement.SQL.String())
assert.Equal([]interface{}{}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid is-not-null query", func(t *testing.T) {
fieldName := "title"
db.DryRun = true
queryI, err := gormDBOperations["is-not-null"](fieldName, "", db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE title IS NOT NULL", query.Statement.SQL.String())
assert.Equal([]interface{}{}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid starts-with query", func(t *testing.T) {
fieldName, value := "title", "Lo"
db.DryRun = true
queryI, err := gormDBOperations["starts-with"](fieldName, value, db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE title LIKE ?", query.Statement.SQL.String())
assert.Equal([]interface{}{"Lo%"}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid not-starts-with query", func(t *testing.T) {
fieldName, value := "title", "Lo"
db.DryRun = true
queryI, err := gormDBOperations["not-starts-with"](fieldName, value, db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE title NOT LIKE ?", query.Statement.SQL.String())
assert.Equal([]interface{}{"Lo%"}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid ends-with query", func(t *testing.T) {
fieldName, value := "title", "ve"
db.DryRun = true
queryI, err := gormDBOperations["ends-with"](fieldName, value, db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE title LIKE ?", query.Statement.SQL.String())
assert.Equal([]interface{}{"%ve"}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid not-ends-with query", func(t *testing.T) {
fieldName, value := "body", "ve"
db.DryRun = true
queryI, err := gormDBOperations["not-ends-with"](fieldName, value, db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE body NOT LIKE ?", query.Statement.SQL.String())
assert.Equal([]interface{}{"%ve"}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid contains query", func(t *testing.T) {
fieldName, value := "body", "ov"
db.DryRun = true
queryI, err := gormDBOperations["contains"](fieldName, value, db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE body LIKE ?", query.Statement.SQL.String())
assert.Equal([]interface{}{"%ov%"}, query.Statement.Vars)
db.DryRun = false
})
t.Run("Should generate a valid not-contains query", func(t *testing.T) {
fieldName, value := "body", "hate"
db.DryRun = true
queryI, err := gormDBOperations["not-contains"](fieldName, value, db, q)
assert.Nil(err)
query := queryI.(*gorm.DB)
query.Find(&[]ContentModelStub{})
assert.Equal("SELECT * FROM `content_model_stubs` WHERE body NOT LIKE ?", query.Statement.SQL.String())
assert.Equal([]interface{}{"%hate%"}, query.Statement.Vars)
db.DryRun = false
})
}