-
Notifications
You must be signed in to change notification settings - Fork 2
/
seeder_test.go
169 lines (140 loc) · 3.75 KB
/
seeder_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package goseeder
import (
"database/sql"
"flag"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/require"
"os"
"testing"
)
type mockStatus struct {
callCount int
callParams []interface{}
}
func newFnMock() (func(), *mockStatus) {
mockStatus := mockStatus{
callCount: 0,
callParams: []interface{}{},
}
fn := func() {
mockStatus.callCount++
}
return fn, &mockStatus
}
func newSeedFnMock() (func(s Seeder), *mockStatus) {
mockStatus := mockStatus{
callCount: 0,
callParams: []interface{}{},
}
fnSeeder := func(s Seeder) {
mockStatus.callCount++
}
return fnSeeder, &mockStatus
}
var conProvider = func() *sql.DB {
db, _, _ := sqlmock.New()
return db
}
func TestWithSeeder_not_seeding(t *testing.T) {
clientMain, mockStatus := newFnMock()
WithSeeder(conProvider, clientMain)
require.Equal(t, 1, mockStatus.callCount)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}
func TestWithSeeder_seed_all(t *testing.T) {
clientMain, mockFnStatus := newFnMock()
mockSeedFn, mockSeedStatus := newSeedFnMock()
Registration{
Name: "test_seed",
Env: "",
}.Complete(mockSeedFn)
os.Args = []string{
"",
"--gseed",
}
WithSeeder(conProvider, clientMain)
require.Equal(t, 0, mockFnStatus.callCount)
require.Equal(t, 1, mockSeedStatus.callCount)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}
func TestWithSeeder_seed_named(t *testing.T) {
clientMain, mockFnStatus := newFnMock()
mockSeedFn, mockSeedStatus := newSeedFnMock()
mockSeed2Fn, mockSeed2Status := newSeedFnMock()
Registration{
Name: "test_seed",
Env: "",
}.Complete(mockSeedFn)
Registration{
Name: "another_weird_seed",
Env: "",
}.Complete(mockSeed2Fn)
os.Args = []string{
"",
"--gseed",
"--gsnames=another_weird_seed",
}
WithSeeder(conProvider, clientMain)
require.Equal(t, 0, mockFnStatus.callCount)
require.Equal(t, 0, mockSeedStatus.callCount)
require.Equal(t, 1, mockSeed2Status.callCount)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}
func TestWithSeeder_seed_for_env(t *testing.T) {
clientMain, mockFnStatus := newFnMock()
mockCommonSeedFn, mockCommonSeedStatus := newSeedFnMock()
mockSeedSecretFn, mockSeedSecretStatus := newSeedFnMock()
mockSeedStageFn, mockSeedStageStatus := newSeedFnMock()
Registration{
Name: "common_seed",
Env: "",
}.Complete(mockCommonSeedFn)
Registration{
Name: "secret_env_seed",
Env: "secret",
}.Complete(mockSeedSecretFn)
Registration{
Name: "stage_env_seed",
Env: "stage",
}.Complete(mockSeedStageFn)
os.Args = []string{
"",
"--gseed",
"--gsenv=secret",
}
WithSeeder(conProvider, clientMain)
require.Equal(t, 0, mockFnStatus.callCount)
require.Equal(t, 1, mockCommonSeedStatus.callCount)
require.Equal(t, 1, mockSeedSecretStatus.callCount)
require.Equal(t, 0, mockSeedStageStatus.callCount)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}
func TestRegister(t *testing.T) {
Register(dummySeeder)
a := seeders[len(seeders)-1]
require.Equal(t, "", a.env)
require.Equal(t, "dummySeeder", a.name)
require.NotEmpty(t, a.cb)
}
func TestRegisterForTest(t *testing.T) {
RegisterForTest(dummySeeder)
a := seeders[len(seeders)-1]
require.Equal(t, "test", a.env)
require.Equal(t, "dummySeeder", a.name)
require.NotEmpty(t, a.cb)
}
func TestRegisterForEnv(t *testing.T) {
RegisterForEnv("mySuperEnv", dummySeeder)
a := seeders[len(seeders)-1]
require.Equal(t, "mySuperEnv", a.env)
require.Equal(t, "dummySeeder", a.name)
require.NotEmpty(t, a.cb)
}
func TestRegisterForEnvNamed(t *testing.T) {
RegisterForEnvNamed("mySuperEnv", dummySeeder, "customName")
a := seeders[len(seeders)-1]
require.Equal(t, "mySuperEnv", a.env)
require.Equal(t, "customName", a.name)
require.NotEmpty(t, a.cb)
}
func dummySeeder(s Seeder) {}