forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlcipher_test.go
120 lines (106 loc) · 2.7 KB
/
sqlcipher_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 sqlcipher
import (
"database/sql"
"fmt"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/golang-migrate/migrate/v4"
dt "github.com/golang-migrate/migrate/v4/database/testing"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/mutecomm/go-sqlcipher/v4"
)
func Test(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s", filepath.Join(dir, "sqlite3.db"))
d, err := p.Open(addr)
if err != nil {
t.Fatal(err)
}
dt.Test(t, d, []byte("CREATE TABLE t (Qty int, Name string);"))
}
func TestMigrate(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
if err != nil {
return
}
defer func() {
if err := db.Close(); err != nil {
return
}
}()
driver, err := WithInstance(db, &Config{})
if err != nil {
t.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./examples/migrations",
"ql", driver)
if err != nil {
t.Fatal(err)
}
dt.TestMigrate(t, m)
}
func TestMigrationTable(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
db, err := sql.Open("sqlite3", filepath.Join(dir, "sqlite3.db"))
if err != nil {
return
}
defer func() {
if err := db.Close(); err != nil {
return
}
}()
config := &Config{
MigrationsTable: "my_migration_table",
}
driver, err := WithInstance(db, config)
if err != nil {
t.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./examples/migrations",
"ql", driver)
if err != nil {
t.Fatal(err)
}
t.Log("UP")
err = m.Up()
if err != nil {
t.Fatal(err)
}
_, err = db.Query(fmt.Sprintf("SELECT * FROM %s", config.MigrationsTable))
if err != nil {
t.Fatal(err)
}
}
func TestNoTxWrap(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=true", filepath.Join(dir, "sqlite3.db"))
d, err := p.Open(addr)
if err != nil {
t.Fatal(err)
}
// An explicit BEGIN statement would ordinarily fail without x-no-tx-wrap.
// (Transactions in sqlite may not be nested.)
dt.Test(t, d, []byte("BEGIN; CREATE TABLE t (Qty int, Name string); COMMIT;"))
}
func TestNoTxWrapInvalidValue(t *testing.T) {
dir := t.TempDir()
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
p := &Sqlite{}
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite3.db"))
_, err := p.Open(addr)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "x-no-tx-wrap")
assert.Contains(t, err.Error(), "invalid syntax")
}
}