forked from xakep666/mongo-migrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
1_global_migrate_test.go
89 lines (81 loc) · 2.25 KB
/
1_global_migrate_test.go
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
package migrate
import (
"context"
"testing"
"go.mongodb.org/mongo-driver/mongo"
)
func TestGlobalMigrateSetGet(t *testing.T) {
oldMigrate := globalMigrate
defer func() {
globalMigrate = oldMigrate
}()
db := &mongo.Database{}
globalMigrate = NewMigrate(db)
if globalMigrate.db != db {
t.Errorf("Unexpected non-equal dbs")
}
db2 := &mongo.Database{}
SetDatabase(db2)
if globalMigrate.db != db2 {
t.Errorf("Unexpected non-equal dbs")
}
SetMigrationsCollection("test")
if globalMigrate.migrationsCollection != "test" {
t.Errorf("Unexpected non-equal collections")
}
}
func TestMigrationsRegistration(t *testing.T) {
oldMigrate := globalMigrate
defer func() {
globalMigrate = oldMigrate
}()
globalMigrate = NewMigrate(nil)
err := Register(func(ctx context.Context, db *mongo.Database) error {
return nil
}, func(ctx context.Context, db *mongo.Database) error {
return nil
})
if err != nil {
t.Errorf("Unexpected register error: %v", err)
return
}
registered := RegisteredMigrations()
if len(registered) <= 0 || len(registered) > 1 {
t.Errorf("Unexpected length of registered migrations")
return
}
if registered[0].Version != 1 || registered[0].Description != "global_migrate_test" {
t.Errorf("Unexpected version/description: %d %s", registered[0].Version, registered[0].Description)
}
err = Register(func(ctx context.Context, db *mongo.Database) error {
return nil
}, func(ctx context.Context, db *mongo.Database) error {
return nil
})
if err == nil {
t.Errorf("Unexpected nil error")
}
}
func TestMigrationMustRegistration(t *testing.T) {
oldMigrate := globalMigrate
defer func() {
globalMigrate = oldMigrate
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
globalMigrate = NewMigrate(nil)
MustRegister(func(ctx context.Context, db *mongo.Database) error {
return nil
}, func(ctx context.Context, db *mongo.Database) error {
return nil
})
registered := RegisteredMigrations()
if len(registered) <= 0 || len(registered) > 1 {
t.Errorf("Unexpected length of registered migrations")
return
}
if registered[0].Version != 1 || registered[0].Description != "global_migrate_test" {
t.Errorf("Unexpected version/description: %d %s", registered[0].Version, registered[0].Description)
}
}