This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
adaptor_test.go
74 lines (60 loc) · 1.68 KB
/
adaptor_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
package rethinkdb
import (
"os"
"testing"
"github.com/compose/transporter/log"
r "gopkg.in/gorethink/gorethink.v3"
)
var (
defaultTestClient = &Client{
uri: DefaultURI,
sessionTimeout: DefaultTimeout,
}
defaultSession *Session
dbsToTest = []*TestData{readerTestData, tailTestData, writerTestData}
)
type TestData struct {
DB string
T string
InsertCount int
}
func setup() {
log.Infoln("setting up tests")
s, err := defaultTestClient.Connect()
if err != nil {
log.Errorf("unable to initialize connection to rethinkdb, %s", err)
os.Exit(1)
}
defaultSession = s.(*Session)
for _, testData := range dbsToTest {
setupData(testData)
}
}
func setupData(data *TestData) {
if _, err := r.DBDrop(data.DB).RunWrite(defaultSession.session); err != nil {
log.Errorf("failed to drop database (%s), may affect tests!, %s", data.DB, err)
}
if _, err := r.DBCreate(data.DB).RunWrite(defaultSession.session); err != nil {
log.Errorf("failed to create database (%s), may affect tests!, %s", data.DB, err)
}
if _, err := r.DB(data.DB).TableCreate(data.T).RunWrite(defaultSession.session); err != nil {
log.Errorf("failed to create table (%s) in %s, may affect tests!, %s", data.T, data.DB, err)
}
for i := 0; i < data.InsertCount; i++ {
_, err := r.DB(data.DB).Table(data.T).Insert(map[string]interface{}{"id": i, "i": i}).RunWrite(defaultSession.session)
if err != nil {
log.Errorf("failed to write mock data, may affect tests!, %s", err)
}
}
}
func TestMain(m *testing.M) {
setup()
code := m.Run()
shutdown()
os.Exit(code)
}
func shutdown() {
log.Infoln("shutting down tests")
defaultClient.Close()
log.Infoln("tests shutdown complete")
}