-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostgres_test.go
262 lines (211 loc) · 6.45 KB
/
postgres_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package postgres
import (
"os"
"sync"
"testing"
"time"
"github.com/lib/pq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aodin/sol"
"github.com/aodin/sol/types"
)
const travisCI = "host=localhost port=5432 dbname=sol_test user=postgres sslmode=disable"
var testconn *sol.DB
var once sync.Once
// getConn returns a postgres connection pool
func getConn(t *testing.T) *sol.DB {
// Check if an ENV VAR has been set, otherwise, use travis
credentials := os.Getenv("SOL_TEST_POSTGRES")
if credentials == "" {
credentials = travisCI
}
once.Do(func() {
var err error
if testconn, err = sol.Open("postgres", credentials); err != nil {
t.Fatalf("Failed to open connection: %s", err)
}
testconn.SetMaxOpenConns(20)
})
return testconn
}
var things = sol.Table("things",
sol.Column("name", types.Varchar()),
sol.Column("created_at", Timestamp().Default(Now)),
)
var thingsWithoutNow = sol.Table("things",
sol.Column("name", types.Varchar()),
sol.Column("created_at", Timestamp()),
)
type thing struct {
Name string
CreatedAt time.Time `db:",omitempty"`
}
var itemsA = Table("items_a",
sol.Column("id", Serial()),
sol.Column("name", types.Varchar()),
)
var itemsB = Table("items_b",
sol.Column("id", Serial()),
sol.Column("name", types.Varchar()),
sol.PrimaryKey("id"),
)
var itemsFK = Table("items_fk",
sol.ForeignKey("id", itemsB, types.Integer().NotNull()),
sol.Column("name", types.Varchar()),
)
type item struct {
ID uint64 `db:",omitempty"`
Name string
}
func (i item) Exists() bool {
return i.ID != 0
}
var meetings = Table("meetings",
sol.Column("uuid", UUID().NotNull().Unique().Default(GenerateV4)),
sol.Column("time", TimestampRange()),
)
// TestPostGres performs the standard integration test
func TestPostGres(t *testing.T) {
conn := getConn(t) // TODO close
sol.IntegrationTest(t, conn, false)
}
func TestPostGres_NullTime(t *testing.T) {
conn := getConn(t) // TODO close
tx, err := conn.Begin()
require.Nil(t, err, "Creating a new transaction should not error")
defer tx.Rollback()
// TODO temp tables
require.Nil(t,
tx.Query(thingsWithoutNow.Create().Temporary().IfNotExists()),
`Create table "%s" should not error`, thingsWithoutNow.Name(),
)
type nullthing struct {
Name string
CreatedAt pq.NullTime `db:"created_at"`
}
nonzero := nullthing{
Name: "a",
CreatedAt: pq.NullTime{Valid: true, Time: time.Now()},
}
tx.Query(thingsWithoutNow.Insert().Values(nonzero))
var things []nullthing
tx.Query(thingsWithoutNow.Select(), &things)
require.Equal(t, 1, len(things))
assert.True(t, things[0].CreatedAt.Valid)
assert.False(t, things[0].CreatedAt.Time.IsZero())
}
func TestPostGres_Create(t *testing.T) {
expect := sol.NewTester(t, &PostGres{})
expect.SQL(
itemsFK.Create(),
`CREATE TABLE items_fk (
id INTEGER NOT NULL REFERENCES items_b(id),
name VARCHAR
);`,
)
}
// TestPostGres_Select tests a variety of SelectStmt features against the
// postgres database
func TestPostGres_Select(t *testing.T) {
conn := getConn(t) // TODO close
tx, err := conn.Begin()
require.Nil(t, err, "Creating a new transaction should not error")
defer tx.Rollback()
// TODO temp tables
require.Nil(t,
tx.Query(itemsA.Create().Temporary().IfNotExists()),
`Create table "%s" should not error`, itemsA.Name(),
)
require.Nil(t,
tx.Query(itemsB.Create().Temporary().IfNotExists()),
`Create table "%s" should not error`, itemsB.Name(),
)
a := item{Name: "A"}
require.Nil(t,
tx.Query(itemsA.Insert().Values(a).Returning(), &a),
`Insert into table "%s" within a transaction should not error`,
itemsA.Name(),
)
if a.ID == 0 {
t.Fatal("Failed to set primary key of item during INSERT")
}
require.Nil(t,
tx.Query(itemsB.Insert().Values([]item{{Name: "A"}, {Name: "B"}})),
`Insert into table "%s" within a transaction should not error`,
itemsB.Name(),
)
stmt := itemsB.Select().InnerJoin(
itemsA,
itemsB.C("name").Equals(itemsA.C("name")),
).Where(
itemsA.C("id").Equals(a.ID),
)
var selected item
tx.Query(stmt, &selected)
if selected.ID == 0 {
t.Fatal("Failed to SELECT item through joined table")
}
}
// TestPostGres_Transaction tests the transactional operations of PostGres,
// including Commit, Rollback, and Close
func TestPostGres_Transaction(t *testing.T) {
conn := getConn(t) // TODO close
require.Nil(t, conn.Query(things.Drop().IfExists()))
require.Nil(t,
conn.Query(things.Create()),
`Create table "things" should not error`,
)
// Start a transaction with the intent to commit
tx1, err := conn.Begin()
require.Nil(t, err, "Creating a new transaction should not error")
require.Nil(t,
tx1.Query(things.Insert().Values(thing{Name: "A"})),
`Insert into table "things" within a transaction should not error`,
)
require.Nil(t, tx1.Commit(), "Committing a transaction should not error")
var first []thing
require.Nil(t, conn.Query(things.Select(), &first))
assert.Equal(t, 1, len(first), "Thing A should have been committed")
// Start a transaction with the intent to rollback
tx2, err := conn.Begin()
require.Nil(t, err, "Creating a new transaction should not error")
require.Nil(t, tx2.Query(things.Insert().Values(thing{Name: "B"})))
require.Nil(t, tx2.Rollback(), "Transaction Rollback should not error")
var second []thing
require.Nil(t, conn.Query(things.Select(), &second))
assert.Equal(t, 1, len(second), "Thing B should not have been committed")
// Create functions that fail, panic, and are successful
func() {
tx3, err := conn.Begin()
require.Nil(t, err)
defer tx3.Close()
require.Nil(t, tx3.Query(things.Insert().Values(thing{Name: "C"})))
// Without a call to IsSuccessful, the transaction should rollback
}()
var third []thing
require.Nil(t, conn.Query(things.Select(), &third))
assert.Equal(t, 1, len(third), "Thing C should not have been committed")
func() {
tx4, err := conn.Begin()
require.Nil(t, err)
defer tx4.Close()
require.Nil(t, tx4.Query(things.Insert().Values(thing{Name: "D"})))
tx4.IsSuccessful()
}()
var fourth []thing
require.Nil(t, conn.Query(things.Select(), &fourth))
assert.Equal(t, 2, len(fourth), "Thing D should have been committed")
assert.Panics(t,
func() {
tx5, err := conn.Begin()
require.Nil(t, err)
defer tx5.Close()
require.Nil(t, tx5.Query(things.Insert().Values(thing{Name: "E"})))
panic("I'm panicking")
},
)
var fifth []thing
require.Nil(t, conn.Query(things.Select(), &fifth))
assert.Equal(t, 2, len(fifth), "Thing E should have been committed")
}