Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internal/compress/zstd_option test #621

Merged
merged 2 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/compress/zstd_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/klauspost/compress/zstd"
)

// ZstdOption represents the functional option for zstdCompressor
type ZstdOption func(c *zstdCompressor) error

var (
Expand All @@ -30,6 +31,7 @@ var (
}
)

// WithZstdGob represents the option to set the GobOption to initialize Gob.
func WithZstdGob(opts ...GobOption) ZstdOption {
return func(c *zstdCompressor) error {
gobc, err := NewGob(opts...)
Expand All @@ -41,6 +43,7 @@ func WithZstdGob(opts ...GobOption) ZstdOption {
}
}

// WithZstdCompressionLevel represents the option to set the compress level for zstd.
func WithZstdCompressionLevel(level int) ZstdOption {
return func(c *zstdCompressor) error {
c.eoptions = append(c.eoptions, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)))
Expand Down
257 changes: 88 additions & 169 deletions internal/compress/zstd_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,233 +18,152 @@
package compress

import (
"reflect"
"testing"

"github.com/klauspost/compress/zstd"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/test/comparator"
"go.uber.org/goleak"
)

func TestWithZstdGob(t *testing.T) {
type T = interface{}
type T = zstdCompressor
type args struct {
opts []GobOption
}
type want struct {
obj *T
// Uncomment this line if the option returns an error, otherwise delete it
// err error
err error
}
type test struct {
name string
args args
want want
// Use the first line if the option returns an error. otherwise use the second line
// checkFunc func(want, *T, error) error
// checkFunc func(want, *T) error
name string
args args
want want
checkFunc func(want, *T, error) error
beforeFunc func(args)
afterFunc func(args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}
*/

// Uncomment this block if the option do not returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T) error {
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.c)
}
return nil
}
*/
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}

tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
opts: nil,
},
want: want {
obj: new(T),
},
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
opts: nil,
},
want: want {
obj: new(T),
},
}
}(),
*/
{
name: "set zdtd gob option success",
args: args{
opts: nil,
},
want: want{
obj: &T{
gobc: new(gobCompressor),
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := WithZstdGob(test.args.opts...)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
*/

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithZstdGob(test.args.opts...)
obj := new(T)
got(obj)
if err := test.checkFunc(tt.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
*/
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := WithZstdGob(test.args.opts...)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestWithZstdCompressionLevel(t *testing.T) {
type T = interface{}
type T = zstdCompressor
type args struct {
level int
}
type want struct {
obj *T
// Uncomment this line if the option returns an error, otherwise delete it
// err error
err error
}
type test struct {
name string
args args
want want
// Use the first line if the option returns an error. otherwise use the second line
// checkFunc func(want, *T, error) error
// checkFunc func(want, *T) error
name string
args args
want want
checkFunc func(want, *T, error) error
beforeFunc func(args)
afterFunc func(args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}
*/

// Uncomment this block if the option do not returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T) error {
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.c)
}
return nil
}
*/
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}

zstdComparator := []comparator.Option{
comparator.AllowUnexported(*obj),
comparator.Comparer(func(x, y zstd.EOption) bool {
return !(x == nil && y != nil) || !(x != nil && y == nil)
}),
}

if diff := comparator.Diff(w.obj, obj, zstdComparator...); diff != "" {
return errors.New(diff)
}
return nil
}

tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
level: 0,
},
want: want {
obj: new(T),
},
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
level: 0,
},
want: want {
obj: new(T),
},
}
}(),
*/
{
name: "set compression level success",
args: args{
level: 1,
},
want: want{
obj: func() *zstdCompressor {
c := new(zstdCompressor)
c.eoptions = []zstd.EOption{
zstd.WithEncoderLevel(zstd.SpeedFastest),
}
return c
}(),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := WithZstdCompressionLevel(test.args.level)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
*/

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithZstdCompressionLevel(test.args.level)
obj := new(T)
got(obj)
if err := test.checkFunc(tt.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
*/
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := WithZstdCompressionLevel(test.args.level)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}