Skip to content

Commit bd295da

Browse files
committed
feat: new testing incoming
1 parent 7808294 commit bd295da

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

spec/action_migrate_up_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ import (
55
)
66

77
func TestActionMigrateUp(t *testing.T) {
8+
cli := NewTestCLI(t)
9+
defer cli.Teardown()
10+
11+
cli.DefaultDatabase()
12+
cli.Set(
13+
"animals.fyml",
14+
`
15+
apiVersion: migrations/v1
16+
kind: MigrationSet
17+
metadata:
18+
name: public
19+
spec:
20+
namespace:
21+
name: public
22+
migrations:
23+
- create_animals
24+
`,
25+
)
26+
cli.AssertRun("migrate", "up", "--set", "public", "-d", "test")
27+
cli.AssertOutputContains()
28+
cli.AssertSchemaMigrate
29+
30+
831
c := mockCli(mockConfig(`
932
migration "do_nothing" {
1033
version = "v1"
@@ -18,7 +41,7 @@ migration_set "public" {
1841
}
1942
`))
2043
defer c.Teardown()
21-
c.AssertSuccessfulRun(t, []string{"migrate", "up", "public"})
44+
c.AssertSuccessfulRun(t, []string{"migrate", "up", "--set", "public", "-d", "test"})
2245
c.AssertOutputContains(t, "\x1b[0;32mOK \x1b[0mv1 do_nothing")
2346
c.AssertSchemaMigrationTable(t, "public", "v1")
2447
}

spec/helpers_test.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/google/uuid"
1112
_ "github.com/jackc/pgx/v5"
1213
_ "github.com/jackc/pgx/v5/stdlib"
13-
"github.com/ohkrab/krab/cli"
1414
"github.com/ohkrab/krab/krab"
15-
"github.com/ohkrab/krab/krabcli"
1615
"github.com/ohkrab/krab/krabdb"
1716
"github.com/ohkrab/krab/web"
1817
"github.com/spf13/afero"
@@ -21,16 +20,55 @@ import (
2120
)
2221

2322
type cliMock struct {
24-
connection *mockDBConnection
25-
config *krab.Config
26-
app *krabcli.App
23+
connection *mockDBConnection
24+
// config *krab.Config
25+
// app *krabcli.App
2726
exitCode int
2827
err error
2928
uiWriter bytes.Buffer
3029
uiErrorWriter bytes.Buffer
3130
helpWriter bytes.Buffer
3231
errorWriter bytes.Buffer
3332
fs afero.Afero
33+
id string
34+
}
35+
36+
func NewTestCLI(t *testing.T) *cliMock {
37+
memfs := afero.NewMemMapFs()
38+
39+
return &cliMock{
40+
id: uuid.Must(uuid.NewV7()).String(),
41+
fs: afero.Afero{Fs: memfs},
42+
}
43+
}
44+
45+
func (c *cliMock) DefaultDatabase() {
46+
c.Files(
47+
"config.fyml",
48+
`
49+
apiVersion: drivers/v1
50+
kind: Driver
51+
metadata:
52+
name: test
53+
spec:
54+
driver: postgresql
55+
config:
56+
dsn: postgres://test:test@localhost:5433/test
57+
`,
58+
)
59+
}
60+
61+
func (c *cliMock) Files(pathContentPair ...string) {
62+
for i := 1; i < len(pathContentPair); i += 2 {
63+
path := pathContentPair[i-1]
64+
content := pathContentPair[i]
65+
afero.WriteFile(
66+
c.fs,
67+
path,
68+
[]byte(content),
69+
0644,
70+
)
71+
}
3472
}
3573

3674
func (m *cliMock) setup(args []string) {

0 commit comments

Comments
 (0)