-
Notifications
You must be signed in to change notification settings - Fork 3
/
testing_test.go
139 lines (125 loc) · 3.64 KB
/
testing_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbw
import (
"context"
"database/sql"
"errors"
"os"
"strings"
"testing"
"github.com/hashicorp/go-secure-stdlib/base62"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_getTestOpts(t *testing.T) {
t.Parallel()
assert := assert.New(t)
t.Run("WithTestMigration", func(t *testing.T) {
fn := func(context.Context, string, string) error { return nil }
opts := getTestOpts(WithTestMigration(fn))
testOpts := getDefaultTestOptions()
testOpts.withTestMigration = fn
assert.NotNil(opts, testOpts.withTestMigration)
})
t.Run("WithTestDatabaseUrl", func(t *testing.T) {
opts := getTestOpts(WithTestDatabaseUrl("url"))
testOpts := getDefaultTestOptions()
testOpts.withTestDatabaseUrl = "url"
assert.Equal(opts, testOpts)
})
}
func Test_TestSetup(t *testing.T) {
testMigrationFn := func(context.Context, string, string) error {
conn, err := Open(Sqlite, "file::memory:")
require.NoError(t, err)
rw := New(conn)
_, err = rw.Exec(context.Background(), testQueryCreateTablesSqlite, nil)
require.NoError(t, err)
return nil
}
testMigrationUsingDbFn := func(_ context.Context, db *sql.DB) error {
var sql string
switch strings.ToLower(os.Getenv("DB_DIALECT")) {
case "postgres":
sql = testQueryCreateTablesPostgres
default:
sql = testQueryCreateTablesSqlite
}
_, err := db.Exec(sql)
require.NoError(t, err)
return nil
}
tests := []struct {
name string
opt []TestOption
validate func(db *DB) bool
}{
{
name: "sqlite-with-migration",
opt: []TestOption{WithTestDialect(Sqlite.String()), WithTestMigration(testMigrationFn)},
// we can't validate this, since WithTestMigration will open a new
// sqlite connection which will result in a new in-memory db which
// will only existing during the testMigrationFn... sort of silly,
// but it does test that the fn is called properly at least.
},
{
name: "sqlite-with-migration-using-db",
opt: []TestOption{WithTestDialect(Sqlite.String()), WithTestMigrationUsingDB(testMigrationUsingDbFn)},
validate: func(db *DB) bool {
rw := New(db)
publicId, err := base62.Random(20)
require.NoError(t, err)
user := &testUser{
PublicId: publicId,
}
require.NoError(t, err)
user.Name = "foo-" + user.PublicId
err = rw.Create(context.Background(), user)
require.NoError(t, err)
return true
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
db, url := TestSetup(t, tt.opt...)
if tt.validate != nil {
assert.True(tt.validate(db))
}
assert.NotNil(db)
assert.NotEmpty(url)
})
}
}
func Test_TestSetupWithMock(t *testing.T) {
assert := assert.New(t)
testCtx := context.Background()
publicId, err := base62.Random(20)
require.NoError(t, err)
user := &testUser{
PublicId: publicId,
}
db, mock := TestSetupWithMock(t)
rw := New(db)
mock.ExpectQuery(`SELECT`).WillReturnError(errors.New("failed-lookup"))
err = rw.Create(testCtx, &user)
assert.Error(err)
}
func Test_CreateDropTestTables(t *testing.T) {
t.Run("execute", func(t *testing.T) {
db, _ := TestSetup(t, WithTestDialect(Sqlite.String()))
testDropTables(t, db)
TestCreateTables(t, db)
})
}
// testUser is required since we can't import dbtest as it creates a circular dep
type testUser struct {
PublicId string `gorm:"primaryKey;default:null"`
Name string `gorm:"default:null"`
PhoneNumber string `gorm:"default:null"`
Email string `gorm:"default:null"`
Version uint32 `gorm:"default:null"`
}
func (u *testUser) TableName() string { return "db_test_user" }