From 6e856a1393795c6c8bd50c2611feeab68e0a504f Mon Sep 17 00:00:00 2001 From: raoptimus Date: Tue, 3 Dec 2024 00:38:54 +0300 Subject: [PATCH] fix tests --- cmd/db-migrator/main.go | 14 +++++++------- internal/action/common_test.go | 19 ------------------- internal/action/create.go | 5 ++--- internal/action/create_test.go | 5 +++-- internal/action/downgrade.go | 5 ++--- internal/action/history.go | 5 ++--- internal/action/history_new.go | 5 ++--- internal/action/redo.go | 5 ++--- internal/action/to.go | 3 +-- internal/action/upgrade.go | 5 ++--- internal/action/upgrade_test.go | 7 ++++--- .../migrator/db_service_integration_test.go | 17 +++++++++-------- internal/migrator/db_service_test.go | 16 ---------------- 13 files changed, 36 insertions(+), 75 deletions(-) diff --git a/cmd/db-migrator/main.go b/cmd/db-migrator/main.go index 037f064..d40cbf3 100644 --- a/cmd/db-migrator/main.go +++ b/cmd/db-migrator/main.go @@ -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)) } }, }, @@ -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)) } }, }, @@ -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)) }, }, { @@ -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)) } }, }, @@ -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)) } }, }, @@ -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)) } }, }, diff --git a/internal/action/common_test.go b/internal/action/common_test.go index 2d17dfe..1e72055 100644 --- a/internal/action/common_test.go +++ b/internal/action/common_test.go @@ -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) -} diff --git a/internal/action/create.go b/internal/action/create.go index e17085e..97da326 100644 --- a/internal/action/create.go +++ b/internal/action/create.go @@ -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 @@ -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 } diff --git a/internal/action/create_test.go b/internal/action/create_test.go index 0018eeb..2bbd382 100644 --- a/internal/action/create_test.go +++ b/internal/action/create_test.go @@ -1,6 +1,7 @@ package action import ( + "context" "testing" "time" @@ -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 }) @@ -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) }) } diff --git a/internal/action/downgrade.go b/internal/action/downgrade.go index 5c18ee4..c594db2 100644 --- a/internal/action/downgrade.go +++ b/internal/action/downgrade.go @@ -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 { @@ -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 } diff --git a/internal/action/history.go b/internal/action/history.go index 4925060..6663428 100644 --- a/internal/action/history.go +++ b/internal/action/history.go @@ -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 @@ -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 } diff --git a/internal/action/history_new.go b/internal/action/history_new.go index 5e965cd..d3f5b06 100644 --- a/internal/action/history_new.go +++ b/internal/action/history_new.go @@ -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 { @@ -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 } diff --git a/internal/action/redo.go b/internal/action/redo.go index b858b7d..1aad9fe 100644 --- a/internal/action/redo.go +++ b/internal/action/redo.go @@ -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 { @@ -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 } diff --git a/internal/action/to.go b/internal/action/to.go index 3b500a7..f1daba3 100644 --- a/internal/action/to.go +++ b/internal/action/to.go @@ -4,7 +4,6 @@ import ( "context" "github.com/raoptimus/db-migrator.go/internal/console" - "github.com/urfave/cli/v3" ) type To struct { @@ -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 diff --git a/internal/action/upgrade.go b/internal/action/upgrade.go index 3c8c3c4..a059037 100644 --- a/internal/action/upgrade.go +++ b/internal/action/upgrade.go @@ -13,7 +13,6 @@ import ( "fmt" "github.com/raoptimus/db-migrator.go/internal/args" - "github.com/urfave/cli/v3" ) const ( @@ -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 } diff --git a/internal/action/upgrade_test.go b/internal/action/upgrade_test.go index b7f9e81..610e9a5 100644 --- a/internal/action/upgrade_test.go +++ b/internal/action/upgrade_test.go @@ -1,6 +1,7 @@ package action import ( + "context" "testing" "github.com/raoptimus/db-migrator.go/internal/action/mockaction" @@ -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) @@ -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) } diff --git a/internal/migrator/db_service_integration_test.go b/internal/migrator/db_service_integration_test.go index 79bddd5..5023f9e 100644 --- a/internal/migrator/db_service_integration_test.go +++ b/internal/migrator/db_service_integration_test.go @@ -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) }) @@ -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")), @@ -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) } diff --git a/internal/migrator/db_service_test.go b/internal/migrator/db_service_test.go index ea640cc..e8c158a 100644 --- a/internal/migrator/db_service_test.go +++ b/internal/migrator/db_service_test.go @@ -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) { @@ -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) -}