Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raoptimus committed Dec 2, 2024
1 parent eaefd7d commit 6e856a1
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 75 deletions.
14 changes: 7 additions & 7 deletions cmd/db-migrator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func commands() []*cli.Command {
if a, err := dbService.Upgrade(); err != nil {
return err
} else {
return a.Run(ctx, cmd.Args())
return a.Run(ctx, cmd.Args().Get(0))
}
},
},
Expand All @@ -65,7 +65,7 @@ func commands() []*cli.Command {
if a, err := dbService.Downgrade(); err != nil {
return err
} else {
return a.Run(ctx, cmd.Args())
return a.Run(ctx, cmd.Args().Get(0))
}
},
},
Expand All @@ -75,14 +75,14 @@ func commands() []*cli.Command {
if a, err := dbService.Redo(); err != nil {
return err
} else {
return a.Run(ctx, cmd.Args())
return a.Run(ctx, cmd.Args().Get(0))
}
},
},
{
Name: "create",
Action: func(ctx context.Context, cmd *cli.Command) error {
return dbService.Create().Run(ctx, cmd.Args())
return dbService.Create().Run(ctx, cmd.Args().Get(0))
},
},
{
Expand All @@ -91,7 +91,7 @@ func commands() []*cli.Command {
if a, err := dbService.History(); err != nil {
return err
} else {
return a.Run(ctx, cmd.Args())
return a.Run(ctx, cmd.Args().Get(0))
}
},
},
Expand All @@ -101,7 +101,7 @@ func commands() []*cli.Command {
if a, err := dbService.HistoryNew(); err != nil {
return err
} else {
return a.Run(ctx, cmd.Args())
return a.Run(ctx, cmd.Args().Get(0))
}
},
},
Expand All @@ -111,7 +111,7 @@ func commands() []*cli.Command {
if a, err := dbService.To(); err != nil {
return err
} else {
return a.Run(ctx, cmd.Args())
return a.Run(ctx, cmd.Args().Get(0))
}
},
},
Expand Down
19 changes: 0 additions & 19 deletions internal/action/common_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
package action

import (
"flag"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v3"
)

var time230527213123 = time.Date(
2023, 05, 27,
21, 31, 23,
0, time.UTC)

func flagSet(t *testing.T, argument string) *flag.FlagSet {
flagSet := flag.NewFlagSet("test", 0)
err := flagSet.Parse([]string{argument})
assert.NoError(t, err)

return flagSet
}

func cliContext(t *testing.T, argument string) *cli.Context {
flagSet := flagSet(t, argument)

return cli.NewContext(nil, flagSet, nil)
}
5 changes: 2 additions & 3 deletions internal/action/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/pkg/errors"
"github.com/raoptimus/db-migrator.go/pkg/timex"
"github.com/urfave/cli/v3"
)

const fileModeExecutable = 0o755
Expand Down Expand Up @@ -47,8 +46,8 @@ func NewCreate(
}
}

func (c *Create) Run(_ context.Context, cmdArgs cli.Args) error {
migrationName := cmdArgs.Get(0)
func (c *Create) Run(_ context.Context, cmdArgs ...string) error {
migrationName := cmdArgs[0]
if !regexpFileName.MatchString(migrationName) {
return ErrInvalidFileName
}
Expand Down
5 changes: 3 additions & 2 deletions internal/action/create_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -37,6 +38,7 @@ func TestCreate_Run_ExpectedArguments_NoError(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
tm := timex.New(func() time.Time {
return time230527213123
})
Expand Down Expand Up @@ -73,9 +75,8 @@ func TestCreate_Run_ExpectedArguments_NoError(t *testing.T) {
Down(tt.version, true).
Return(tt.fileNameDown, tt.safely)

ctx := cliContext(t, "init")
create := NewCreate(tm, f, c, fb, "/tmp")
err := create.Run(ctx)
err := create.Run(ctx, "init")
assert.NoError(t, err)
})
}
Expand Down
5 changes: 2 additions & 3 deletions internal/action/downgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/raoptimus/db-migrator.go/internal/args"
"github.com/raoptimus/db-migrator.go/internal/console"
"github.com/urfave/cli/v3"
)

type Downgrade struct {
Expand All @@ -35,8 +34,8 @@ func NewDowngrade(
}
}

func (d *Downgrade) Run(ctx context.Context, cmdArgs cli.Args) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs.Get(0), minLimit)
func (d *Downgrade) Run(ctx context.Context, cmdArgs ...string) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs[0], minLimit)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/action/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/raoptimus/db-migrator.go/internal/args"
"github.com/raoptimus/db-migrator.go/internal/console"
"github.com/urfave/cli/v3"
)

const defaultGetHistoryLimit = 10
Expand All @@ -30,8 +29,8 @@ func NewHistory(
}
}

func (h *History) Run(ctx context.Context, cmdArgs cli.Args) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs.Get(0), defaultGetHistoryLimit)
func (h *History) Run(ctx context.Context, cmdArgs ...string) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs[0], defaultGetHistoryLimit)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/action/history_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/raoptimus/db-migrator.go/internal/args"
"github.com/raoptimus/db-migrator.go/internal/console"
"github.com/urfave/cli/v3"
)

type HistoryNew struct {
Expand All @@ -28,8 +27,8 @@ func NewHistoryNew(
}
}

