Skip to content

Commit

Permalink
complete coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Oct 31, 2023
1 parent 6460348 commit 6ed0453
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
9 changes: 3 additions & 6 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ var cmdTable = []Cmd{
if err != nil {
return err
}
str := strconv.Itoa(l.Len())
_, err = w.Write(s2b(&str))
str := base.FormatInt(l.Len())
_, err = w.Write(str)
return err
}},
// bitmap
Expand Down Expand Up @@ -318,7 +318,6 @@ const (

KB = 1024
MB = 1024 * KB
GB = 1024 * MB
)

// Type aliases for structx types.
Expand Down Expand Up @@ -1112,10 +1111,8 @@ func formatSize[T base.Integer](size T) string {
return fmt.Sprintf("%dB", size)
case size < MB:
return fmt.Sprintf("%.1fKB", float64(size)/KB)
case size < GB:
return fmt.Sprintf("%.1fMB", float64(size)/MB)
default:
return fmt.Sprintf("%.1fGB", float64(size)/GB)
return fmt.Sprintf("%.1fMB", float64(size)/MB)
}
}

Expand Down
35 changes: 30 additions & 5 deletions rotom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ func TestDB(t *testing.T) {
// Set
db.Set("foo", []byte("bar"))
db.Set("num", []byte("1"))
db.SetEx("foo1", []byte("bar"), time.Second)
db.HSet("hm", "foo", []byte("bar"))

db.Scan(func(s string, a any, i int64) bool {
if s == "foo" || s == "num" || s == "hm" || s == "foo1" {
return true
}
return false
})

// Get
val, ts, err := db.GetBytes("hm")
assert.Equal(val, nilBytes)
Expand All @@ -72,7 +80,7 @@ func TestDB(t *testing.T) {
assert.Nil(err)

// Keys
assert.ElementsMatch(db.Keys(), []string{"foo", "num", "hm"})
assert.ElementsMatch(db.Keys(), []string{"foo", "num", "hm", "foo1"})

// Rename
ok := db.Rename("num", "num-new")
Expand Down Expand Up @@ -604,20 +612,37 @@ func TestClient(t *testing.T) {
}

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

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

cli.HSet("fakemap", "m1", []byte("m2"))
{
res, err := cli.Get("fakemap")
assert.Nil(res)
assert.Equal(err, base.ErrTypeAssert)
}
{
res, err := cli.HGet("fake", "none")
assert.Nil(res)
assert.Equal(err, base.ErrWrongType)
}
{
res, err := cli.HGet("fakemap", "none")
assert.Nil(res)
assert.Equal(err, base.ErrFieldNotFound)
}
}

0 comments on commit 6ed0453

Please sign in to comment.