Skip to content

Commit

Permalink
test(lint): Enable testifylint
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 25, 2024
1 parent 814ff63 commit 8954d1d
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ linters:
- stylecheck
- tenv
- testableexamples
# - testifylint
- testifylint
- typecheck
- unconvert
- unparam
Expand Down
17 changes: 8 additions & 9 deletions internal/actions/dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/clevyr/kubedb/internal/database/postgres"
"github.com/clevyr/kubedb/internal/database/sqlformat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_buildCommand(t *testing.T) {
Expand All @@ -23,46 +24,44 @@ func Test_buildCommand(t *testing.T) {
name string
args args
want *command.Builder
wantErr assert.ErrorAssertionFunc
wantErr require.ErrorAssertionFunc
}{
{
"postgres-gzip",
args{Dump{Dump: config.Dump{Global: config.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}}},
command.NewBuilder(pgpassword, "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose", command.Pipe, "gzip", "--force"),
assert.NoError,
require.NoError,
},
{
"postgres-gzip-no-compression",
args{Dump{Dump: config.Dump{Global: config.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u"}}}},
command.NewBuilder(pgpassword, "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose"),
assert.NoError,
require.NoError,
},
{
"postgres-plain",
args{Dump{Dump: config.Dump{Files: config.Files{Format: sqlformat.Plain}, Global: config.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}}},
command.NewBuilder(pgpassword, "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose", command.Pipe, "gzip", "--force"),
assert.NoError,
require.NoError,
},
{
"postgres-custom",
args{Dump{Dump: config.Dump{Files: config.Files{Format: sqlformat.Custom}, Global: config.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}}},
command.NewBuilder(pgpassword, "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--format=c", "--verbose"),
assert.NoError,
require.NoError,
},
{
"mariadb-gzip",
args{Dump{Dump: config.Dump{Files: config.Files{Format: sqlformat.Gzip}, Global: config.Global{Dialect: mariadb.MariaDB{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}}},
command.NewBuilder(mysqlPwd, command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--verbose", command.Pipe, "gzip", "--force"),
assert.NoError,
require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := tt.args.conf.buildCommand()
if !tt.wantErr(t, err) {
return
}
tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
})
}
Expand Down
19 changes: 9 additions & 10 deletions internal/actions/restore/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/clevyr/kubedb/internal/database/sqlformat"
gzip "github.com/klauspost/pgzip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRestore_buildCommand(t *testing.T) {
Expand All @@ -30,35 +31,35 @@ func TestRestore_buildCommand(t *testing.T) {
fields fields
args args
want *command.Builder
wantErr assert.ErrorAssertionFunc
wantErr require.ErrorAssertionFunc
}{
{
"postgres-gzip",
fields{Restore: config.Restore{Global: config.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}},
args{sqlformat.Gzip},
command.NewBuilder("gunzip", "--force", command.Pipe, pgpassword, "psql", "--host=1.1.1.1", "--username=u", "--dbname=d"),
assert.NoError,
require.NoError,
},
{
"postgres-plain",
fields{Restore: config.Restore{Global: config.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}},
args{sqlformat.Gzip},
command.NewBuilder("gunzip", "--force", command.Pipe, pgpassword, "psql", "--host=1.1.1.1", "--username=u", "--dbname=d"),
assert.NoError,
require.NoError,
},
{
"postgres-custom",
fields{Restore: config.Restore{Global: config.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}},
args{sqlformat.Gzip},
command.NewBuilder("gunzip", "--force", command.Pipe, pgpassword, "psql", "--host=1.1.1.1", "--username=u", "--dbname=d"),
assert.NoError,
require.NoError,
},
{
"postgres-remote-gzip-disabled",
fields{Restore: config.Restore{Global: config.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u"}}},
args{sqlformat.Gzip},
command.NewBuilder(command.Env{Key: "PGPASSWORD", Value: ""}, "psql", "--host=1.1.1.1", "--username=u", "--dbname=d"),
assert.NoError,
require.NoError,
},
}
for _, tt := range tests {
Expand All @@ -69,9 +70,7 @@ func TestRestore_buildCommand(t *testing.T) {
Analyze: tt.fields.Analyze,
}
cmd, err := action.buildCommand(tt.args.inputFormat)
if !tt.wantErr(t, err) {
return
}
tt.wantErr(t, err)
assert.Equal(t, tt.want, cmd)
})
}
Expand Down Expand Up @@ -106,9 +105,9 @@ func Test_gzipCopy(t *testing.T) {
w := &bytes.Buffer{}
err := gzipCopy(w, tt.args.r)
if tt.wantErr {
assert.Error(t, err)
require.Error(t, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
assert.Equal(t, tt.wantW, w.String())
})
Expand Down
17 changes: 7 additions & 10 deletions internal/database/detect_dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/clevyr/kubedb/internal/database/postgres"
"github.com/clevyr/kubedb/internal/kubernetes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubernetesfake "k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestDetectDialect(t *testing.T) {
args args
want config.Database
want1 []v1.Pod
wantErr bool
wantErr require.ErrorAssertionFunc
}{
{
"postgres",
Expand All @@ -52,7 +53,7 @@ func TestDetectDialect(t *testing.T) {
},
postgres.Postgres{},
[]v1.Pod{postgresPod},
false,
require.NoError,
},
{
"mariadb",
Expand All @@ -63,7 +64,7 @@ func TestDetectDialect(t *testing.T) {
},
postgres.Postgres{},
[]v1.Pod{mariadbPod},
false,
require.NoError,
},
{
"no database",
Expand All @@ -74,7 +75,7 @@ func TestDetectDialect(t *testing.T) {
},
nil,
[]v1.Pod{},
true,
require.Error,
},
{
"no pods in namespace",
Expand All @@ -85,18 +86,14 @@ func TestDetectDialect(t *testing.T) {
},
nil,
[]v1.Pod{},
true,
require.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, got1, err := DetectDialect(context.Background(), tt.args.client)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.want1, got1)
})
Expand Down
29 changes: 13 additions & 16 deletions internal/database/dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/clevyr/kubedb/internal/database/postgres"
"github.com/clevyr/kubedb/internal/database/sqlformat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
Expand All @@ -20,28 +21,24 @@ func TestNew(t *testing.T) {
name string
args args
want config.Database
wantErr bool
wantErr require.ErrorAssertionFunc
}{
{"postgresql", args{"postgresql"}, postgres.Postgres{}, false},
{"postgres", args{"postgres"}, postgres.Postgres{}, false},
{"psql", args{"psql"}, postgres.Postgres{}, false},
{"pg", args{"pg"}, postgres.Postgres{}, false},
{"mariadb", args{"mariadb"}, mariadb.MariaDB{}, false},
{"maria", args{"maria"}, mariadb.MariaDB{}, false},
{"mysql", args{"mysql"}, mariadb.MariaDB{}, false},
{"mongodb", args{"mongodb"}, mongodb.MongoDB{}, false},
{"mongo", args{"mongo"}, mongodb.MongoDB{}, false},
{"invalid", args{"invalid"}, nil, true},
{"postgresql", args{"postgresql"}, postgres.Postgres{}, require.NoError},
{"postgres", args{"postgres"}, postgres.Postgres{}, require.NoError},
{"psql", args{"psql"}, postgres.Postgres{}, require.NoError},
{"pg", args{"pg"}, postgres.Postgres{}, require.NoError},
{"mariadb", args{"mariadb"}, mariadb.MariaDB{}, require.NoError},
{"maria", args{"maria"}, mariadb.MariaDB{}, require.NoError},
{"mysql", args{"mysql"}, mariadb.MariaDB{}, require.NoError},
{"mongodb", args{"mongodb"}, mongodb.MongoDB{}, require.NoError},
{"mongo", args{"mongo"}, mongodb.MongoDB{}, require.NoError},
{"invalid", args{"invalid"}, nil, require.Error},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := New(tt.args.name)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
})
}
Expand Down
6 changes: 3 additions & 3 deletions internal/database/mariadb/mariadb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestMariaDB_DumpCommand(t *testing.T) {
t.Parallel()
ma := MariaDB{}
got := ma.DumpCommand(tt.args.conf)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestMariaDB_ExecCommand(t *testing.T) {
t.Parallel()
ma := MariaDB{}
got := ma.ExecCommand(tt.args.conf)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestMariaDB_RestoreCommand(t *testing.T) {
t.Parallel()
ma := MariaDB{}
got := ma.RestoreCommand(tt.args.conf, tt.args.inputFormat)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
6 changes: 3 additions & 3 deletions internal/database/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestMongoDB_DumpCommand(t *testing.T) {
t.Parallel()
ma := MongoDB{}
got := ma.DumpCommand(tt.args.conf)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestMongoDB_ExecCommand(t *testing.T) {
t.Parallel()
ma := MongoDB{}
got := ma.ExecCommand(tt.args.conf)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestMongoDB_RestoreCommand(t *testing.T) {
t.Parallel()
ma := MongoDB{}
got := ma.RestoreCommand(tt.args.conf, tt.args.inputFormat)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand Down
19 changes: 8 additions & 11 deletions internal/database/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/clevyr/kubedb/internal/database/sqlformat"
"github.com/clevyr/kubedb/internal/kubernetes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestPostgres_DumpCommand(t *testing.T) {
t.Parallel()
po := Postgres{}
got := po.DumpCommand(tt.args.conf)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -115,7 +116,7 @@ func TestPostgres_ExecCommand(t *testing.T) {
t.Parallel()
po := Postgres{}
got := po.ExecCommand(tt.args.conf)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand All @@ -139,7 +140,7 @@ func TestPostgres_FilterPods(t *testing.T) {
name string
args args
want []v1.Pod
wantErr bool
wantErr require.ErrorAssertionFunc
}{
{
"postgresql",
Expand All @@ -148,21 +149,17 @@ func TestPostgres_FilterPods(t *testing.T) {
[]v1.Pod{postgresPod},
},
[]v1.Pod{postgresPod},
false,
require.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
po := Postgres{}
got, err := po.FilterPods(context.TODO(), tt.args.client, tt.args.pods)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
tt.wantErr(t, err)

assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -264,7 +261,7 @@ func TestPostgres_RestoreCommand(t *testing.T) {
t.Parallel()
po := Postgres{}
got := po.RestoreCommand(tt.args.conf, tt.args.inputFormat)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}
Expand Down
Loading

0 comments on commit 8954d1d

Please sign in to comment.