func (h *HistoryNew) Run(ctx context.Context, cmdArgs cli.Args) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs.Get(0), defaultGetHistoryLimit)
func (h *HistoryNew) Run(ctx context.Context, cmdArgs ...string) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs[0], defaultGetHistoryLimit)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/action/redo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/raoptimus/db-migrator.go/internal/args"
"github.com/raoptimus/db-migrator.go/internal/console"
"github.com/raoptimus/db-migrator.go/internal/dal/entity"
"github.com/urfave/cli/v3"
)

type Redo struct {
Expand All @@ -36,8 +35,8 @@ func NewRedo(
}
}

func (r *Redo) Run(ctx context.Context, cmdArgs cli.Args) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs.Get(0), minLimit)
func (r *Redo) Run(ctx context.Context, cmdArgs ...string) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs[0], minLimit)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/action/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/raoptimus/db-migrator.go/internal/console"
"github.com/urfave/cli/v3"
)

type To struct {
Expand All @@ -25,7 +24,7 @@ func NewTo(
}
}

func (t *To) Run(_ context.Context, _ cli.Args) error {
func (t *To) Run(_ context.Context, _ ...string) error {
// version string from args
console.Info("coming soon")
return nil
Expand Down
5 changes: 2 additions & 3 deletions internal/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"fmt"

"github.com/raoptimus/db-migrator.go/internal/args"
"github.com/urfave/cli/v3"
)

const (
Expand Down Expand Up @@ -41,8 +40,8 @@ func NewUpgrade(
}
}

func (u *Upgrade) Run(ctx context.Context, cmdArgs cli.Args) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs.Get(0), defaultUpgradeLimit)
func (u *Upgrade) Run(ctx context.Context, cmdArgs ...string) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs[0], defaultUpgradeLimit)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions internal/action/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action

import (
"context"
"testing"

"github.com/raoptimus/db-migrator.go/internal/action/mockaction"
Expand All @@ -9,11 +10,11 @@ import (
)

func TestUpgrade_Run_NoMigrations_NoError(t *testing.T) {
ctx := cliContext(t, "2")
ctx := context.Background()

serv := mockaction.NewMigrationService(t)
serv.EXPECT().
NewMigrations(ctx.Context).
NewMigrations(ctx).
Return(entity.Migrations{}, nil)

c := mockaction.NewConsole(t)
Expand All @@ -23,6 +24,6 @@ func TestUpgrade_Run_NoMigrations_NoError(t *testing.T) {
fb := mockaction.NewFileNameBuilder(t)

upgrade := NewUpgrade(c, serv, fb, true)
err := upgrade.Run(ctx)
err := upgrade.Run(ctx, "2")
assert.NoError(t, err)
}
17 changes: 9 additions & 8 deletions internal/migrator/db_service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,20 @@ func TestIntegrationDBService_UpDown_Successfully(t *testing.T) {
assert.NoError(t, err)

defer func() {
_ = down.Run(cliContext(t, "all"))
_ = down.Run(ctx, "all")
}()

err = up.Run(cliContext(t, "2"))
err = up.Run(ctx, "2")
assert.NoError(t, err)
assertEqualRowsCount(t, ctx, dbServ.repo, 3)

err = up.Run(cliContext(t, "1")) // migration with error
err = up.Run(ctx, "1") // migration with error
assert.Error(t, err)
assertEqualRowsCount(t, ctx, dbServ.repo, 3)
err = dbServ.repo.ExecQuery(ctx, "select * from test") // checks table exists
assert.NoError(t, err)

err = down.Run(cliContext(t, "all"))
err = down.Run(ctx, "all")
assert.NoError(t, err)
assertEqualRowsCount(t, ctx, dbServ.repo, 1)
})
Expand All @@ -108,6 +108,7 @@ func TestIntegrationDBService_Upgrade_AlreadyExistsMigration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
opts := Options{
DSN: os.Getenv("POSTGRES_DSN"),
Directory: migrationsPathAbs(os.Getenv("POSTGRES_MIGRATIONS_PATH")),
Expand All @@ -119,19 +120,19 @@ func TestIntegrationDBService_Upgrade_AlreadyExistsMigration(t *testing.T) {

down, err := dbServ.Downgrade()
assert.NoError(t, err)
err = down.Run(cliContext(t, "all"))
err = down.Run(ctx, "all")
assert.NoError(t, err)

up, err := dbServ.Upgrade()
assert.NoError(t, err)
// apply first migration
err = up.Run(cliContext(t, "1"))
err = up.Run(ctx, "1")
assert.NoError(t, err)
// apply second migration
err = up.Run(cliContext(t, "1"))
err = up.Run(ctx, "1")
assert.NoError(t, err)
// apply third broken migration
err = up.Run(cliContext(t, "1"))
err = up.Run(ctx, "1")
assert.Error(t, err)
}

Expand Down
16 changes: 0 additions & 16 deletions internal/migrator/db_service_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package migrator

import (
"flag"
"testing"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v3"
)

func TestDBService_Create_ReturnsAction(t *testing.T) {
Expand Down Expand Up @@ -67,17 +65,3 @@ func TestDBService_Redo_ReturnsAction(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, action)
}

func flagSet(t *testing.T, argument string) *flag.FlagSet {
flagSet := flag.NewFlagSet("test", 0)
err := flagSet.Parse([]string{argument})
assert.NoError(t, err)

return flagSet
}

func cliContext(t *testing.T, argument string) *cli.Context {
flagSet := flagSet(t, argument)

return cli.NewContext(nil, flagSet, nil)
}

0 comments on commit 6e856a1

Please sign in to comment.