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 test for internal/errors/compressor.go #870

Merged
merged 10 commits into from
Dec 18, 2020
14 changes: 10 additions & 4 deletions internal/errors/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,35 @@
package errors

var (
// internal compressor
// ErrInvalidCompressionLevel represents a function to generate an error of invalid compression level.
ErrInvalidCompressionLevel = func(level int) error {
return Errorf("invalid compression level: %d", level)
}

// Compressor
// ErrCompressorNameNotFound represents a function to generate an error of compressor not found.
ErrCompressorNameNotFound = func(name string) error {
return Errorf("compressor %s not found", name)
}

// ErrCompressedDataNotFound returns an error of compressed data is not found.
ErrCompressedDataNotFound = New("compressed data not found")

// ErrDecompressedDataNotFound returns an error of decompressed data is not found.
ErrDecompressedDataNotFound = New("decompressed data not found")

// ErrCompressFailed returns an error of compress failed.
ErrCompressFailed = New("compress failed")

// ErrDecompressFailed returns an error of decompressing failed.
ErrDecompressFailed = New("decompress failed")

// ErrCompressorRegistererIsNotRunning represents a function to generate an error of compressor registerers is not running.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
line is 124 characters (lll)

ErrCompressorRegistererIsNotRunning = func() error {
return Errorf("compressor registerers is not running")
return New("compressor registerers is not running")
}

// ErrCompressorRegistererChannelIsFull represents a function to generate an error that compressor registerer channel is full.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
line is 127 characters (lll)

ErrCompressorRegistererChannelIsFull = func() error {
return Errorf("compressor registerer channel is full")
return New("compressor registerer channel is full")
}
)
255 changes: 255 additions & 0 deletions internal/errors/compressor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package errors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
package should be errors_test instead of errors (testpackage)


import (
"math"
"testing"
)

func TestErrInvalidCompressionLevel(t *testing.T) {
type args struct {
level int
}
type want struct {
want error
}
type test struct {
name string
args args
want want
checkFunc func(want, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "returns error when compression leve is 100",
args: args{
level: 100,
},
want: want{
want: New("invalid compression level: 100"),
},
},
{
name: "returns error when compression leve is 0",
args: args{
level: 0,
hlts2 marked this conversation as resolved.
Show resolved Hide resolved
},
want: want{
want: New("invalid compression level: 0"),
},
},
{
name: "returns error when compression leve is the maximum number of int",
args: args{
level: int(math.MaxInt64),
},
want: want{
want: Errorf("invalid compression level: %d", int(math.MaxInt64)),
},
},
{
name: "returns error when compression leve is the minimum number of int",
args: args{
level: int(math.MinInt64),
},
want: want{
want: Errorf("invalid compression level: %d", int(math.MinInt64)),
},
},
}

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

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

func TestErrCompressorNameNotFound(t *testing.T) {
type args struct {
name string
}
type want struct {
want error
}
type test struct {
name string
args args
want want
checkFunc func(want, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "returns error when name is `gob`",
args: args{
name: "gob",
},
want: want{
want: New("compressor gob not found"),
},
},
{
name: "returns error when name is empty",
args: args{
name: "",
},
want: want{
want: New("compressor not found"),
},
},
}

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

got := ErrCompressorNameNotFound(test.args.name)
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrCompressorRegistererIsNotRunning(t *testing.T) {
type want struct {
want error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "returns error",
want: want{
want: New("compressor registerers is not running"),
},
},
}

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

got := ErrCompressorRegistererIsNotRunning()
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrCompressorRegistererChannelIsFull(t *testing.T) {
type want struct {
want error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got error) error {
if !Is(got, w.want) {
return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "returns error",
want: want{
want: New("compressor registerer channel is full"),
},
},
}

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

got := ErrCompressorRegistererChannelIsFull()
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}