Skip to content

Commit

Permalink
feat: improve tests (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
microup authored Dec 2, 2023
1 parent 3110350 commit bda2dc2
Show file tree
Hide file tree
Showing 27 changed files with 308 additions and 320 deletions.
194 changes: 97 additions & 97 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,115 +16,115 @@ linters-settings:

linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- decorder
- depguard
- dogsled
- dupl
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- decorder
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exhaustruct
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exhaustruct
- exportloopref
- forbidigo
- forcetypeassert
- funlen

- gochecknoglobals
- gochecknoinits
- gocognit
- gochecknoglobals
- gochecknoinits
- gocognit

- gocognit
- goconst
- gocritic
- gomnd
- gocognit
- goconst
- gocritic
- gomnd

- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- grouper
- importas
- ineffassign
- interfacebloat
- ireturn
- lll
- loggercheck
- maintidx
- makezero
- misspell
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- grouper
- importas
- ineffassign
- interfacebloat
- ireturn
- lll
- loggercheck
- maintidx
- makezero
- misspell
- nakedret
- nestif

- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- promlinter
- reassign
- revive
- staticcheck
- stylecheck
- tagliatelle
- tenv
- testableexamples
- testpackage
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- varnamelen
- whitespace
- wrapcheck
- wsl
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- promlinter
- reassign
- revive
- staticcheck
- stylecheck
- tagliatelle
- tenv
- testableexamples
- testpackage
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- varnamelen
- whitespace
- wrapcheck
- wsl

- gochecknoglobals
- gochecknoinits
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gochecknoglobals
- gochecknoinits
- gocritic
- gocyclo
- godot
- godox
- goerr113

- goconst
- gocyclo
- godot
- godox
- goerr113
- goconst
- gocyclo
- godot
- godox
- goerr113
- golint
- deadcode
- nosnakecase
- varcheck
- scopelint
- maligned
- nosnakecase
- varcheck
- scopelint
- maligned
- interfacer
- exhaustivestruct
unsused:
- ifshort
- gofumpt
- wastedassign
- depguard
- ifshort
- gofumpt
- wastedassign
- rowserrcheck
- structcheck
- sqlclosecheck
- structcheck
- sqlclosecheck
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/microup/vcache v0.0.0-20230809073841-0dd87922568e
github.com/stretchr/testify v1.8.4
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
Expand All @@ -7,6 +9,10 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/microup/vcache v0.0.0-20230809073841-0dd87922568e h1:K7vMoZfAeVbhsqt+0Fz5yczYwkXEtveyRzbEopvYGOg=
github.com/microup/vcache v0.0.0-20230809073841-0dd87922568e/go.mod h1:GeOzwOgxTozs3eOlL2qFy/fm1ro4CmeAXy7nhP+O/yc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -35,3 +41,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion internal/core/archiv.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
)

// ArchiveFile function for archive file.
// ArchiveFile function for archive file.
func ArchiveFile(fileName string, extension string) error {
file := filepath.Base(fileName) // + ".zip"
file = strings.TrimSuffix(file, filepath.Ext(file)) + extension // ".zip"
Expand Down
47 changes: 20 additions & 27 deletions internal/core/archiv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"testing"
"vbalancer/internal/core"

"github.com/stretchr/testify/assert"
)

// TestArchive this is a test function for `archive()`.
Expand All @@ -16,7 +18,6 @@ func TestArchiveFile(t *testing.T) {
helperArchiveFile(t)
}

//nolint:funlen,cyclop
func helperArchiveFile(t *testing.T) {
t.Helper()

Expand All @@ -28,58 +29,50 @@ func helperArchiveFile(t *testing.T) {
extension := ".zip"

testFile, err := os.Create(fileName)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}

assert.Nil(t, err, "failed to create test file")

defer os.Remove(fileName)

_, err = testFile.Write([]byte("test data"))
if err != nil {
t.Fatalf("Failed to write data to test file: %v", err)
}

assert.Nil(t, err, "failed to write data to test file")

err = testFile.Close()
if err != nil {
t.Fatalf("Failed to close test file: %v", err)
}

assert.Nil(t, err, "failed to close test file")

err = core.ArchiveFile(fileName, extension)
if err != nil {
t.Fatalf("Archiving failed: %v", err)
}

assert.Nil(t, err, "archiving failed")

archivedFile := strings.TrimSuffix(fileName, filepath.Ext(fileName)) + extension
if _, err = os.Stat(archivedFile); os.IsNotExist(err) {
t.Fatalf("Archived file does not exist: %v", err)
assert.FailNow(t, "archived file does not exist", err)
}

assert.Nil(t, err, "archiving failed")

defer os.Remove(archivedFile)

zipFile, err := zip.OpenReader(archivedFile)
if err != nil {
t.Fatalf("Failed to open archived file: %v", err)
assert.FailNow(t, "failed to open archived file: %v", err)
}
defer zipFile.Close()

if len(zipFile.File) != 1 {
t.Fatalf("Unexpected number of files in archived file: %d", len(zipFile.File))
}
assert.Equal(t, 1, len(zipFile.File), "unexpected number of files in archived file")

fileInArchive := zipFile.File[0]
zipFileContent, err := fileInArchive.Open()

if err != nil {
t.Fatalf("Failed to open file in archive: %v", err)
}
assert.Nil(t, err, "failed to open file in archive")

defer zipFileContent.Close()

data, err := io.ReadAll(zipFileContent)
if err != nil {
t.Fatalf("Failed to read data from file in archive: %v", err)
}

if string(data) != "test data" {
t.Fatalf("Unexpected data in archived file: %s", string(data))
}
assert.Nil(t, err, "failed to read data from file in archive")

assert.Equal(t, "test data", string(data), "unexpected data in archived file")
}
8 changes: 4 additions & 4 deletions internal/core/converts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const (
place = 2
prec = -1
place = 2
prec = -1
)

// HumanFileSize returns a human-readable file size.
Expand All @@ -33,8 +33,8 @@ func HumanFileSize(size float64) string {
}

// Round rounds a float to a given number of decimal places.
func Round(val float64, roundOn float64, places int) float64 {
func Round(val float64, _ float64, places int) float64 {
pow := math.Pow(types.PowX, float64(places))

return math.Round(val*pow) / pow
}
Loading

0 comments on commit bda2dc2

Please sign in to comment.