-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproxy_test.go
210 lines (179 loc) · 3.96 KB
/
proxy_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
package y
import (
"database/sql"
"fmt"
"net"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
const (
FixturedID int64 = 1
FixturedName = "foo"
)
type env func(string, string) string
type testrdb interface {
Setup(env) (string, error)
CreateTable() string
}
type mysqlrdb struct{}
func (m mysqlrdb) Setup(e env) (dsn string, err error) {
user := e("MYSQL_TEST_USER", "root")
pass := e("MYSQL_TEST_PASS", "")
prot := e("MYSQL_TEST_PROT", "tcp")
addr := e("MYSQL_TEST_ADDR", "localhost:3306")
dbname := e("MYSQL_TEST_DBNAME", "y_test")
netAddr := fmt.Sprintf("%s(%s)", prot, addr)
dsn = fmt.Sprintf("%s:%s@%s/%s?timeout=30s", user, pass, netAddr, dbname)
c, err := net.Dial(prot, addr)
if err == nil {
c.Close()
}
return
}
func (m mysqlrdb) CreateTable() string {
return `
CREATE TABLE y_test (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
name TEXT,
_version INTEGER
)`
}
type postgresrdb struct{}
func (p postgresrdb) Setup(e env) (dsn string, err error) {
SetBuilderProvider(Postgres)
user := e("PG_TEST_USER", "postgres")
pass := e("PG_TEST_PASS", "")
addr := e("PG_TEST_ADDR", "localhost:5432")
dbname := e("PG_TEST_DBNAME", "y_test")
dsn = fmt.Sprintf("postgres://%s:%s@%s/%s?connect_timeout=30&sslmode=disable", user, pass, addr, dbname)
return
}
func (p postgresrdb) CreateTable() string {
return `
CREATE TABLE y_test (
id SERIAL PRIMARY KEY,
name TEXT,
_version INTEGER
)`
}
var (
rdb testrdb
rdbtype string
rdberr error
dsn string
)
func rdbfactory(rdbtype string) (testrdb, error) {
switch rdbtype {
case "mysql":
return mysqlrdb{}, nil
case "postgres":
return postgresrdb{}, nil
}
return nil, fmt.Errorf("Unknown RDBS: %s", rdbtype)
}
func init() {
env := func(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
rdbtype = env("DB", "mysql")
rdb, rdberr = rdbfactory(rdbtype)
if rdberr == nil {
dsn, rdberr = rdb.Setup(env)
}
}
var _ = Describe("Proxy", func() {
var db *sql.DB
type Fixtured struct {
ID int64 `y:",pk,autoincr"`
Name string
}
// common setup
BeforeEach(func() {
if rdberr != nil {
Skip(fmt.Sprintf("%s server has got an error: %s", rdbtype, rdberr.Error()))
}
db, _ = sql.Open(rdbtype, dsn)
_, err := db.Exec(rdb.CreateTable())
if err != nil {
Skip(err.Error())
}
})
// common teardown
AfterEach(func() {
db.Exec("DROP TABLE y_test")
db.Close()
})
Context("when one item created", func() {
type YTest struct {
Fixtured
}
var (
err error
ytest YTest
)
BeforeEach(func() {
ytest = YTest{Fixtured{Name: FixturedName}}
_, err = New(&ytest).Put(db)
})
It("no error occurred", func() {
Expect(err).To(BeNil())
})
It("the primary key is not empty", func() {
Expect(ytest.ID).To(Equal(FixturedID))
})
})
Context("when one item loaded", func() {
type YTest struct {
Fixtured
}
var (
err error
ytest YTest
)
BeforeEach(func() {
// insert a fixture
New(&YTest{Fixtured{FixturedID, FixturedName}}).Put(db)
// load the testable bean
ytest = YTest{Fixtured{ID: FixturedID}}
err = New(&ytest).Load(db)
})
It("no error occurred", func() {
Expect(err).To(BeNil())
})
It("the name is not empty", func() {
Expect(ytest.Name).To(Equal(FixturedName))
})
})
Context("when an item updated", func() {
type YTest struct {
Fixtured
Versionable
}
var (
err error
ytest YTest
)
BeforeEach(func() {
ytest = YTest{Fixtured{FixturedID, FixturedName}, MakeVersionable(1)}
// insert a fixture
New(&ytest).Put(db)
// update the item
err = New(&ytest).Update(db, Values{"name": "bar"})
})
It("no error occurred", func() {
Expect(err).To(BeNil())
})
It("the name is changed", func() {
Expect(ytest.Name).To(Equal("bar"))
})
It("the version is updated", func() {
Expect(ytest.Version.Int64).To(Equal(int64(2)))
})
})
})