forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres_test.go
120 lines (113 loc) · 2.85 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
package integration
import (
"database/sql"
"errors"
"testing"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
// buildDB creates a new test Postgres database and halts the test if anything
// fails during this process
func buildDB(t *testing.T, opts ...option) testPostgresDB {
db, err := NewTestPostgresDB(opts...)
if err != nil {
t.Fatalf("unable to create test postgres database")
}
return db
}
func TestGetDSN(t *testing.T) {
var tests = []struct {
name string
opts []option
expected string
}{
{
name: "vanilla test database",
opts: []option{
// test will fail if default port is taken, so the port is specified
WithPort(37000),
},
expected: "host=localhost port=37000 user=postgres " +
"password=postgres sslmode=disable dbname=test-postgres-db",
},
{
name: "test with all options",
opts: []option{
WithName("some-test-db"),
WithPort(37000),
WithUser("test-user"),
WithPassword("secret"),
WithVersion("9.5.13"),
},
expected: "host=localhost port=37000 user=test-user " +
"password=secret sslmode=disable dbname=some-test-db",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
db, err := NewTestPostgresDB(test.opts...)
if err != nil {
t.Fatalf("unable to build test database")
}
if dsn := db.GetDSN(); dsn != test.expected {
t.Errorf("unexpected database connection string: have %s; want %s",
dsn, test.expected,
)
}
})
}
}
func TestCheckConnection(t *testing.T) {
db, err := NewTestPostgresDB(
WithTimeout(time.Second * 5),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
db.host = "some-fake-hostname"
if err := db.checkConnection(); err == nil {
t.Errorf("expected to get non-nil error")
}
db.host = "localhost"
rm, err := db.RunAsDockerContainer()
if err != nil {
t.Fatalf("unable to start the database container: %v", err)
}
defer rm()
}
type testTable struct {
gorm.Model
Test string
}
func TestReset(t *testing.T) {
db, err := NewTestPostgresDB()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := db.Reset(); err == nil {
t.Errorf("expected to receive an error when resetting database")
}
rm, err := db.RunAsDockerContainer()
if err != nil {
t.Fatalf("unable to start the database container: %v", err)
}
defer rm()
db.migrateUpFunction = func(*sql.DB) error {
return errors.New("intentional testing error")
}
if err := db.Reset(); err == nil {
t.Errorf("expected to receive an error when migrating database")
}
db.migrateUpFunction = func(*sql.DB) error {
orm, err := gorm.Open("postgres", db.GetDSN())
if err != nil {
t.Errorf("unable to connect to database: %v", err)
}
orm.AutoMigrate(&testTable{})
return nil
}
if err := db.Reset(); err != nil {
t.Errorf("unexpected error when resetting the database: %v", err)
}
}