-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,869 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.