-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_test.go
74 lines (68 loc) · 2.05 KB
/
cli_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
package imigrate
import (
"fmt"
"os"
"testing"
)
type commandData struct {
command string
steps int
version int64
newName string
}
type TestingMigrator struct {
}
var data commandData
func (o TestingMigrator) Create(name string) {
data.command = "create"
data.newName = name
}
func (o TestingMigrator) Up(steps int, version int64) {
data.command = "up"
data.steps = steps
data.version = version
}
func (o TestingMigrator) Down(steps int, version int64) {
data.command = "down"
data.steps = steps
data.version = version
}
func (o TestingMigrator) Redo(steps int, version int64) {
data.command = "redo"
data.steps = steps
}
func (o TestingMigrator) Rollback(steps int) {
data.command = "rollback"
data.steps = steps
}
func (o TestingMigrator) Status() {
data.command = "status"
}
func TestCLIArgs(t *testing.T) {
tests := []struct {
args []string
expected commandData
}{
{[]string{"cli", "create", "new_table"}, commandData{"create", 0, 0, "new_table"}},
{[]string{"cli", "up"}, commandData{"up", -1, 0, ""}},
{[]string{"cli", "up", "-steps=1"}, commandData{"up", 1, 0, ""}},
{[]string{"cli", "down"}, commandData{"down", -1, 0, ""}},
{[]string{"cli", "down", "-steps=2"}, commandData{"down", 2, 0, ""}},
{[]string{"cli", "redo", "-steps=3"}, commandData{"redo", 3, 0, ""}},
{[]string{"cli", "rollback", "-steps=4"}, commandData{"rollback", 4, 0, ""}},
{[]string{"cli", "status", "new_table"}, commandData{"status", 0, 0, ""}},
{[]string{"cli", "up", "-version=1610069160"}, commandData{"up", -1, 1610069160, ""}},
{[]string{"cli", "down", "-version=1610069160"}, commandData{"down", -1, 1610069160, ""}},
}
mig := TestingMigrator{}
for _, tt := range tests {
data = commandData{}
t.Run(fmt.Sprintf("%s-%d", tt.expected.command, tt.expected.steps), func(t *testing.T) {
os.Args = tt.args
CLI(mig)
if data.command != tt.expected.command || data.steps != tt.expected.steps || data.version != tt.expected.version || data.newName != tt.expected.newName {
t.Fatalf("expected %#v got %#v\n", tt.expected, data)
}
})
}
}