-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
206 lines (159 loc) · 4.83 KB
/
command_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package dbshiftcore
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"os"
"os/exec"
"path/filepath"
"testing"
)
var c *cmd
type dummyDbImplementation struct {
envStatus string
}
func (db *dummyDbImplementation) GetExtension() string {
return "txt"
}
func (db *dummyDbImplementation) GetStatus() (*Status, error) {
s := Status{}
statusJson := os.Getenv(db.envStatus)
if statusJson == "" {
return &s, nil
}
if err := json.Unmarshal([]byte(statusJson), &s); err != nil {
return nil, err
}
return &s, nil
}
func (db *dummyDbImplementation) SetStatus(migration Migration, executionTimeInSeconds float64) error {
s := Status{
Type: migration.Type,
Version: migration.Version,
}
statusJson, err := json.Marshal(s)
if err != nil {
return err
}
if err := os.Setenv(db.envStatus, string(statusJson)); err != nil {
return err
}
return nil
}
func (db *dummyDbImplementation) ExecuteMigration([]byte) error {
return nil
}
func TestNewCmd_NoImplementation(t *testing.T) {
_, err := NewCmd(nil)
assert.NotNil(t, err, "expected missing db implementation error")
}
func TestNewCmd_NoConfiguration(t *testing.T) {
_, err := NewCmd(new(dummyDbImplementation))
assert.NotNil(t, err, "expected missing configuration")
}
func TestNewCmd(t *testing.T) {
err := setConfigurationWithCustomOptions("false", "false", "false")
assert.Nil(t, err, "expected nil error on set configuration with custom options")
c, err = NewCmd(&dummyDbImplementation{envStatus: "DBSHIFT_DUMMY_STATUS"})
assert.Nil(t, err, "expected nil error")
}
func TestCmd_Run_NoArgs(t *testing.T) {
// Reset args in order to run shell in interactive mode
os.Args = []string{}
// Run shell in interactive mode
c.Run()
}
func TestCmd_Run_WithArgs(t *testing.T) {
os.Args = []string{"help"}
c.Run()
}
func TestGetShellCommands(t *testing.T) {
cmdList := c.getShellCommands()
assert.Equal(t, 4, len(cmdList), "expected specific amount of commands")
}
func TestCmd_HandleStatus(t *testing.T) {
assert.Nil(t, c.status(), "expect nil error handling status")
}
func TestCmd_HandleCreate(t *testing.T) {
assert.Nil(t, c.create("some-migration"), "expect nil error handling create")
}
func TestCmd_HandleUpgrade(t *testing.T) {
assert.Nil(t, c.upgrade(""), "expect nil error handling upgrade")
}
func TestCmd_HandleDowngrade(t *testing.T) {
assert.Nil(t, c.downgrade(""), "expect nil error handling downgrade")
}
func TestCmdCreate(t *testing.T) {
err := c.create("my-beautiful-migration")
migrationsPath := os.Getenv("DBSHIFT_ABS_FOLDER_MIGRATIONS")
assert.NotEmpty(t, migrationsPath, "expected valid migration path")
t.Log(migrationsPath)
stdout, err := exec.Command("ls", migrationsPath).Output()
t.Log(string(stdout), err)
assert.Nil(t, err)
}
func TestCmdStatus(t *testing.T) {
err := c.status()
assert.Nil(t, err)
}
func TestCmdUpgrade(t *testing.T) {
err := c.upgrade("")
assert.Nil(t, err, "expect nil error on upgrade")
}
func TestCmdDowngrade(t *testing.T) {
err := c.downgrade("")
assert.Nil(t, err, "expect nil error on downgrade")
}
// When Disabled
func TestNewCmd_Disabled(t *testing.T) {
err := setConfigurationWithCustomOptions("true", "true", "true")
assert.Nil(t, err, "expected nil error on set configuration with custom options")
c, err = NewCmd(&dummyDbImplementation{envStatus: "DBSHIFT_DUMMY_STATUS"})
assert.Nil(t, err, "expected nil error")
}
func TestCmd_Create_Disabled(t *testing.T) {
err := c.create("")
assert.NotNil(t, err, "expect error on create because disabled")
}
func TestCmd_Upgrade_Disabled(t *testing.T) {
err := c.upgrade("")
assert.NotNil(t, err, "expect error on upgrade because disabled")
}
func TestCmd_Downgrade_Disabled(t *testing.T) {
err := c.downgrade("")
assert.NotNil(t, err, "expect nil error on downgrade because disabled")
}
// Helpers
func setConfigurationWithCustomOptions(isCreateDisabled, isDowngradeDisabled, isUpgradeDisabled string) error {
migrationsPath := filepath.Join("./tmp/dbshift")
// Remove everything inside the migration path for a clean test
if err := os.RemoveAll(migrationsPath); err != nil {
return err
}
// Create temp migration path
if err := os.MkdirAll(migrationsPath, 0777); err != nil {
return err
}
err := os.Setenv("DBSHIFT_ABS_FOLDER_MIGRATIONS", migrationsPath)
if err != nil {
return err
}
if err := setIsCreateDisabled(isCreateDisabled); err != nil {
return err
}
if err := setIsDowngradeDisabled(isDowngradeDisabled); err != nil {
return err
}
if err := setIsUpgradeDisabled(isUpgradeDisabled); err != nil {
return err
}
return nil
}
func setIsCreateDisabled(v string) error {
return os.Setenv("DBSHIFT_OPTION_IS_CREATE_DISABLED", v)
}
func setIsDowngradeDisabled(v string) error {
return os.Setenv("DBSHIFT_OPTION_IS_DOWNGRADE_DISABLED", v)
}
func setIsUpgradeDisabled(v string) error {
return os.Setenv("DBSHIFT_OPTION_IS_UPGRADE_DISABLED", v)
}