forked from polothy/pg2mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier_test.go
139 lines (120 loc) · 3.98 KB
/
verifier_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
package pg2mysql_test
import (
"fmt"
"time"
"github.com/millin/pg2mysql"
"github.com/millin/pg2mysql/pg2mysqlfakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Verifier", func() {
var (
verifier pg2mysql.Verifier
mysql pg2mysql.DB
pg pg2mysql.DB
watcher *pg2mysqlfakes.FakeVerifierWatcher
)
BeforeEach(func() {
mysql = pg2mysql.NewMySQLDB(
mysqlRunner.DBName,
"root",
"",
"127.0.0.1",
3306,
)
err := mysql.Open()
Expect(err).NotTo(HaveOccurred())
pg = pg2mysql.NewPostgreSQLDB(
pgRunner.DBName,
"",
"",
"127.0.0.1",
5432,
"disable",
)
err = pg.Open()
Expect(err).NotTo(HaveOccurred())
watcher = &pg2mysqlfakes.FakeVerifierWatcher{}
verifier = pg2mysql.NewVerifier(pg, mysql, watcher)
})
AfterEach(func() {
err := mysql.Close()
Expect(err).NotTo(HaveOccurred())
err = pg.Close()
Expect(err).NotTo(HaveOccurred())
})
Describe("Verify", func() {
It("notifies the watcher", func() {
err := verifier.Verify()
Expect(err).NotTo(HaveOccurred())
Expect(watcher.TableVerificationDidFinishCallCount()).To(Equal(3))
for i := 0; i < watcher.TableVerificationDidFinishCallCount(); i++ {
_, missingRows, missingIDs := watcher.TableVerificationDidFinishArgsForCall(i)
Expect(missingRows).To(BeZero())
Expect(missingIDs).To(BeNil())
}
})
Context("when there is data in postgres that is not in mysql", func() {
var lastInsertID int
BeforeEach(func() {
err := pgRunner.DB().QueryRow("INSERT INTO table_with_id (id, name, ci_name, created_at, truthiness) VALUES (3, 'some-name', 'some-ci-name', now(), false) RETURNING id;").Scan(&lastInsertID)
Expect(err).NotTo(HaveOccurred())
})
It("notifies the watcher", func() {
err := verifier.Verify()
Expect(err).To(HaveOccurred())
Expect(watcher.TableVerificationDidFinishCallCount()).To(Equal(3))
expected := map[string]int64{
"table_with_id": 1,
"table_with_string_id": 0,
"table_without_id": 0,
}
for i := 0; i < len(expected); i++ {
tableName, missingRows, missingIDs := watcher.TableVerificationDidFinishArgsForCall(i)
Expect(missingRows).To(Equal(expected[tableName]), fmt.Sprintf("unexpected result for %s", tableName))
if tableName == "table_with_id" {
Expect(missingIDs).To(Equal([]string{fmt.Sprintf("%d", lastInsertID)}))
} else {
Expect(missingIDs).To(BeNil())
}
}
})
})
Context("when there is data in postgres that is in mysql", func() {
BeforeEach(func() {
id := 3
name := "some-name"
ciname := "some-ci-name"
created_at := time.Now().UTC()
truthiness := true
stmt := "INSERT INTO table_with_id (id, name, ci_name, created_at, truthiness) VALUES ($1, $2, $3, $4, $5);"
result, err := pgRunner.DB().Exec(stmt, id, name, ciname, created_at, truthiness)
Expect(err).NotTo(HaveOccurred())
rowsAffected, err := result.RowsAffected()
Expect(err).NotTo(HaveOccurred())
Expect(rowsAffected).To(BeNumerically("==", 1))
stmt = "INSERT INTO table_with_id (id, name, ci_name, created_at, truthiness) VALUES (?, ?, ?, ?, ?);"
result, err = mysqlRunner.DB().Exec(stmt, id, name, ciname, created_at, truthiness)
Expect(err).NotTo(HaveOccurred())
rowsAffected, err = result.RowsAffected()
Expect(err).NotTo(HaveOccurred())
Expect(rowsAffected).To(BeNumerically("==", 1))
})
It("notifies the watcher", func() {
err := verifier.Verify()
Expect(err).NotTo(HaveOccurred())
Expect(watcher.TableVerificationDidFinishCallCount()).To(Equal(3))
expected := map[string]int64{
"table_with_id": 0,
"table_with_string_id": 0,
"table_without_id": 0,
}
for i := 0; i < len(expected); i++ {
tableName, missingRows, missingIDs := watcher.TableVerificationDidFinishArgsForCall(i)
Expect(missingRows).To(Equal(expected[tableName]), fmt.Sprintf("unexpected result for %s", tableName))
Expect(missingIDs).To(BeNil())
}
})
})
})
})