Skip to content

Commit 3d9c549

Browse files
committed
Refactor openTempSQLite to return only *sql.DB and update all test cases accordingly
1 parent dece3c8 commit 3d9c549

3 files changed

Lines changed: 23 additions & 28 deletions

File tree

dialects_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
package dmorph_test
55

66
import (
7-
"regexp"
87
"testing"
98

109
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1111

1212
"github.com/AlphaOne1/dmorph"
1313
)
@@ -30,8 +30,6 @@ func TestDialectStatements(t *testing.T) {
3030
{name: "SQLite", caller: dmorph.DialectSQLite},
3131
}
3232

33-
re := regexp.MustCompile("%s")
34-
3533
for k, v := range tests {
3634
d := v.caller()
3735

@@ -40,28 +38,25 @@ func TestDialectStatements(t *testing.T) {
4038
}
4139
assert.Contains(t, d.CreateTemplate, "%s",
4240
"no table name placeholder in create template for", v.name)
43-
assert.Regexp(t, re, d.CreateTemplate)
4441

4542
if len(d.AppliedTemplate) < 10 {
4643
t.Errorf("%v: applied template is too short for %v", k, v.name)
4744
}
4845
assert.Contains(t, d.AppliedTemplate, "%s",
4946
"no table name placeholder in applied template for", v.name)
50-
assert.Regexp(t, re, d.AppliedTemplate)
5147

5248
if len(d.RegisterTemplate) < 10 {
5349
t.Errorf("%v: register template is too short for %v", k, v.name)
5450
}
5551
assert.Contains(t, d.RegisterTemplate, "%s",
5652
"no table name placeholder in register template for", v.name)
57-
assert.Regexp(t, re, d.RegisterTemplate)
5853
}
5954
}
6055

6156
// TestCallsOnClosedDB verifies that methods fail as expected when called on a closed database connection.
6257
func TestCallsOnClosedDB(t *testing.T) {
63-
db, _ := openTempSQLite(t)
64-
db.Close()
58+
db := openTempSQLite(t)
59+
require.NoError(t, db.Close())
6560

6661
assert.Error(t,
6762
dmorph.DialectSQLite().EnsureMigrationTableExists(t.Context(), db, "irrelevant"),
@@ -82,7 +77,7 @@ func TestEnsureMigrationTableExistsSQLError(t *testing.T) {
8277
)`,
8378
}
8479

85-
db, _ := openTempSQLite(t)
80+
db := openTempSQLite(t)
8681

8782
assert.Error(t, dialect.EnsureMigrationTableExists(t.Context(), db, "test"), "expected error")
8883
}
@@ -108,11 +103,11 @@ func TestEnsureMigrationTableExistsCommitError(t *testing.T) {
108103
DELETE FROM t0 WHERE id = 1;`,
109104
}
110105

111-
db, _ := openTempSQLite(t)
106+
db := openTempSQLite(t)
112107

113108
_, execErr := db.ExecContext(t.Context(), "PRAGMA foreign_keys = ON")
114109

115-
assert.NoError(t, execErr, "foreign keys checking could not be enabled")
110+
require.NoError(t, execErr, "foreign keys checking could not be enabled")
116111

117112
assert.Error(t, dialect.EnsureMigrationTableExists(t.Context(), db, "test"), "expected error")
118113
}

