Skip to content

Commit

Permalink
test: add tests for cache package
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuan Nguyen committed Feb 27, 2024
1 parent ff698aa commit 3c78845
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 30 deletions.
20 changes: 20 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@ package cache

import (
"context"
"errors"
"strings"
"time"

"github.com/kanthorlabs/common/cache/config"
"github.com/kanthorlabs/common/logging"
"github.com/kanthorlabs/common/patterns"
)

func New(conf *config.Config, logger logging.Logger) (Cache, error) {
if err := conf.Validate(); err != nil {
return nil, err
}

if strings.HasPrefix(conf.Uri, "memory") {
return NewMemory(conf, logger)
}

if strings.HasPrefix(conf.Uri, "redis") {
return NewRedis(conf, logger)
}

return nil, errors.New("CACHE.SCHEME_UNKNOWN.ERROR")
}

type Cache interface {
patterns.Connectable
Get(ctx context.Context, key string) ([]byte, error)
Expand Down
40 changes: 39 additions & 1 deletion cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
package cache

import "github.com/kanthorlabs/common/cache/config"
import (
"testing"

"github.com/kanthorlabs/common/cache/config"
"github.com/kanthorlabs/common/testify"
"github.com/stretchr/testify/require"
)

var testconf = &config.Config{
Uri: "memory://",
}

func TestCache(t *testing.T) {
t.Run("OK - memory", func(st *testing.T) {
conf := &config.Config{
Uri: "memory://",
}
_, err := New(conf, testify.Logger())
require.Nil(st, err)
})

t.Run("OK - redis", func(st *testing.T) {
conf := &config.Config{
Uri: "redis://localhost:6379/0",
}
_, err := New(conf, testify.Logger())
require.Nil(st, err)
})

t.Run("KO - unknown error", func(st *testing.T) {
conf := &config.Config{
Uri: "tcp://127.0.0.1",
}
_, err := New(conf, testify.Logger())
require.ErrorContains(st, err, "CACHE.SCHEME_UNKNOWN.ERROR")
})

t.Run("KO - configuration error", func(st *testing.T) {
conf := &config.Config{}
_, err := New(conf, testify.Logger())
require.ErrorContains(st, err, "CACHE.CONFIG.")
})
}
2 changes: 1 addition & 1 deletion checksum
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bb7480ce99fb01c7a846dd18388beb3729e56a991f590ec091874b00ff469fe2
189bce743cf9689746de8bb3df1521534934aa2ce73a96db8091e9ffe74b2dbf
55 changes: 31 additions & 24 deletions cover.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
mode: set
github.com/kanthorlabs/common/cache/config/config.go:9.38,13.2 1 1
github.com/kanthorlabs/common/cache/cache.go:14.69,15.40 1 1
github.com/kanthorlabs/common/cache/cache.go:15.40,17.3 1 1
github.com/kanthorlabs/common/cache/cache.go:19.2,19.43 1 1
github.com/kanthorlabs/common/cache/cache.go:19.43,21.3 1 1
github.com/kanthorlabs/common/cache/cache.go:23.2,23.42 1 1
github.com/kanthorlabs/common/cache/cache.go:23.42,25.3 1 1
github.com/kanthorlabs/common/cache/cache.go:27.2,27.54 1 1
github.com/kanthorlabs/common/cache/memory.go:16.75,17.40 1 1
github.com/kanthorlabs/common/cache/memory.go:17.40,19.3 1 1
github.com/kanthorlabs/common/cache/memory.go:21.2,22.49 2 1
Expand Down Expand Up @@ -242,7 +249,7 @@ github.com/kanthorlabs/common/gateway/gateway.go:66.2,66.18 1 1
github.com/kanthorlabs/common/gateway/gateway.go:69.51,70.26 1 1
github.com/kanthorlabs/common/gateway/gateway.go:70.26,73.3 2 1
github.com/kanthorlabs/common/gateway/gateway.go:75.2,75.25 1 1
github.com/kanthorlabs/common/gateway/gateway.go:78.28,80.47 2 0
github.com/kanthorlabs/common/gateway/gateway.go:78.28,80.47 2 1
github.com/kanthorlabs/common/gateway/gateway.go:80.47,82.3 1 0
github.com/kanthorlabs/common/gateway/gateway.go:85.56,89.26 3 1
github.com/kanthorlabs/common/gateway/gateway.go:89.26,91.3 1 1
Expand Down Expand Up @@ -405,15 +412,6 @@ github.com/kanthorlabs/common/passport/config/strategy.go:35.2,35.37 1 1
github.com/kanthorlabs/common/passport/config/strategy.go:35.37,36.52 1 1
github.com/kanthorlabs/common/passport/config/strategy.go:36.52,38.4 1 1
github.com/kanthorlabs/common/passport/config/strategy.go:41.2,41.12 1 0
github.com/kanthorlabs/common/passport/entities/account.go:21.40,23.2 1 1
github.com/kanthorlabs/common/passport/entities/account.go:25.38,33.2 1 1
github.com/kanthorlabs/common/passport/entities/account.go:35.39,46.2 3 1
github.com/kanthorlabs/common/passport/entities/account.go:48.52,49.28 1 1
github.com/kanthorlabs/common/passport/entities/account.go:49.28,51.3 1 1
github.com/kanthorlabs/common/passport/entities/account.go:52.2,52.52 1 1
github.com/kanthorlabs/common/passport/entities/credentials.go:10.55,11.77 1 1
github.com/kanthorlabs/common/passport/entities/credentials.go:11.77,13.3 1 1
github.com/kanthorlabs/common/passport/entities/credentials.go:14.2,17.3 1 1
github.com/kanthorlabs/common/healthcheck/background/client.go:13.65,14.40 1 1
github.com/kanthorlabs/common/healthcheck/background/client.go:14.40,16.3 1 1
github.com/kanthorlabs/common/healthcheck/background/client.go:18.2,18.33 1 1
Expand Down Expand Up @@ -456,6 +454,15 @@ github.com/kanthorlabs/common/healthcheck/background/server.go:62.34,64.5 1 1
github.com/kanthorlabs/common/healthcheck/background/server.go:65.4,65.49 1 1
github.com/kanthorlabs/common/healthcheck/background/server.go:65.49,67.5 1 1
github.com/kanthorlabs/common/healthcheck/background/server.go:72.48,77.2 3 1
github.com/kanthorlabs/common/passport/entities/account.go:21.40,23.2 1 1
github.com/kanthorlabs/common/passport/entities/account.go:25.38,33.2 1 1
github.com/kanthorlabs/common/passport/entities/account.go:35.39,46.2 3 1
github.com/kanthorlabs/common/passport/entities/account.go:48.52,49.28 1 1
github.com/kanthorlabs/common/passport/entities/account.go:49.28,51.3 1 1
github.com/kanthorlabs/common/passport/entities/account.go:52.2,52.52 1 1
github.com/kanthorlabs/common/passport/entities/credentials.go:10.55,11.77 1 1
github.com/kanthorlabs/common/passport/entities/credentials.go:11.77,13.3 1 1
github.com/kanthorlabs/common/passport/entities/credentials.go:14.2,17.3 1 1
github.com/kanthorlabs/common/persistence/database/database.go:11.61,13.16 2 1
github.com/kanthorlabs/common/persistence/database/database.go:13.16,15.3 1 1
github.com/kanthorlabs/common/persistence/database/database.go:16.2,17.16 2 1
Expand All @@ -471,20 +478,6 @@ github.com/kanthorlabs/common/persistence/database/query.go:53.24,55.3 1 1
github.com/kanthorlabs/common/persistence/database/query.go:57.2,57.28 1 1
github.com/kanthorlabs/common/persistence/database/query.go:57.28,59.3 1 1
github.com/kanthorlabs/common/persistence/database/query.go:61.2,61.11 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:17.38,21.16 2 1
github.com/kanthorlabs/common/persistence/database/config/config.go:21.16,23.3 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:25.2,25.31 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:25.31,26.46 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:26.46,28.4 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:31.2,31.12 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:8.60,15.50 6 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:15.50,17.3 1 0
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:18.2,18.40 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:18.40,20.3 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:22.2,22.28 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:29.39,30.49 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:30.49,32.3 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:33.2,33.12 1 1
github.com/kanthorlabs/common/passport/strategies/ask.go:15.72,16.40 1 1
github.com/kanthorlabs/common/passport/strategies/ask.go:16.40,18.3 1 1
github.com/kanthorlabs/common/passport/strategies/ask.go:20.2,21.31 2 1
Expand Down Expand Up @@ -562,6 +555,20 @@ github.com/kanthorlabs/common/passport/strategies/durability.go:132.46,134.4 1 1
github.com/kanthorlabs/common/passport/strategies/durability.go:136.3,139.23 3 1
github.com/kanthorlabs/common/passport/strategies/durability.go:139.23,141.4 1 0
github.com/kanthorlabs/common/passport/strategies/durability.go:143.3,143.13 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:17.38,21.16 2 1
github.com/kanthorlabs/common/persistence/database/config/config.go:21.16,23.3 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:25.2,25.31 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:25.31,26.46 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:26.46,28.4 1 1
github.com/kanthorlabs/common/persistence/database/config/config.go:31.2,31.12 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:8.60,15.50 6 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:15.50,17.3 1 0
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:18.2,18.40 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:18.40,20.3 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:22.2,22.28 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:29.39,30.49 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:30.49,32.3 1 1
github.com/kanthorlabs/common/persistence/database/config/wrapper.go:33.2,33.12 1 1
github.com/kanthorlabs/common/persistence/datastore/config/config.go:15.38,19.16 2 1
github.com/kanthorlabs/common/persistence/datastore/config/config.go:19.16,21.3 1 1
github.com/kanthorlabs/common/persistence/datastore/config/config.go:23.2,23.31 1 1
Expand Down
2 changes: 1 addition & 1 deletion coverage.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
93.9
94.1
2 changes: 1 addition & 1 deletion gateway/httpx/httpx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var testconf = &config.Config{
},
}

func TestNew(t *testing.T) {
func TestHttpx(t *testing.T) {
t.Run("OK", func(st *testing.T) {
s, err := New(testconf, testify.Logger())
require.Nil(st, err)
Expand Down
2 changes: 1 addition & 1 deletion persistence/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
func TestDatabase(t *testing.T) {
t.Run("OK", func(st *testing.T) {
provider, err := configuration.New(testdata.Fake.Color().SafeColorName())
require.Nil(st, err)
Expand Down
2 changes: 1 addition & 1 deletion persistence/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
func TestDatastore(t *testing.T) {
t.Run("OK", func(st *testing.T) {
provider, err := configuration.New(testdata.Fake.Color().SafeColorName())
require.Nil(st, err)
Expand Down

0 comments on commit 3c78845

Please sign in to comment.