forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader_test.go
128 lines (120 loc) · 4.02 KB
/
reader_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
package postgres
import (
"fmt"
"strings"
"testing"
"time"
"github.com/compose/transporter/client"
"github.com/compose/transporter/message"
)
var (
readerTestData = &TestData{"reader_test", "reader_test_table", basicSchema, 10}
)
func TestRead(t *testing.T) {
if testing.Short() {
t.Skip("skipping Read in short mode")
}
reader := newReader()
readFunc := reader.Read(map[string]client.MessageSet{}, func(table string) bool {
if strings.HasPrefix(table, "information_schema.") || strings.HasPrefix(table, "pg_catalog.") {
return false
}
return table == fmt.Sprintf("public.%s", readerTestData.Table)
})
done := make(chan struct{})
c, err := NewClient(WithURI(fmt.Sprintf("postgres://postgres@transporter-db:5432/%s?sslmode=disable", readerTestData.DB)))
if err != nil {
t.Fatalf("unable to initialize connection to postgres, %s", err)
}
defer c.Close()
s, err := c.Connect()
if err != nil {
t.Fatalf("unable to obtain session to postgres, %s", err)
}
msgChan, err := readFunc(s, done)
if err != nil {
t.Fatalf("unexpected Read error, %s\n", err)
}
var numMsgs int
for range msgChan {
numMsgs++
}
if numMsgs != readerTestData.InsertCount {
t.Errorf("bad message count, expected %d, got %d\n", readerTestData.InsertCount, numMsgs)
}
close(done)
}
var (
readerComplexTestData = &TestData{"reader_complex_test", "reader_complex_test_table", complexSchema, 10}
)
func TestReadComplex(t *testing.T) {
if testing.Short() {
t.Skip("skipping Read in short mode")
}
reader := newReader()
readFunc := reader.Read(map[string]client.MessageSet{}, func(table string) bool {
if strings.HasPrefix(table, "information_schema.") || strings.HasPrefix(table, "pg_catalog.") {
return false
}
return table == fmt.Sprintf("public.%s", readerComplexTestData.Table)
})
done := make(chan struct{})
c, err := NewClient(WithURI(fmt.Sprintf("postgres://postgres@transporter-db:5432/%s?sslmode=disable", readerComplexTestData.DB)))
if err != nil {
t.Fatalf("unable to initialize connection to postgres, %s", err)
}
defer c.Close()
s, err := c.Connect()
if err != nil {
t.Fatalf("unable to obtain session to postgres, %s", err)
}
msgChan, err := readFunc(s, done)
if err != nil {
t.Fatalf("unexpected Read error, %s\n", err)
}
msgs := make([]message.Msg, 0)
for msg := range msgChan {
msgs = append(msgs, msg.Msg)
}
if len(msgs) != readerComplexTestData.InsertCount {
t.Errorf("bad message count, expected %d, got %d\n", readerComplexTestData.InsertCount, len(msgs))
}
for i := 0; i < readerTestData.InsertCount; i++ {
for key, value := range map[string]interface{}{
"id": int64(i),
"colvar": randomHeros[i%len(randomHeros)],
"colbigint": int64(4000001240124),
"colbit": "1",
"colboolean": false,
"colbox": "(20,20),(10,10)",
"colcharacter": "a",
"colcidr": "10.0.1.0/28",
"colcircle": "<(5,10),3>",
"coldoubleprecision": 0.314259892323,
"colenum": "sad",
"colinet": "10.0.1.0",
"colinteger": int64(3),
"colline": "{1,1,3}",
"collseg": "[(10,10),(25,25)]",
"colmacaddr": "08:00:2b:01:02:03",
"colmoney": 35.68,
"colnumeric": 0.23509838,
"colpath": "[(10,10),(20,20),(20,10),(15,15)]",
"colpg_lsn": "0/3000000",
"colpoint": "(15,15)",
"colpolygon": "((10,10),(11,11),(11,0),(5,5))",
"colreal": float64(7),
"colsmallint": int64(3),
"coltext": "this is \\n extremely important",
"coltime": time.Date(0, 1, 1, 13, 45, 0, 0, time.UTC),
"coltsquery": "'fat':AB & 'cat'",
"coluuid": "f0a0da24-4068-4be4-961d-7c295117ccca",
"colxml": "<person><name>Batman</name></person>",
} {
if msgs[i].Data().Get(key) != value {
t.Fatalf("Expected %v of row to equal %v (%T), but was %v (%T)", key, value, value, msgs[i].Data().Get(key), msgs[i].Data().Get(key))
}
}
}
close(done)
}