This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate_test.go
110 lines (85 loc) · 2.04 KB
/
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gondolier
import (
"testing"
)
type testModelA struct {
Id uint64 `gondolier:"type:bigint;primarykey;notnull"`
Name string `gondolier:"type:character varying(100)"`
B uint64 `gondolier:"type:bigint;fk(testModelB)"`
}
type testModelB struct {
Id uint64 `gondolier:"type:bigint;primarykey;notnull"`
Age uint `gondolier:"type:integer"`
}
type dummyMigrator struct {
models []MetaModel
drop []string
}
func (m *dummyMigrator) Migrate(metaModels []MetaModel) {
m.models = metaModels
}
func (m *dummyMigrator) DropTable(name string) {
m.drop = append(m.drop, name)
}
type dummyCase struct{}
func (n *dummyCase) Get(name string) string {
return "works"
}
func TestUse(t *testing.T) {
if migrator != nil {
t.Fatal("No migrator must be selected")
}
Use(testdb, &Postgres{})
if migrator == nil {
t.Fatal("Postgres must be selected")
}
}
func testNaming(t *testing.T) {
Naming(&dummyCase{})
if naming.Get("") != "works" {
t.Fatal("Name schema must have been set")
}
}
func TestNamingNotNil(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("Naming must panic if nil was passed")
}
}()
Naming(nil)
}
func TestModel(t *testing.T) {
Model(&testModelA{}, testModelB{}, &testModelB{})
if len(metaModels) != 2 {
t.Fatal("Two models must have been added")
}
}
func TestMigrate(t *testing.T) {
dummy := &dummyMigrator{}
Use(testdb, dummy)
Model(testModelA{}, testModelB{})
Migrate()
if len(dummy.models) != 2 {
t.Fatal("Translate must have been called")
}
}
func TestMigrateNoMigrator(t *testing.T) {
migrator = nil
defer func() {
if r := recover(); r == nil {
t.Fatal("Migrate must panic if no migrator was selected")
}
}()
Migrate()
}
func TestDrop(t *testing.T) {
dummy := &dummyMigrator{}
Use(testdb, dummy)
Drop(testModelA{}, testModelB{})
if len(dummy.drop) != 2 {
t.Fatal("Two models must have been dropped")
}
if dummy.drop[0] != "testModelA" || dummy.drop[1] != "testModelB" {
t.Fatalf("Two tables must have been dropped, but was %v %v", dummy.drop[0], dummy.drop[1])
}
}