Skip to content

Commit 663aa59

Browse files
committed
More tests.
1 parent 5c41eb0 commit 663aa59

3 files changed

Lines changed: 106 additions & 3 deletions

File tree

dialects_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dmorph
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestDialectStatements(t *testing.T) {
9+
// we cannot run tests against all databases, but at least we can test
10+
// that the statements for the databases are somehow filled
11+
tests := []struct {
12+
name string
13+
caller func() BaseDialect
14+
}{
15+
{name: "DB2", caller: DialectDB2},
16+
{name: "MSSQL", caller: DialectMSSQL},
17+
{name: "MySQL", caller: DialectMySQL},
18+
{name: "Oracle", caller: DialectOracle},
19+
{name: "Postgres", caller: DialectPostgres},
20+
{name: "SQLite", caller: DialectSQLite},
21+
}
22+
23+
for k, v := range tests {
24+
d := v.caller()
25+
26+
if len(d.CreateTemplate) < 10 {
27+
t.Errorf("%v: create template is too short for %v", k, v.name)
28+
}
29+
assert.Contains(t, d.CreateTemplate, "%s",
30+
"no table name placeholder in create template for", v.name)
31+
32+
if len(d.AppliedTemplate) < 10 {
33+
t.Errorf("%v: applied template is too short for %v", k, v.name)
34+
}
35+
assert.Contains(t, d.AppliedTemplate, "%s",
36+
"no table name placeholder in applied template for", v.name)
37+
38+
if len(d.RegisterTemplate) < 10 {
39+
t.Errorf("%v: register template is too short for %v", k, v.name)
40+
}
41+
assert.Contains(t, d.RegisterTemplate, "%s",
42+
"no table name placeholder in register template for", v.name)
43+
}
44+
}

file_migration_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package dmorph
2+
3+
import (
4+
"database/sql"
5+
"io/fs"
6+
"os"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestWithMigrationFromFile(t *testing.T) {
13+
dbFile, dbFileErr := prepareDB()
14+
15+
if dbFileErr != nil {
16+
t.Errorf("DB file could not be created: %v", dbFileErr)
17+
} else {
18+
defer func() { _ = os.Remove(dbFile) }()
19+
}
20+
21+
db, dbErr := sql.Open("sqlite", dbFile)
22+
23+
if dbErr != nil {
24+
t.Errorf("DB file could not be created: %v", dbErr)
25+
} else {
26+
defer func() { _ = db.Close() }()
27+
}
28+
29+
runErr := Run(db,
30+
WithDialect(DialectSQLite()),
31+
WithMigrationFromFile("testData/01_base_table.sql"))
32+
33+
assert.NoError(t, runErr, "did not expect an error")
34+
}
35+
36+
func TestWithMigrationFromFileError(t *testing.T) {
37+
dbFile, dbFileErr := prepareDB()
38+
39+
if dbFileErr != nil {
40+
t.Errorf("DB file could not be created: %v", dbFileErr)
41+
} else {
42+
defer func() { _ = os.Remove(dbFile) }()
43+
}
44+
45+
db, dbErr := sql.Open("sqlite", dbFile)
46+
47+
if dbErr != nil {
48+
t.Errorf("DB file could not be created: %v", dbErr)
49+
} else {
50+
defer func() { _ = db.Close() }()
51+
}
52+
53+
runErr := Run(db,
54+
WithDialect(DialectSQLite()),
55+
WithMigrationFromFile("testData/00_non_existent.sql"))
56+
57+
var pathErr *fs.PathError
58+
assert.ErrorAs(t, runErr, &pathErr, "unexpected error")
59+
}

migration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestMigration(t *testing.T) {
5353

5454
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
5555

56-
assert.Nil(t, migrationsDirErr, "migrations directory could not be opened")
56+
assert.NoError(t, migrationsDirErr, "migrations directory could not be opened")
5757

5858
runErr := Run(db,
5959
WithDialect(DialectSQLite()),
@@ -84,7 +84,7 @@ func TestMigrationTooOld(t *testing.T) {
8484

8585
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
8686

87-
assert.Nil(t, migrationsDirErr, "migrations directory could not be opened")
87+
assert.NoError(t, migrationsDirErr, "migrations directory could not be opened")
8888

8989
runErr := Run(db,
9090
WithDialect(DialectSQLite()),
@@ -121,7 +121,7 @@ func TestMigrationUnrelated(t *testing.T) {
121121

122122
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
123123

124-
assert.Nil(t, migrationsDirErr, "migrations directory could not be opened")
124+
assert.NoError(t, migrationsDirErr, "migrations directory could not be opened")
125125

126126
runErr := Run(db,
127127
WithDialect(DialectSQLite()),

0 commit comments

Comments
 (0)