forked from peterldowns/pgmigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
86 lines (73 loc) · 2.17 KB
/
main_test.go
File metadata and controls
86 lines (73 loc) · 2.17 KB
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
package main
import (
"database/sql"
"testing"
"github.com/peterldowns/pgtestdb"
"github.com/peterldowns/pgtestdb/migrators/pgmigrator"
"github.com/peterldowns/testy/assert"
"github.com/geckoboard/pgmigrate"
)
// This is a helper function to open a connection to a unique, fully-isolated,
// fully-migrated database that will be deleted when the test is done.
//
// For more information, see https://github.com/peterldowns/pgtestdb
func newDB(t *testing.T) *sql.DB {
t.Helper()
logger := pgmigrate.NewTestLogger(t)
pgm, err := pgmigrator.New(migrationsFS, pgmigrator.WithLogger(logger))
assert.Nil(t, err)
db := pgtestdb.New(t, pgtestdb.Config{
DriverName: "postgres",
Host: "localhost",
User: "appuser",
Database: "testdb",
Password: "verysecret",
Port: "5436",
Options: "sslmode=disable",
}, pgm)
assert.NotEqual(t, nil, db)
return db
}
// Tests that newDB() works and the new database is queryable.
func TestWithMigratedDatabase(t *testing.T) {
t.Parallel()
db := newDB(t)
row := db.QueryRow("select 'hello world'")
assert.Nil(t, row.Err())
var message string
err := row.Scan(&message)
assert.Nil(t, err)
assert.Equal(t, "hello world", message)
}
// Tests that newDB() works and the new database has the expected schema, which
// is the result of applying the migrations.
func TestApplicationHasNoDataButSchemaIsCorrect(t *testing.T) {
t.Parallel()
db := newDB(t)
var count int
// 0 companies
row := db.QueryRow("select count(*) from companies")
assert.Nil(t, row.Err())
assert.Nil(t, row.Scan(&count))
assert.Equal(t, 0, count)
// 0 users
row = db.QueryRow("select count(*) from companies")
assert.Nil(t, row.Err())
assert.Nil(t, row.Scan(&count))
assert.Equal(t, 0, count)
// 0 blobs
row = db.QueryRow("select count(*) from blobs")
assert.Nil(t, row.Err())
assert.Nil(t, row.Scan(&count))
assert.Equal(t, 0, count)
// 3 review states
rows, err := db.Query("select value from blob_type_enum")
assert.Nil(t, err)
var types []string
for rows.Next() {
var blobtype string
assert.Nil(t, rows.Scan(&blobtype))
types = append(types, blobtype)
}
assert.Equal(t, []string{"pending_review", "approved", "rejected"}, types)
}