forked from xakep666/mongo-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_global_migrate_test.go
88 lines (80 loc) · 2.09 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
package migrate
import (
"testing"
"github.com/globalsign/mgo"
)
func TestGlobalMigrateSetGet(t *testing.T) {
oldMigrate := globalMigrate
defer func() {
globalMigrate = oldMigrate
}()
db := &mgo.Database{}
globalMigrate = NewMigrate(db)
if globalMigrate.db != db {
t.Errorf("Unexpected non-equal dbs")
}
db2 := &mgo.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(db *mgo.Database) error {
return nil
}, func(db *mgo.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(db *mgo.Database) error {
return nil
}, func(db *mgo.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(db *mgo.Database) error {
return nil
}, func(db *mgo.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)
}
}