diff --git a/internal/compress/zstd_option.go b/internal/compress/zstd_option.go index 633a8a65ee..8578b3a471 100644 --- a/internal/compress/zstd_option.go +++ b/internal/compress/zstd_option.go @@ -21,6 +21,7 @@ import ( "github.com/klauspost/compress/zstd" ) +// ZstdOption represents the functional option for zstdCompressor type ZstdOption func(c *zstdCompressor) error var ( @@ -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...) @@ -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))) diff --git a/internal/compress/zstd_option_test.go b/internal/compress/zstd_option_test.go index 1af3685d76..fb5c4d8bad 100644 --- a/internal/compress/zstd_option_test.go +++ b/internal/compress/zstd_option_test.go @@ -18,88 +18,60 @@ 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) } @@ -107,112 +79,75 @@ func TestWithZstdGob(t *testing.T) { 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) } @@ -220,31 +155,15 @@ func TestWithZstdCompressionLevel(t *testing.T) { 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) + } }) } }