Skip to content

Commit

Permalink
complete coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Oct 30, 2023
1 parent 370c21f commit 45ecc1a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
32 changes: 15 additions & 17 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ type Cmd struct {

// cmdTable defines the argsNum and callback function required for the operation.
var cmdTable = []Cmd{
{Response, 2, func(_ *Engine, _ [][]byte, _ base.Writer) error {
return nil
}},
{Response, 2, nil},
{OpSetTx, 4, func(e *Engine, args [][]byte, _ base.Writer) error {
// type, key, ts, val
ts := base.ParseInt[int64](args[2]) * timeCarry
Expand Down Expand Up @@ -269,7 +267,7 @@ var cmdTable = []Cmd{
return e.BitXor(*b2s(args[0]), *b2s(args[1]), *b2s(args[2]))
}},
// zset
{OpZAdd, 4, func(e *Engine, args [][]byte, w base.Writer) error {
{OpZAdd, 4, func(e *Engine, args [][]byte, _ base.Writer) error {
// key, score, val
s, err := strconv.ParseFloat(*b2s(args[2]), 64)
if err != nil {
Expand Down Expand Up @@ -378,7 +376,7 @@ type Engine struct {
ctx context.Context
cancel context.CancelFunc

loading bool //if db loading encode() not allowed.
loading bool // if db loading encode() not allowed.

buf *bytes.Buffer
rwbuf *bytes.Buffer
Expand Down Expand Up @@ -1041,42 +1039,42 @@ func (e *Engine) shrink() {
}

// fetchMap
func (e *Engine) fetchMap(key string, setWhenNotExist ...bool) (m Map, err error) {
func (e *Engine) fetchMap(key string, setnx ...bool) (m Map, err error) {
return fetch(e, key, func() Map {
return structx.NewSyncMap[string, []byte]()
}, setWhenNotExist...)
}, setnx...)
}

// fetchSet
func (e *Engine) fetchSet(key string, setWhenNotExist ...bool) (s Set, err error) {
func (e *Engine) fetchSet(key string, setnx ...bool) (s Set, err error) {
return fetch(e, key, func() Set {
return structx.NewSet[string]()
}, setWhenNotExist...)
}, setnx...)
}

// fetchList
func (e *Engine) fetchList(key string, setWhenNotExist ...bool) (m List, err error) {
func (e *Engine) fetchList(key string, setnx ...bool) (m List, err error) {
return fetch(e, key, func() List {
return structx.NewList[string]()
}, setWhenNotExist...)
}, setnx...)
}

// fetchBitMap
func (e *Engine) fetchBitMap(key string, setWhenNotExist ...bool) (bm BitMap, err error) {
func (e *Engine) fetchBitMap(key string, setnx ...bool) (bm BitMap, err error) {
return fetch(e, key, func() BitMap {
return structx.NewBitmap()
}, setWhenNotExist...)
}, setnx...)
}

// fetchZSet
func (e *Engine) fetchZSet(key string, setWhenNotExist ...bool) (z ZSet, err error) {
func (e *Engine) fetchZSet(key string, setnx ...bool) (z ZSet, err error) {
return fetch(e, key, func() ZSet {
return structx.NewZSet[string, float64, []byte]()
}, setWhenNotExist...)
}, setnx...)
}

// fetch
func fetch[T any](e *Engine, key string, new func() T, setWhenNotExist ...bool) (T, error) {
func fetch[T any](e *Engine, key string, new func() T, setnx ...bool) (T, error) {
m, _, ok := e.m.Get(key)
if ok {
m, ok := m.(T)
Expand All @@ -1087,7 +1085,7 @@ func fetch[T any](e *Engine, key string, new func() T, setWhenNotExist ...bool)
return v, base.ErrWrongType
}
vptr := new()
if len(setWhenNotExist) > 0 && setWhenNotExist[0] {
if len(setnx) > 0 && setnx[0] {
e.m.Set(key, vptr)
}

Expand Down
18 changes: 18 additions & 0 deletions rotom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,22 @@ func TestClient(t *testing.T) {
assert.Nil(err)
assert.True(ok)
}

// Error
cli.Set("fakemap", []byte("123"))

res, err := cli.HLen("fakemap")
assert.Equal(res, 0)
assert.Equal(err, base.ErrWrongType)
{
res, err := cli.HKeys("fakemap")
var nilSlice []string
assert.Equal(res, nilSlice)
assert.Equal(err, base.ErrWrongType)
}
{
res, err := cli.HRemove("fakemap", "foo")
assert.False(res)
assert.Equal(err, base.ErrWrongType)
}
}

0 comments on commit 45ecc1a

Please sign in to comment.