-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathcmd_test.go
157 lines (127 loc) · 4.17 KB
/
cmd_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
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package popx_test
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"github.com/bradleyjkemp/cupaloy/v2"
"github.com/sirupsen/logrus"
"github.com/ory/x/cmdx"
"github.com/ory/x/dbal"
"github.com/ory/x/logrusx"
"github.com/ory/x/popx"
"github.com/gobuffalo/pop/v6"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
type MockPersistenceProvider struct {
c *pop.Connection
mb *popx.MigrationBox
}
func (m *MockPersistenceProvider) MigrateDown(ctx context.Context, i int) error {
return m.mb.Down(ctx, i)
}
func (m *MockPersistenceProvider) Connection(ctx context.Context) *pop.Connection {
return m.c
}
func (m *MockPersistenceProvider) MigrationStatus(ctx context.Context) (popx.MigrationStatuses, error) {
return m.mb.Status(ctx)
}
func (m *MockPersistenceProvider) MigrateUp(ctx context.Context) error {
return m.mb.Up(ctx)
}
func NewMockPersistenceProvider(
c *pop.Connection,
mb *popx.MigrationBox,
) *MockPersistenceProvider {
return &MockPersistenceProvider{c: c, mb: mb}
}
func TestMigrateSQLUp(t *testing.T) {
ctx := context.Background()
c, err := pop.NewConnection(&pop.ConnectionDetails{
URL: dbal.NewSQLiteTestDatabase(t),
})
require.NoError(t, err)
require.NoError(t, c.Open())
migrator := popx.NewMigrator(c, logrusx.New("", "", logrusx.ForceLevel(logrus.DebugLevel)), nil, 0)
mb, err := popx.NewMigrationBox(transactionalMigrations, migrator)
require.NoError(t, err)
p := NewMockPersistenceProvider(c, mb)
newCmd := func() *cobra.Command {
cmd := &cobra.Command{Use: ""}
cmd.AddCommand(popx.RegisterMigrateSQLUpFlags(&cobra.Command{
Use: "up <database-url>",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
return popx.MigrateSQLUp(cmd, p)
}}))
cmd.AddCommand(popx.RegisterMigrateSQLDownFlags(&cobra.Command{
Use: "down <database-url>",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
return popx.MigrateSQLDown(cmd, p)
}}))
cmd.AddCommand(popx.RegisterMigrateStatusFlags(&cobra.Command{
Use: "status <database-url>",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
return popx.MigrateStatus(cmd, p)
}}))
return cmd
}
run := func(t *testing.T, cmd *cobra.Command, stdIn io.Reader, args ...string) {
t.Helper()
stdout, stderr, err := cmdx.ExecCtx(ctx, newCmd(), stdIn, args...)
require.NoError(t, err, stdout, stderr)
cupaloy.New(
cupaloy.CreateNewAutomatically(true),
cupaloy.FailOnUpdate(true),
cupaloy.SnapshotFileExtension(".txt"),
).SnapshotT(t, fmt.Sprintf("stdout: %s\nstderr: %s", stdout, stderr))
}
t.Run("status pre", func(t *testing.T) {
run(t, newCmd(), nil, "status")
})
t.Run("migrate up", func(t *testing.T) {
run(t, newCmd(), nil, "up", "-y")
})
t.Run("status migrated", func(t *testing.T) {
run(t, newCmd(), nil, "status")
})
t.Run("migrate down four steps", func(t *testing.T) {
run(t, newCmd(), nil, "down", "-y", "--steps", "4")
})
t.Run("status two steps rolled back", func(t *testing.T) {
run(t, newCmd(), nil, "status")
})
t.Run("migrate down but no steps", func(t *testing.T) {
stdout, stderr, err := cmdx.ExecCtx(ctx, newCmd(), nil, "down", "-y")
require.Error(t, err)
cupaloy.New(
cupaloy.CreateNewAutomatically(true),
cupaloy.FailOnUpdate(true),
cupaloy.SnapshotFileExtension(".txt"),
).SnapshotT(t, fmt.Sprintf("stdout: %s\nstderr: %s", stdout, stderr))
})
t.Run("migrate down but do not confirm", func(t *testing.T) {
run(t, newCmd(), bytes.NewBufferString("n\n"), "down", "--steps", "2")
})
t.Run("migrate down two steps", func(t *testing.T) {
run(t, newCmd(), bytes.NewBufferString("y\n"), "down", "--steps", "2")
})
t.Run("status two versions rolled back", func(t *testing.T) {
run(t, newCmd(), nil, "status")
})
t.Run("migrate rollbacks up again", func(t *testing.T) {
run(t, newCmd(), bytes.NewBufferString("y\n"), "up")
})
t.Run("final status", func(t *testing.T) {
run(t, newCmd(), nil, "status")
})
t.Run("migrate rollbacks up without confirm", func(t *testing.T) {
run(t, newCmd(), bytes.NewBufferString("n\n"), "up")
})
}