-
Notifications
You must be signed in to change notification settings - Fork 36
/
mysql_test.go
212 lines (170 loc) · 5.41 KB
/
mysql_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
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
package features
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path"
"testing"
"time"
"github.com/go-sql-driver/mysql"
"github.com/hellofresh/klepto/pkg/config"
"github.com/hellofresh/klepto/pkg/dumper"
_ "github.com/hellofresh/klepto/pkg/dumper/mysql"
"github.com/hellofresh/klepto/pkg/reader"
_ "github.com/hellofresh/klepto/pkg/reader/mysql"
"github.com/stretchr/testify/suite"
)
type MysqlTestSuite struct {
suite.Suite
rootDSN string
rootConnection *sql.DB
databases []string
timeout time.Duration
}
func TestMysqlTestSuite(t *testing.T) {
s := &MysqlTestSuite{timeout: time.Second * 3}
suite.Run(t, s)
}
func (s *MysqlTestSuite) TestExample() {
readDSN := s.createDatabase("simple")
dumpDSN := s.createDatabase("simple_dump")
s.loadFixture(readDSN, "mysql_simple.sql")
rdr, err := reader.Connect(reader.ConnOpts{DSN: readDSN, Timeout: s.timeout})
s.Require().NoError(err, "Unable to create reader")
defer func() {
err := rdr.Close()
s.Assert().NoError(err)
}()
dmp, err := dumper.NewDumper(dumper.ConnOpts{DSN: dumpDSN}, rdr)
s.Require().NoError(err, "Unable to create dumper")
defer func() {
err := dmp.Close()
s.Assert().NoError(err)
}()
done := make(chan struct{})
defer close(done)
s.Require().NoError(dmp.Dump(done, config.Tables{}, 4), "Failed to dump")
<-done
s.assertDatabaseAreTheSame(readDSN, dumpDSN)
}
func (s *MysqlTestSuite) SetupSuite() {
rootDSN, ok := os.LookupEnv("TEST_MYSQL")
if !ok {
s.T().Skip("TEST_MYSQL env is not defined")
}
rootCfg, err := mysql.ParseDSN(rootDSN)
s.Require().NoError(err, "TEST_MYSQL failed to parse")
rootCfg.MultiStatements = true
s.rootDSN = rootCfg.FormatDSN()
s.rootConnection, err = sql.Open("mysql", rootDSN)
s.Require().NoError(err, "Failed to connect to mysql")
s.Require().NoError(s.rootConnection.Ping(), "Failed to ping mysql")
}
func (s *MysqlTestSuite) TearDownSuite() {
for _, db := range s.databases {
s.dropDatabase(db)
}
err := s.rootConnection.Close()
s.Assert().NoError(err)
}
func (s *MysqlTestSuite) createDatabase(name string) string {
s.databases = append(s.databases, name)
s.dropDatabase(name)
_, err := s.rootConnection.Exec(fmt.Sprintf("CREATE DATABASE %s", name))
s.Require().NoError(err, "Unable to create db")
dbURL, _ := mysql.ParseDSN(s.rootDSN)
dbURL.DBName = name
return dbURL.FormatDSN()
}
func (s *MysqlTestSuite) dropDatabase(name string) {
_, err := s.rootConnection.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", name))
s.NoError(err, "Unable to drop db")
}
func (s *MysqlTestSuite) loadFixture(dsn string, file string) {
data, err := ioutil.ReadFile(path.Join("../fixtures/", file))
s.Require().NoError(err, "Unable to load fixture file")
conn, err := sql.Open("mysql", dsn)
defer func() {
err := conn.Close()
s.Assert().NoError(err)
}()
s.Require().NoError(err, "Unable to open db connection to load fixture")
_, err = conn.Exec(string(data))
s.Require().NoError(err, "Unable to execute fixture")
}
func (s *MysqlTestSuite) assertDatabaseAreTheSame(expectedDSN string, dumpDSN string) {
sourceConn, err := sql.Open("mysql", expectedDSN)
s.Require().NoError(err, "Unable to connect to source db")
defer func() {
err := sourceConn.Close()
s.Assert().NoError(err)
}()
targetConn, err := sql.Open("mysql", dumpDSN)
s.Require().NoError(err, "Unable to connect to target db")
defer func() {
err := targetConn.Close()
s.Assert().NoError(err)
}()
tables := s.fetchTableRowCount(sourceConn)
s.Require().Equal(tables, s.fetchTableRowCount(targetConn))
for _, table := range tables {
s.compareTable(sourceConn, targetConn, table.name, table.columnCount)
}
}
func (s *MysqlTestSuite) fetchTableRowCount(db *sql.DB) []tableInfo {
tableRows, err := db.Query(
`SELECT
t.TABLE_NAME AS name,
SUM(t.TABLE_ROWS) AS count,
COUNT(c.COLUMN_NAME) AS columnCount
FROM information_schema.TABLES AS t
LEFT JOIN information_schema.COLUMNS AS c ON
c.TABLE_SCHEMA = t.TABLE_SCHEMA AND
c.TABLE_NAME = t.TABLE_NAME
WHERE t.TABLE_SCHEMA = DATABASE()
GROUP BY t.TABLE_NAME`,
)
s.Require().NoError(err, "Unable to fetch table info")
defer tableRows.Close()
var tables []tableInfo
for tableRows.Next() {
table := tableInfo{}
s.Require().NoError(
tableRows.Scan(&table.name, &table.count, &table.columnCount),
"Unable to fetch table info row",
)
tables = append(tables, table)
}
return tables
}
func (s *MysqlTestSuite) compareTable(source *sql.DB, target *sql.DB, table string, columnCount int) {
assert := s.Require()
query := fmt.Sprintf("SELECT * FROM %s", table)
expectedRows, err := source.Query(query)
assert.NoError(err, "Unable to query source table")
defer func() {
err := expectedRows.Close()
s.Assert().NoError(err)
}()
rows, err := target.Query(query)
assert.NoError(err, "Unable to query target table")
defer func() {
err := rows.Close()
s.Assert().NoError(err)
}()
for expectedRows.Next() {
assert.True(rows.Next(), "target row mismatch")
expectedFields := make([]interface{}, columnCount)
targetFields := make([]interface{}, columnCount)
for i := 0; i < columnCount; i++ {
var sourceValue interface{}
expectedFields[i] = &sourceValue
var targetValue interface{}
targetFields[i] = &targetValue
}
assert.NoError(expectedRows.Scan(expectedFields...), "failed to fetch expected rows")
assert.NoError(rows.Scan(targetFields...), "failed to fetch target rows")
assert.Equal(expectedFields, targetFields)
}
}