-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_migration_test.go
More file actions
90 lines (60 loc) · 1.99 KB
/
file_migration_test.go
File metadata and controls
90 lines (60 loc) · 1.99 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
// SPDX-FileCopyrightText: 2026 The DMorph contributors.
// SPDX-License-Identifier: MPL-2.0
package dmorph_test
import (
"bytes"
"io/fs"
"log/slog"
"os"
"testing"
"github.com/AlphaOne1/dmorph"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWithMigrationFromFile(t *testing.T) {
t.Parallel()
db := openTempSQLite(t)
runErr := dmorph.Run(t.Context(),
db,
dmorph.WithDialect(dmorph.DialectSQLite()),
dmorph.WithMigrationFromFile("testData/01_base_table.sql"))
assert.NoError(t, runErr, "did not expect an error")
}
func TestWithMigrationFromFileError(t *testing.T) {
t.Parallel()
db := openTempSQLite(t)
runErr := dmorph.Run(t.Context(),
db,
dmorph.WithDialect(dmorph.DialectSQLite()),
dmorph.WithMigrationFromFile("testData/00_non_existent.sql"))
var pathErr *fs.PathError
assert.ErrorAs(t, runErr, &pathErr, "unexpected error")
}
// TestMigrationFromFileFSError validates that migrationFromFileFS returns an error
// when the specified file does not exist.
func TestMigrationFromFileFSError(t *testing.T) {
t.Parallel()
dir := os.DirFS("testData")
mig := dmorph.TmigrationFromFileFS("nonexistent", dir, slog.Default())
err := mig.Migrate(t.Context(), nil)
assert.Error(t, err, "expected error")
}
// TestApplyStepsStreamError tests error handling in applyStepsStream.
func TestApplyStepsStreamError(t *testing.T) {
t.Parallel()
db := openTempSQLite(t)
buf := bytes.Buffer{}
buf.WriteString("utter nonsense")
tx, txErr := db.BeginTx(t.Context(), nil)
require.NoError(t, txErr, "expected no tx error")
err := dmorph.TapplyStepsStream(t.Context(), tx, &buf, "test", slog.Default())
require.Error(t, err, "expected error")
_ = tx.Rollback()
tx, txErr = db.BeginTx(t.Context(), nil)
require.NoError(t, txErr, "expected no tx error")
buf.Reset()
buf.WriteString("utter nonsense\n;")
err = dmorph.TapplyStepsStream(t.Context(), tx, &buf, "test", slog.Default())
require.Error(t, err, "expected error")
_ = tx.Rollback()
}