-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_test.go
More file actions
261 lines (228 loc) · 4.95 KB
/
query_test.go
File metadata and controls
261 lines (228 loc) · 4.95 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"testing"
"time"
"github.com/DATA-DOG/go-txdb"
_ "github.com/DATA-DOG/go-txdb"
_ "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
"github.com/romanyx/polluter"
"github.com/stretchr/testify/assert"
)
var dbname string = "test"
const mysqlSchema = `
CREATE TABLE IF NOT EXISTS users (
id integer NOT NULL,
name varchar(255) NOT NULL
);
`
const input = `
users:
- id: 1
name: Jake
- id: 2
name: Sarah
- id: 3
name: Ted
`
func init() {
err := newMySQL()
if err != nil {
log.Fatalf("prepare mysql: %v\n", err)
}
txdb.Register("mysqltx", "mysql", fmt.Sprintf("test:test@/%s", dbname))
}
func newMySQL() error {
db, err := sql.Open("mysql", "test:test@/")
if err != nil {
return err
}
defer db.Close()
if _, err := db.Exec("CREATE DATABASE IF NOT EXISTS " + dbname); err != nil {
return err
}
if _, err := db.Exec("USE " + dbname); err != nil {
return err
}
if _, err := db.Exec(mysqlSchema); err != nil {
return errors.Wrap(err, "failed to create schema")
}
return nil
}
func TestRunQuery(t *testing.T) {
tests := []struct {
name string
request *reqMySQL
result func(res interface{}) bool
wantErr bool
}{
{
name: "Check QueryRows",
request: &reqMySQL{
QueryRows: &QueryRows{
Query: "SELECT * FROM users WHERE name = 'Ted';",
},
Timeout: 10,
},
result: func(res interface{}) bool {
resT, ok := res.([]map[string]interface{})
return ok && resT[0]["name"] == "Ted"
},
wantErr: false,
},
{
name: "Check InsertRows",
request: &reqMySQL{
InsertRows: &InsertRows{
Query: "INSERT INTO users VALUES(?, ?);",
Params: []interface{}{4, "Lily"},
},
Timeout: 10,
},
result: func(res interface{}) bool {
str, ok := res.(string)
if ok {
return str == "1 row inserted, last inserted ID: 0"
}
return ok
},
wantErr: false,
},
{
name: "Check ",
request: &reqMySQL{
AffectedRows: &AffectedRows{
Query: "UPDATE users SET name = 'Teddy' WHERE name = 'Ted';",
},
Timeout: 10,
},
result: func(res interface{}) bool {
str, ok := res.(string)
if ok {
return str == "1 row affected"
}
return ok
},
wantErr: false,
},
{
name: "Error: MySQL requires either `query_rows`, `insert_rows`, `affected_rows`, or `get_db_stats`",
request: &reqMySQL{
Timeout: 10,
},
wantErr: true,
},
{
name: "Error: MySQL requires either `query_rows`, `insert_rows`, `affected_rows`, or `get_db_stats, not more then one",
request: &reqMySQL{
QueryRows: &QueryRows{
Query: "SELECT * FROM users WHERE name = 'Ted';",
},
InsertRows: &InsertRows{
Query: "INSERT INTO users VALUES(?, ?);",
Params: []any{5, "Siri"},
},
Timeout: 10,
},
wantErr: true,
},
{
name: "Error query syntax",
request: &reqMySQL{
QueryRows: &QueryRows{
Query: "SELECT * FROM;",
},
Timeout: 10,
},
wantErr: true,
},
{
name: "Error unknown table",
request: &reqMySQL{
QueryRows: &QueryRows{
Query: "SELECT * FROM unknown;",
},
Timeout: 10,
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
db, cleanup := exampleSuite(t)
defer cleanup()
num := 0
var fn func(stmt *sql.Stmt, ctx context.Context) (interface{}, error)
var q *Query
if tt.request.QueryRows != nil {
q = (*Query)(tt.request.QueryRows)
fn = tt.request.QueryRows.run
num++
}
if tt.request.InsertRows != nil {
q = (*Query)(tt.request.InsertRows)
fn = tt.request.InsertRows.run
num++
}
if tt.request.AffectedRows != nil {
q = (*Query)(tt.request.AffectedRows)
fn = tt.request.AffectedRows.run
num++
}
if tt.request.GetDBStats {
if num == 0 {
return
}
num++
}
if num == 0 {
return
}
if num > 1 {
return
}
if tt.request.Timeout == 0 {
tt.request.Timeout = 10
}
ctx, cancelfunc := context.WithTimeout(context.Background(), time.Duration(tt.request.Timeout)*time.Second)
defer cancelfunc()
res, err := q.handleQuery(db, ctx, fn)
if tt.wantErr && err == nil {
assert.NotNil(t, err)
return
}
if !tt.wantErr && err != nil {
assert.Nil(t, err)
return
}
if err == nil {
if equal := tt.result(res); !equal {
t.Fatalf("Got unexpected result: %v", res)
}
}
})
}
}
func exampleSuite(t *testing.T) (*sql.DB, func() error) {
db, cleanup := prepareMySQLDB(t)
p := polluter.New(polluter.MySQLEngine(db))
if err := p.Pollute(strings.NewReader(input)); err != nil {
t.Fatalf("failed to pollute: %s", err)
}
return db, cleanup
}
func prepareMySQLDB(t *testing.T) (db *sql.DB, cleanup func() error) {
cName := fmt.Sprintf("connection_%d", time.Now().UnixNano())
db, err := sql.Open("mysqltx", cName)
if err != nil {
t.Fatalf("open mysqltx connection: %s", err)
}
return db, db.Close
}