-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_test.go
More file actions
59 lines (50 loc) · 1.94 KB
/
migrate_test.go
File metadata and controls
59 lines (50 loc) · 1.94 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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestMigrate(t *testing.T) {
t.Run("POSITIVE-MigrateSuccess", func(t *testing.T) {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
assert.NoError(t, err, "failed to open in-memory db")
err = Migrate(db)
assert.NoError(t, err)
// Check if table created
assert.True(t, db.Migrator().HasTable(&Queue{}))
})
t.Run("POSITIVE-MigrateNoOp", func(t *testing.T) {
// Open in-memory SQLite database
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
assert.NoError(t, err, "failed to open in-memory db")
// First migration should succeed
err = Migrate(db)
assert.NoError(t, err, "first migration failed")
assert.True(t, db.Migrator().HasTable(&Queue{}))
// Second migration (no-op) should also succeed
err = Migrate(db)
assert.NoError(t, err, "second migration (no-op) failed")
assert.True(t, db.Migrator().HasTable(&Queue{}))
})
t.Run("NEGATIVE-MigrateError_NilDB", func(t *testing.T) {
// Now that Migrate returns an error, we can test this more gracefully
err := Migrate(nil)
assert.Error(t, err)
})
t.Run("NEGATIVE-MigrateError_AutoMigrateFails", func(t *testing.T) {
// Use a mock dialector that will cause AutoMigrate to fail.
// This is a bit tricky as we need to simulate a failure at the GORM level.
// One way is to use a real DB but with an invalid table structure or permissions.
// For simplicity, we'll use a mock that simulates the error.
// NOTE: This requires a more advanced mocking of the gorm.DB which is complex.
// A simpler approach for this specific case is to use a closed DB connection.
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
assert.NoError(t, err)
sqlDB, _ := db.DB()
sqlDB.Close() // Close the underlying connection
err = Migrate(db)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to run migration")
})
}