Skip to content

Commit

Permalink
✅ Add a whole slew of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 21, 2022
1 parent 9742529 commit d3ffd27
Show file tree
Hide file tree
Showing 17 changed files with 1,869 additions and 89 deletions.
92 changes: 68 additions & 24 deletions cmd/dump/dump_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,81 @@
package dump

import (
"fmt"
"github.com/clevyr/kubedb/internal/command"
"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/database/dialect"
"github.com/clevyr/kubedb/internal/database/sqlformat"
"strings"
"github.com/spf13/cobra"
"reflect"
"testing"
"time"
)

func TestGenerateFilename(t *testing.T) {
testCases := []struct {
namespace string
filetype sqlformat.Format
err error
func Test_buildCommand(t *testing.T) {
pgpassword := command.NewEnv("PGPASSWORD", "")
mysql_pwd := command.NewEnv("MYSQL_PWD", "")

type args struct {
db config.Databaser
conf config.Dump
}
tests := []struct {
name string
args args
want *command.Builder
}{
{
"postgres-gzip",
args{dialect.Postgres{}, config.Dump{Global: config.Global{Database: "d", Username: "u"}}},
command.NewBuilder(pgpassword, "pg_dump", "--host=127.0.0.1", "--username=u", "--dbname=d", command.Pipe, "gzip", "--force", command.Pipe, "base64", "-w0"),
},
{
"postgres-plain",
args{dialect.Postgres{}, config.Dump{Files: config.Files{Format: sqlformat.Plain}, Global: config.Global{Database: "d", Username: "u"}}},
command.NewBuilder(pgpassword, "pg_dump", "--host=127.0.0.1", "--username=u", "--dbname=d", command.Pipe, "gzip", "--force", command.Pipe, "base64", "-w0"),
},
{
"postgres-custom",
args{dialect.Postgres{}, config.Dump{Files: config.Files{Format: sqlformat.Custom}, Global: config.Global{Database: "d", Username: "u"}}},
command.NewBuilder(pgpassword, "pg_dump", "--host=127.0.0.1", "--username=u", "--dbname=d", "--format=c", command.Pipe, "base64", "-w0"),
},
{
"mariadb-gzip",
args{dialect.MariaDB{}, config.Dump{Files: config.Files{Format: sqlformat.Gzip}, Global: config.Global{Database: "d", Username: "u"}}},
command.NewBuilder(mysql_pwd, "mysqldump", "--host=127.0.0.1", "--user=u", "d", command.Pipe, "gzip", "--force", command.Pipe, "base64", "-w0"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildCommand(tt.args.db, tt.args.conf); !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildCommand() = %v, want %v", got, tt.want)
}
})
}
}

func Test_validArgs(t *testing.T) {
type args struct {
cmd *cobra.Command
args []string
toComplete string
}
tests := []struct {
name string
args args
want []string
want1 cobra.ShellCompDirective
}{
{"test", sqlformat.Gzip, nil},
{"another", sqlformat.Plain, nil},
{"0 arg", args{}, []string{"sql", "sql.gz", "dmp"}, cobra.ShellCompDirectiveFilterFileExt},
{"1 arg", args{args: []string{"sql.gz"}}, nil, cobra.ShellCompDirectiveNoFileComp},
}
for _, tc := range testCases {
tc := tc // capture range variable
t.Run(fmt.Sprintf("%v to %v with error %v", tc.namespace, tc.filetype, tc.err), func(t *testing.T) {
t.Parallel()
filename, err := Filename{
Namespace: tc.namespace,
Format: tc.filetype,
Date: time.Now(),
}.Generate()
if err != tc.err {
t.Error(err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := validArgs(tt.args.cmd, tt.args.args, tt.args.toComplete)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("validArgs() got = %v, want %v", got, tt.want)
}
expected := tc.namespace + "_"
if !strings.HasPrefix(filename, expected) {
t.Errorf("got %v; expected %#v", filename, expected)
if got1 != tt.want1 {
t.Errorf("validArgs() got1 = %v, want %v", got1, tt.want1)
}
})
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/dump/generate_filename.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ type Filename struct {
func (vars Filename) Ext() string {
ext, err := sqlformat.WriteExtension(vars.Format)
if err != nil {
panic(err)
return ""
}
return ext
}

func (vars Filename) Generate() (string, error) {
t, err := template.New("filename").Parse(FilenameTemplate)
return generate(vars, FilenameTemplate)
}

func generate(vars Filename, tmpl string) (string, error) {
t, err := template.New("filename").Parse(tmpl)
if err != nil {
return "", err
}
Expand Down
131 changes: 131 additions & 0 deletions cmd/dump/generate_filename_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package dump

import (
"github.com/clevyr/kubedb/internal/database/sqlformat"
"testing"
"time"
)

func TestFilename_Ext(t *testing.T) {
type fields struct {
Namespace string
Format sqlformat.Format
Date time.Time
}
tests := []struct {
name string
fields fields
want string
}{
{"gzip", fields{"test", sqlformat.Gzip, time.Time{}}, ".sql.gz"},
{"plain", fields{"test", sqlformat.Plain, time.Time{}}, ".sql"},
{"custom", fields{"test", sqlformat.Custom, time.Time{}}, ".dmp"},
{"unknown", fields{"test", sqlformat.Unknown, time.Time{}}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vars := Filename{
Namespace: tt.fields.Namespace,
Format: tt.fields.Format,
Date: tt.fields.Date,
}
got := vars.Ext()
if got != tt.want {
t.Errorf("Ext() got = %v, want %v", got, tt.want)
}
})
}
}

func TestFilename_Generate(t *testing.T) {
type fields struct {
Namespace string
Format sqlformat.Format
Date time.Time
}
tests := []struct {
name string
fields fields
want string
wantErr bool
}{
{"simple", fields{"test", sqlformat.Gzip, time.Time{}}, "test_0001-01-01_000000.sql.gz", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vars := Filename{
Namespace: tt.fields.Namespace,
Format: tt.fields.Format,
Date: tt.fields.Date,
}
got, err := vars.Generate()
if (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Generate() got = %v, want %v", got, tt.want)
}
})
}
}

func TestHelpFilename(t *testing.T) {
tests := []struct {
name string
want string
}{
{"default", "clevyr_2022-01-09_094100.sql.gz"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HelpFilename(); got != tt.want {
t.Errorf("HelpFilename() = %v, want %v", got, tt.want)
}
})
}
}

func Test_generate(t *testing.T) {
type args struct {
vars Filename
tmpl string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
"simple",
args{Filename{"test", sqlformat.Gzip, time.Time{}}, FilenameTemplate},
"test_0001-01-01_000000.sql.gz",
false,
},
{
"invalid template",
args{Filename{"test", sqlformat.Gzip, time.Time{}}, "{{"},
"",
true,
},
{
"invalid function",
args{Filename{"test", sqlformat.Gzip, time.Time{}}, "{{ .NotARealFunction }}"},
"",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := generate(tt.args.vars, tt.args.tmpl)
if (err != nil) != tt.wantErr {
t.Errorf("generate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("generate() got = %v, want %v", got, tt.want)
}
})
}
}
48 changes: 48 additions & 0 deletions cmd/exec/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package exec

import (
"github.com/clevyr/kubedb/internal/command"
"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/database/dialect"
"reflect"
"testing"
)

func Test_buildCommand(t *testing.T) {
pgpassword := command.NewEnv("PGPASSWORD", "")
mysql_pwd := command.NewEnv("MYSQL_PWD", "")

type args struct {
db config.Databaser
conf config.Exec
args []string
}
tests := []struct {
name string
args args
want *command.Builder
}{
{
"command",
args{dialect.Postgres{}, config.Exec{Global: config.Global{Database: "d", Username: "u"}}, []string{"whoami"}},
command.NewBuilder("exec", "whoami"),
},
{
"postgres",
args{dialect.Postgres{}, config.Exec{Global: config.Global{Database: "d", Username: "u"}}, []string{}},
command.NewBuilder(pgpassword, "psql", "--host=127.0.0.1", "--username=u", "--dbname=d"),
},
{
"mariadb-gzip",
args{dialect.MariaDB{}, config.Exec{Global: config.Global{Database: "d", Username: "u"}}, []string{}},
command.NewBuilder(mysql_pwd, "mysql", "--host=127.0.0.1", "--user=u", "--database=d"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildCommand(tt.args.db, tt.args.conf, tt.args.args); !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildCommand() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit d3ffd27

Please sign in to comment.