@@ -6,11 +6,14 @@ package dmorph
66import (
77 "database/sql"
88 "os"
9+ "regexp"
910 "testing"
1011
1112 "github.com/stretchr/testify/assert"
1213)
1314
15+ // TestDialectStatements verifies that each database dialect has valid and
16+ // sufficiently complete SQL statement templates.
1417func TestDialectStatements (t * testing.T ) {
1518 // we cannot run tests against all databases, but at least we can test
1619 // that the statements for the databases are somehow filled
@@ -26,6 +29,8 @@ func TestDialectStatements(t *testing.T) {
2629 {name : "SQLite" , caller : DialectSQLite },
2730 }
2831
32+ re := regexp .MustCompile ("%s" )
33+
2934 for k , v := range tests {
3035 d := v .caller ()
3136
@@ -34,21 +39,25 @@ func TestDialectStatements(t *testing.T) {
3439 }
3540 assert .Contains (t , d .CreateTemplate , "%s" ,
3641 "no table name placeholder in create template for" , v .name )
42+ assert .Regexp (t , re , d .CreateTemplate )
3743
3844 if len (d .AppliedTemplate ) < 10 {
3945 t .Errorf ("%v: applied template is too short for %v" , k , v .name )
4046 }
4147 assert .Contains (t , d .AppliedTemplate , "%s" ,
4248 "no table name placeholder in applied template for" , v .name )
49+ assert .Regexp (t , re , d .AppliedTemplate )
4350
4451 if len (d .RegisterTemplate ) < 10 {
4552 t .Errorf ("%v: register template is too short for %v" , k , v .name )
4653 }
4754 assert .Contains (t , d .RegisterTemplate , "%s" ,
4855 "no table name placeholder in register template for" , v .name )
56+ assert .Regexp (t , re , d .RegisterTemplate )
4957 }
5058}
5159
60+ // TestCallsOnClosedDB verifies that methods fail as expected when called on a closed database connection.
5261func TestCallsOnClosedDB (t * testing.T ) {
5362 dbFile , dbFileErr := prepareDB ()
5463
@@ -74,6 +83,8 @@ func TestCallsOnClosedDB(t *testing.T) {
7483 assert .Error (t , err , "expected error on closed database" )
7584}
7685
86+ // TestEnsureMigrationTableExistsSQLError tests the EnsureMigrationTableExists function
87+ // for handling SQL errors during execution.
7788func TestEnsureMigrationTableExistsSQLError (t * testing.T ) {
7889 d := BaseDialect {
7990 CreateTemplate : `
@@ -102,6 +113,7 @@ func TestEnsureMigrationTableExistsSQLError(t *testing.T) {
102113 assert .Error (t , d .EnsureMigrationTableExists (db , "test" ), "expected error" )
103114}
104115
116+ // TestEnsureMigrationTableExistsCommitError tests the behavior of EnsureMigrationTableExists when a commit error occurs.
105117func TestEnsureMigrationTableExistsCommitError (t * testing.T ) {
106118 d := BaseDialect {
107119 CreateTemplate : `
@@ -129,13 +141,17 @@ func TestEnsureMigrationTableExistsCommitError(t *testing.T) {
129141 // defer func() { _ = os.Remove(dbFile) }()
130142 }
131143
132- db , dbErr := sql .Open ("sqlite" , "file://" + dbFile + "?_pragma=foreign_keys(1)" )
144+ db , dbErr := sql .Open ("sqlite" , dbFile )
133145
134146 if dbErr != nil {
135147 t .Errorf ("DB file could not be created: %v" , dbErr )
136148 } else {
137149 defer func () { _ = db .Close () }()
138150 }
139151
152+ _ , execErr := db .Exec ("PRAGMA foreign_keys = ON" )
153+
154+ assert .NoError (t , execErr , "foreign keys checking could not be enabled" )
155+
140156 assert .Error (t , d .EnsureMigrationTableExists (db , "test" ), "expected error" )
141157}
0 commit comments