-
Notifications
You must be signed in to change notification settings - Fork 0
/
executer_test.go
155 lines (139 loc) · 3.71 KB
/
executer_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
package mysqlbatch_test
import (
"bytes"
"context"
_ "embed"
"fmt"
"log"
"os"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/mashiike/mysqlbatch"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/stretchr/testify/require"
)
func TestQueryScanner(t *testing.T) {
cases := []struct {
input string
queries []string
}{
{
input: `
UPDATE user_credentials
SET password_hash = "";
UPDATE user_profiles SET email = concat(MD5(email), '@localhost');
DELETE FROM roles
`,
queries: []string{
`UPDATE user_credentials SET password_hash = ""`,
`UPDATE user_profiles SET email = concat(MD5(email), '@localhost')`,
`DELETE FROM roles`,
},
},
{
input: `
BEGIN;
SET FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
`,
queries: []string{
`BEGIN`,
`SET FOREIGN_KEY_CHECKS = 0`,
`SET FOREIGN_KEY_CHECKS = 1`,
`COMMIT`,
``,
},
},
}
for casenum, c := range cases {
t.Run(fmt.Sprintf("case.%d", casenum), func(t *testing.T) {
scanner := mysqlbatch.NewQueryScanner(strings.NewReader(c.input))
i := 0
for ; scanner.Scan(); i++ {
query := scanner.Query()
if i >= len(c.queries) {
t.Logf("query: %s", query)
t.Fatalf("unexpected over query count: %d", i)
}
if diff, same := diffStr(query, c.queries[i]); !same {
t.Logf("got : %s", query)
t.Logf("expected: %s", c.queries[i])
t.Errorf("unexpected query diff: %s", diff)
}
}
if i != len(c.queries) {
t.Errorf("unexpected count: %d", i)
}
})
}
}
func diffStr(a, b string) (string, bool) {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(b, a, true)
var same bool = true
for _, d := range diffs {
if d.Type != diffmatchpatch.DiffEqual {
same = false
}
}
return dmp.DiffPrettyText(diffs), same
}
//go:embed testdata/test.sql
var testSQL []byte
//go:embed testdata/test_template.sql
var testTemplateSQL []byte
func TestExecuterExecute(t *testing.T) {
conf := mysqlbatch.NewDefaultConfig()
conf.Password = "mysqlbatch"
conf.Location = "Asia/Tokyo"
e, err := mysqlbatch.New(context.Background(), conf)
require.NoError(t, err)
defer e.Close()
e.SetTimeCheckQuery("SELECT NOW()")
e.SetTableSelectHook(func(query, table string) {
t.Log(query + "\n" + table + "\n")
})
var count int32
e.SetSelectHook(func(query string, columns []string, rows [][]string) {
atomic.AddInt32(&count, 1)
require.Equal(t, `SELECT * FROM users WHERE age is NOT NULL LIMIT 5`, query)
require.Equal(t, 5, len(rows))
})
err = e.Execute(bytes.NewReader(testSQL), nil)
require.NoError(t, err)
log.Println("LastExecuteTime:", e.LastExecuteTime())
require.InDelta(t, time.Since(e.LastExecuteTime()), 0, float64(5*time.Minute))
require.EqualValues(t, 1, count)
}
func TestExecuterExecute__WithVars(t *testing.T) {
os.Setenv("ENV", "test")
mysqlbatch.DefaultSQLDumper = os.Stderr
conf := mysqlbatch.NewDefaultConfig()
conf.Password = "mysqlbatch"
conf.Location = "Asia/Tokyo"
e, err := mysqlbatch.New(context.Background(), conf)
require.NoError(t, err)
defer e.Close()
e.SetTimeCheckQuery("SELECT NOW()")
e.SetTableSelectHook(func(query, table string) {
t.Log(query + "\n" + table + "\n")
})
var count int32
e.SetSelectHook(func(query string, columns []string, rows [][]string) {
atomic.AddInt32(&count, 1)
require.Equal(t, `SELECT * FROM users WHERE age is NOT NULL LIMIT 5`, query)
require.Equal(t, 5, len(rows))
})
err = e.Execute(bytes.NewReader(testTemplateSQL), map[string]string{
"relation": "users",
"limit": "5",
"age_condition": " > 20",
})
require.NoError(t, err)
log.Println("LastExecuteTime:", e.LastExecuteTime())
require.InDelta(t, time.Since(e.LastExecuteTime()), 0, float64(5*time.Minute))
require.EqualValues(t, 1, count)
}