-
Notifications
You must be signed in to change notification settings - Fork 27
/
postgresql_test.go
171 lines (145 loc) · 4.18 KB
/
postgresql_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
package godb_test
import (
"os"
"testing"
"time"
"github.com/samonzeweb/godb"
"github.com/samonzeweb/godb/adapters/postgresql"
"github.com/samonzeweb/godb/dbtests/common"
. "github.com/smartystreets/goconvey/convey"
)
func fixturesSetupPostgreSQL(t *testing.T) (*godb.DB, func()) {
if os.Getenv("GODB_POSTGRESQL") == "" {
t.Skip("Don't run PostgreSQL test, GODB_POSTGRESQL not set")
}
db, err := godb.Open(postgresql.Adapter, os.Getenv("GODB_POSTGRESQL"))
if err != nil {
t.Fatal(err)
}
// Enable logger if needed
//db.SetLogger(log.New(os.Stderr, "", 0))
createTable :=
`create table if not exists books (
id serial primary key,
title varchar(128) not null,
author varchar(128) not null,
published date not null,
version int not null default 0);
create table if not exists inventories (
id serial primary key,
book_id int not null,
last_inventory date,
counting int not null default 0);
`
_, err = db.CurrentDB().Exec(createTable)
if err != nil {
t.Fatal(err)
}
fixturesTeardown := func() {
dropTable := "drop table if exists books; drop table if exists inventories;"
_, err := db.CurrentDB().Exec(dropTable)
if err != nil {
t.Fatal(err)
}
err = db.Close()
if err != nil {
t.Fatal(err)
}
}
return db, fixturesTeardown
}
func TestStatementsPostgreSQL(t *testing.T) {
Convey("A DB for a PostgreSQL database", t, func() {
db, teardown := fixturesSetupPostgreSQL(t)
defer teardown()
Convey("The common statements tests must pass", func() {
common.StatementsTests(db, t)
})
})
}
func TestStructsPostgreSQL(t *testing.T) {
Convey("A DB for a PostgreSQL database", t, func() {
db, teardown := fixturesSetupPostgreSQL(t)
defer teardown()
Convey("The common structs tests must pass", func() {
common.StructsTests(db, t)
})
})
}
func TestRawPowtgreSQL(t *testing.T) {
Convey("A DB for a SQLite database", t, func() {
db, teardown := fixturesSetupPostgreSQL(t)
defer teardown()
Convey("The common raw tests must pass", func() {
common.RawSQLTests(db, t)
})
})
}
type bookWithXmin struct {
Id int `db:"id,key,auto"`
Title string `db:"title"`
Author string `db:"author"`
Published time.Time `db:"published"`
Version int `db:"version"`
Xmin int `db:"xmin,auto,oplock"`
}
func (bookWithXmin) TableName() string {
return "books"
}
func TestReturningClausePostgreSQL(t *testing.T) {
Convey("A DB for a PostgreSQL database", t, func() {
db, teardown := fixturesSetupPostgreSQL(t)
defer teardown()
Convey("The auto columns are set after a struct insert", func() {
book := bookWithXmin{
Title: "The Hobbit",
Author: "Tolkien",
Published: time.Date(1937, 9, 21, 0, 0, 0, 0, time.UTC),
Version: 1,
}
db.Insert(&book).Do()
So(book.Id, ShouldBeGreaterThan, 0)
So(book.Xmin, ShouldBeGreaterThan, 0)
})
Convey("The auto columns are updates after a struct update", func() {
book := bookWithXmin{
Title: "The Hobbit",
Author: "Tolkien",
Published: time.Date(1937, 9, 21, 0, 0, 0, 0, time.UTC),
Version: 1,
}
db.Insert(&book).Do()
previousXmin := book.Xmin
db.Update(&book).Do()
So(book.Xmin, ShouldBeGreaterThan, previousXmin)
})
})
}
func TestAutomaticOptimisticLockingPostgreSQL(t *testing.T) {
Convey("A DB for a PostgreSQL database", t, func() {
db, teardown := fixturesSetupPostgreSQL(t)
defer teardown()
Convey("A row inserted in database", func() {
book := bookWithXmin{
Title: "The Hobbit",
Author: "Tolkien",
Published: time.Date(1937, 9, 21, 0, 0, 0, 0, time.UTC),
Version: 1,
}
db.Insert(&book).Do()
Convey("Another player read the row", func() {
var sameBook bookWithXmin
db.Select(&sameBook).Where("id = ? ", book.Id).Do()
Convey("Both instances has same value for the oplock field", func() {
So(book.Xmin, ShouldEqual, sameBook.Xmin)
Convey("Both players update the row, but the second fails because oplock", func() {
err := db.Update(&book).Do()
So(err, ShouldBeNil)
err = db.Update(&sameBook).Do()
So(err, ShouldEqual, godb.ErrOpLock)
})
})
})
})
})
}