Skip to content

Commit

Permalink
add generator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adwski committed Jan 26, 2024
1 parent 8b5504c commit 9970646
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ vendor/

# Reports
report.xml
profile.cov
coverage.html

.DS_Store

Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ statictest:

test-nginx:
docker run --rm -it --entrypoint nginx -v ./docker/compose/nginx.conf:/etc/nginx/nginx.conf nginx:1.25.3-bookworm -t

.PHONY: unittests
unittests:
go test ./... -v -count=1 -cover -coverpkg=./... -coverprofile=profile.cov ./...

.PHONY: cover
cover:
go tool cover -html profile.cov -o coverage.html
1 change: 0 additions & 1 deletion docker/compose/docker-compose.api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ services:
environment:
VIDI_API_PREFIX: /api/users
VIDI_DATABASE_DSN: postgres://userapi:userapi@postgres:5432/userapi?sslmode=disable
VIDI_DATABASE_SALT: q1w2e3r4t5
VIDI_SERVER_ADDRESS: ":8080"
expose:
- 8080
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
github.com/valyala/fasthttp v1.51.0
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.17.0
Expand Down Expand Up @@ -62,6 +63,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand Down
76 changes: 76 additions & 0 deletions internal/generators/generators_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package generators

import (
"encoding/base64"
"errors"

Check failure on line 5 in internal/generators/generators_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/org/project (goimports)
"github.com/gofrs/uuid/v5"
"github.com/stretchr/testify/assert"
"testing"
)

func TestID_Get(t *testing.T) {
gen := NewID()

uuid.NewGenWithOptions()

u, err := gen.Get()
assert.NoError(t, err)

b, err := base64.RawURLEncoding.DecodeString(u)
assert.NoError(t, err)

_, err = uuid.FromBytes(b)
assert.NoError(t, err)
}

func TestID_GetError(t *testing.T) {
gen := NewID()
gen.gen = uuid.NewGenWithOptions(uuid.WithRandomReader(&errReader{}))

_, err := gen.Get()
assert.Error(t, err)
}

func TestID_GetString(t *testing.T) {
gen := NewID()

u, err := gen.GetString()
assert.NoError(t, err)

_, err = uuid.FromString(u)
assert.NoError(t, err)
}

func TestID_GetStringError(t *testing.T) {
gen := NewID()
gen.gen = uuid.NewGenWithOptions(uuid.WithRandomReader(&errReader{}))

_, err := gen.GetString()
assert.Error(t, err)
}

func TestID_GetStringOrPanic(t *testing.T) {
gen := NewID()

u := gen.GetStringOrPanic()

_, err := uuid.FromString(u)
assert.NoError(t, err)
}

func TestID_GetStringOrPanicPanic(t *testing.T) {
gen := NewID()
gen.gen = uuid.NewGenWithOptions(uuid.WithRandomReader(&errReader{}))

pFunc := func() {
_ = gen.GetStringOrPanic()
}

assert.Panics(t, pFunc)
}

type errReader struct{}

func (e errReader) Read(_ []byte) (int, error) {
return 0, errors.New("error")
}

0 comments on commit 9970646

Please sign in to comment.