file_migration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
func TestWithMigrationFromFile(t *testing.T) {
19-
db, _ := openTempSQLite(t)
19+
db := openTempSQLite(t)
2020

2121
runErr := dmorph.Run(t.Context(),
2222
db,
@@ -27,7 +27,7 @@ func TestWithMigrationFromFile(t *testing.T) {
2727
}
2828

2929
func TestWithMigrationFromFileError(t *testing.T) {
30-
db, _ := openTempSQLite(t)
30+
db := openTempSQLite(t)
3131

3232
runErr := dmorph.Run(t.Context(),
3333
db,
@@ -52,7 +52,7 @@ func TestMigrationFromFileFSError(t *testing.T) {
5252

5353
// TestApplyStepsStreamError tests error handling in applyStepsStream.
5454
func TestApplyStepsStreamError(t *testing.T) {
55-
db, _ := openTempSQLite(t)
55+
db := openTempSQLite(t)
5656

5757
buf := bytes.Buffer{}
5858
buf.WriteString("utter nonsense")

migration_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func prepareDB() (string, error) {
3939
return result, nil
4040
}
4141

42-
func openTempSQLite(t *testing.T) (*sql.DB, string) {
42+
func openTempSQLite(t *testing.T) *sql.DB {
4343
t.Helper()
4444

4545
dbFile, err := prepareDB()
@@ -50,12 +50,12 @@ func openTempSQLite(t *testing.T) (*sql.DB, string) {
5050
require.NoError(t, dbErr, "DB could not be opened")
5151
t.Cleanup(func() { _ = db.Close() })
5252

53-
return db, dbFile
53+
return db
5454
}
5555

5656
// TestMigration tests the happy flow.
5757
func TestMigration(t *testing.T) {
58-
db, _ := openTempSQLite(t)
58+
db := openTempSQLite(t)
5959

6060
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
6161

@@ -71,7 +71,7 @@ func TestMigration(t *testing.T) {
7171

7272
// TestMigrationUpdate tests the happy flow of updating on existing migrations.
7373
func TestMigrationUpdate(t *testing.T) {
74-
db, _ := openTempSQLite(t)
74+
db := openTempSQLite(t)
7575

7676
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
7777

@@ -103,7 +103,7 @@ func (m TestMigrationImpl) Migrate(ctx context.Context, tx *sql.Tx) error {
103103

104104
// TestWithMigrations tests the adding of migrations using WithMigrations.
105105
func TestWithMigrations(t *testing.T) {
106-
db, _ := openTempSQLite(t)
106+
db := openTempSQLite(t)
107107

108108
runErr := dmorph.Run(t.Context(),
109109
db,
@@ -123,7 +123,7 @@ func TestMigrationUnableToCreateMorpher(t *testing.T) {
123123

124124
// TestMigrationTooOld tests what happens if the applied migrations are too old.
125125
func TestMigrationTooOld(t *testing.T) {
126-
db, _ := openTempSQLite(t)
126+
db := openTempSQLite(t)
127127

128128
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
129129

@@ -146,7 +146,7 @@ func TestMigrationTooOld(t *testing.T) {
146146

147147
// TestMigrationUnrelated0 tests what happens if the applied migrations are unrelated to existing ones.
148148
func TestMigrationUnrelated0(t *testing.T) {
149-
db, _ := openTempSQLite(t)
149+
db := openTempSQLite(t)
150150

151151
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
152152

@@ -169,7 +169,7 @@ func TestMigrationUnrelated0(t *testing.T) {
169169

170170
// TestMigrationUnrelated1 tests what happens if the applied migrations are unrelated to existing ones.
171171
func TestMigrationUnrelated1(t *testing.T) {
172-
db, _ := openTempSQLite(t)
172+
db := openTempSQLite(t)
173173

174174
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
175175

@@ -193,7 +193,7 @@ func TestMigrationUnrelated1(t *testing.T) {
193193
// TestMigrationAppliedUnordered tests the case, that somehow the migrations in the
194194
// database are registered not in the order of their keys.
195195
func TestMigrationAppliedUnordered(t *testing.T) {
196-
db, _ := openTempSQLite(t)
196+
db := openTempSQLite(t)
197197

198198
migrationsDir, migrationsDirErr := fs.Sub(testMigrationsDir, "testData")
199199

@@ -380,7 +380,7 @@ func TestMigrationRunInvalid(t *testing.T) {
380380
// TestMigrationRunInvalidCreate tests the behavior of running a migration
381381
// with an invalid CreateTemplate in the dialect.
382382
func TestMigrationRunInvalidCreate(t *testing.T) {
383-
db, _ := openTempSQLite(t)
383+
db := openTempSQLite(t)
384384

385385
dialect := dmorph.DialectSQLite()
386386
dialect.CreateTemplate = "utter nonsense"
@@ -398,7 +398,7 @@ func TestMigrationRunInvalidCreate(t *testing.T) {
398398

399399
// TestMigrationRunInvalidApplied tests the failure scenario where the AppliedTemplate of the dialect is invalid.
400400
func TestMigrationRunInvalidApplied(t *testing.T) {
401-
db, _ := openTempSQLite(t)
401+
db := openTempSQLite(t)
402402

403403
dialect := dmorph.DialectSQLite()
404404
dialect.AppliedTemplate = "utter nonsense"
@@ -416,7 +416,7 @@ func TestMigrationRunInvalidApplied(t *testing.T) {
416416

417417
// TestMigrationApplyInvalidDB verifies that applying migrations to an invalid or closed database results in an error.
418418
func TestMigrationApplyInvalidDB(t *testing.T) {
419-
db, _ := openTempSQLite(t)
419+
db := openTempSQLite(t)
420420

421421
morpher, morpherErr := dmorph.NewMorpher(
422422
dmorph.WithDialect(dmorph.DialectSQLite()),
@@ -431,7 +431,7 @@ func TestMigrationApplyInvalidDB(t *testing.T) {
431431

432432
// TestMigrationApplyUnableRegister tests the behavior when the migration registration fails due to an invalid template.
433433
func TestMigrationApplyUnableRegister(t *testing.T) {
434-
db, _ := openTempSQLite(t)
434+
db := openTempSQLite(t)
435435

436436
morpher, morpherErr := dmorph.NewMorpher(
437437
dmorph.WithDialect(dmorph.DialectSQLite()),
@@ -453,7 +453,7 @@ func TestMigrationApplyUnableRegister(t *testing.T) {
453453
// TestMigrationApplyUnableCommit tests the scenario where a migration application fails
454454
// due to inability to commit a transaction.
455455
func TestMigrationApplyUnableCommit(t *testing.T) {
456-
db, _ := openTempSQLite(t)
456+
db := openTempSQLite(t)
457457

458458
morpher, morpherErr := dmorph.NewMorpher(
459459
dmorph.WithDialect(dmorph.DialectSQLite()),

0 commit comments

Comments
 (0)