✅ Add tests for New
This commit is contained in:
parent
749033170d
commit
40ed936c47
3 changed files with 62 additions and 0 deletions
44
ezcache_test.go
Normal file
44
ezcache_test.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package ezcache_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"codeberg.org/danjones000/ezcache"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var fetcher ezcache.Fetcher[uint8, string] = func(key uint8) (string, error) { return fmt.Sprintf("%d", key), nil }
|
||||
|
||||
func TestNewHappy(t *testing.T) {
|
||||
cache, err := ezcache.New(fetcher, 1)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, cache)
|
||||
}
|
||||
|
||||
func TestNewNilFetcher(t *testing.T) {
|
||||
cache, err := ezcache.New[uint8, string](nil, 1)
|
||||
|
||||
assert.ErrorIs(t, err, ezcache.ErrInvalidFetcher)
|
||||
assert.Nil(t, cache)
|
||||
}
|
||||
|
||||
func TestNewBadExpiry(tt *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
exp time.Duration
|
||||
}{
|
||||
{"zero", 0},
|
||||
{"negative", -5},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
tt.Run(tc.name, func(t *testing.T) {
|
||||
cache, err := ezcache.New(fetcher, tc.exp)
|
||||
|
||||
assert.ErrorIs(t, err, ezcache.ErrInvalidExpiry)
|
||||
assert.Nil(t, cache)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue