-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialects_test.go
More file actions
161 lines (128 loc) · 4.38 KB
/
dialects_test.go
File metadata and controls
161 lines (128 loc) · 4.38 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
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
// Copyright the DMorph contributors.
// SPDX-License-Identifier: MPL-2.0
package dmorph_test
import (
"database/sql"
"os"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/AlphaOne1/dmorph"
)
// TestDialectStatements verifies that each database dialect has valid and
// sufficiently complete SQL statement templates.
func TestDialectStatements(t *testing.T) {
// we cannot run tests against all databases, but at least we can test
// that the statements for the databases are somehow filled
tests := []struct {
name string
caller func() dmorph.BaseDialect
}{
{name: "CSVQ", caller: dmorph.DialectCSVQ},
{name: "DB2", caller: dmorph.DialectDB2},
{name: "MSSQL", caller: dmorph.DialectMSSQL},
{name: "MySQL", caller: dmorph.DialectMySQL},
{name: "Oracle", caller: dmorph.DialectOracle},
{name: "Postgres", caller: dmorph.DialectPostgres},
{name: "SQLite", caller: dmorph.DialectSQLite},
}
re := regexp.MustCompile("%s")
for k, v := range tests {
d := v.caller()
if len(d.CreateTemplate) < 10 {
t.Errorf("%v: create template is too short for %v", k, v.name)
}
assert.Contains(t, d.CreateTemplate, "%s",
"no table name placeholder in create template for", v.name)
assert.Regexp(t, re, d.CreateTemplate)
if len(d.AppliedTemplate) < 10 {
t.Errorf("%v: applied template is too short for %v", k, v.name)
}
assert.Contains(t, d.AppliedTemplate, "%s",
"no table name placeholder in applied template for", v.name)
assert.Regexp(t, re, d.AppliedTemplate)
if len(d.RegisterTemplate) < 10 {
t.Errorf("%v: register template is too short for %v", k, v.name)
}
assert.Contains(t, d.RegisterTemplate, "%s",
"no table name placeholder in register template for", v.name)
assert.Regexp(t, re, d.RegisterTemplate)
}
}
// TestCallsOnClosedDB verifies that methods fail as expected when called on a closed database connection.
func TestCallsOnClosedDB(t *testing.T) {
dbFile, dbFileErr := prepareDB()
if dbFileErr != nil {
t.Errorf("DB file could not be created: %v", dbFileErr)
} else {
defer func() { _ = os.Remove(dbFile) }()
}
db, dbErr := sql.Open("sqlite", dbFile)
if dbErr != nil {
t.Errorf("DB file could not be created: %v", dbErr)
} else {
_ = db.Close()
}
assert.Error(t,
dmorph.DialectSQLite().EnsureMigrationTableExists(db, "irrelevant"),
"expected error on closed database")
_, err := dmorph.DialectSQLite().AppliedMigrations(db, "irrelevant")
assert.Error(t, err, "expected error on closed database")
}
// TestEnsureMigrationTableExistsSQLError tests the EnsureMigrationTableExists function
// for handling SQL errors during execution.
func TestEnsureMigrationTableExistsSQLError(t *testing.T) {
d := dmorph.BaseDialect{
CreateTemplate: `
CRATE TABLE test (
id VARCHAR(255) PRIMARY KEY,
create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`,
}
dbFile, dbFileErr := prepareDB()
if dbFileErr != nil {
t.Errorf("DB file could not be created: %v", dbFileErr)
} else {
defer func() { _ = os.Remove(dbFile) }()
}
db, dbErr := sql.Open("sqlite", dbFile)
if dbErr != nil {
t.Errorf("DB file could not be created: %v", dbErr)
} else {
defer func() { _ = db.Close() }()
}
assert.Error(t, d.EnsureMigrationTableExists(db, "test"), "expected error")
}
// TestEnsureMigrationTableExistsCommitError tests the behavior of EnsureMigrationTableExists
// when a commit error occurs.
func TestEnsureMigrationTableExistsCommitError(t *testing.T) {
d := dmorph.BaseDialect{
CreateTemplate: `
CREATE TABLE t0 (
id INTEGER PRIMARY KEY
);
CREATE TABLE t1 (
id INTEGER PRIMARY KEY,
parent_id INTEGER REFERENCES t0 (id) DEFERRABLE INITIALLY DEFERRED
);
INSERT INTO t0 (id) VALUES (1);
INSERT INTO t1 (id, parent_id) VALUES (1, 1);
-- %s catching argument
DELETE FROM t0 WHERE id = 1;`,
}
dbFile, dbFileErr := prepareDB()
if dbFileErr != nil {
t.Errorf("DB file could not be created: %v", dbFileErr)
} else {
defer func() { _ = os.Remove(dbFile) }()
}
db, dbErr := sql.Open("sqlite", dbFile)
if dbErr != nil {
t.Errorf("DB file could not be created: %v", dbErr)
} else {
defer func() { _ = db.Close() }()
}
_, execErr := db.Exec("PRAGMA foreign_keys = ON")
assert.NoError(t, execErr, "foreign keys checking could not be enabled")
assert.Error(t, d.EnsureMigrationTableExists(db, "test"), "expected error")
}