Fix Sonar Part2
This commit is contained in:
114
adapter/cache/ssdb/ssdb_test.go
vendored
114
adapter/cache/ssdb/ssdb_test.go
vendored
@@ -7,6 +7,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/beego/beego/v2/adapter/cache"
|
||||
)
|
||||
|
||||
@@ -25,95 +27,59 @@ func TestSsdbcacheCache(t *testing.T) {
|
||||
}
|
||||
|
||||
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
|
||||
if err != nil {
|
||||
t.Error(initError)
|
||||
}
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.False(t, ssdb.IsExist("ssdb"))
|
||||
// test put and exist
|
||||
if ssdb.IsExist("ssdb") {
|
||||
t.Error(checkError)
|
||||
}
|
||||
timeoutDuration := 10 * time.Second
|
||||
timeoutDuration := 3 * time.Second
|
||||
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
|
||||
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
|
||||
t.Error(setError, err)
|
||||
}
|
||||
if !ssdb.IsExist("ssdb") {
|
||||
t.Error(checkError)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put("ssdb", "ssdb", timeoutDuration))
|
||||
assert.True(t, ssdb.IsExist("ssdb"))
|
||||
|
||||
// Get test done
|
||||
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
|
||||
t.Error(setError, err)
|
||||
}
|
||||
assert.NotNil(t, ssdb.Put("ssdb", "ssdb", timeoutDuration))
|
||||
|
||||
if v := ssdb.Get("ssdb"); v != "ssdb" {
|
||||
t.Error("get Error")
|
||||
}
|
||||
assert.Equal(t, "ssdb", ssdb.Get("ssdb"))
|
||||
|
||||
// inc/dec test done
|
||||
if err = ssdb.Put("ssdb", "2", timeoutDuration); err != nil {
|
||||
t.Error(setError, err)
|
||||
}
|
||||
if err = ssdb.Incr("ssdb"); err != nil {
|
||||
t.Error("incr Error", err)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put("ssdb", "2", timeoutDuration))
|
||||
|
||||
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
|
||||
t.Error(getError)
|
||||
}
|
||||
assert.Nil(t, ssdb.Incr("ssdb"))
|
||||
|
||||
if err = ssdb.Decr("ssdb"); err != nil {
|
||||
t.Error("decr error")
|
||||
}
|
||||
v, err := strconv.Atoi(ssdb.Get("ssdb").(string))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 3, v)
|
||||
|
||||
assert.Nil(t, ssdb.Decr("ssdb"))
|
||||
|
||||
assert.Nil(t, ssdb.Put("ssdb", "3", timeoutDuration))
|
||||
|
||||
// test del
|
||||
if err = ssdb.Put("ssdb", "3", timeoutDuration); err != nil {
|
||||
t.Error(setError, err)
|
||||
}
|
||||
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
|
||||
t.Error(getError)
|
||||
}
|
||||
if err := ssdb.Delete("ssdb"); err == nil {
|
||||
if ssdb.IsExist("ssdb") {
|
||||
t.Error("delete err")
|
||||
}
|
||||
}
|
||||
v, err = strconv.Atoi(ssdb.Get("ssdb").(string))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 3, v)
|
||||
|
||||
assert.Nil(t, ssdb.Delete("ssdb"))
|
||||
assert.False(t, ssdb.IsExist("ssdb"))
|
||||
|
||||
// test string
|
||||
if err = ssdb.Put("ssdb", "ssdb", -10*time.Second); err != nil {
|
||||
t.Error(setError, err)
|
||||
}
|
||||
if !ssdb.IsExist("ssdb") {
|
||||
t.Error(checkError)
|
||||
}
|
||||
if v := ssdb.Get("ssdb").(string); v != "ssdb" {
|
||||
t.Error(getError)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put("ssdb", "ssdb", -10*time.Second))
|
||||
|
||||
assert.True(t, ssdb.IsExist("ssdb"))
|
||||
assert.Equal(t, "ssdb", ssdb.Get("ssdb"))
|
||||
|
||||
// test GetMulti done
|
||||
if err = ssdb.Put("ssdb1", "ssdb1", -10*time.Second); err != nil {
|
||||
t.Error(setError, err)
|
||||
}
|
||||
if !ssdb.IsExist("ssdb1") {
|
||||
t.Error(checkError)
|
||||
}
|
||||
vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error(getMultiError)
|
||||
}
|
||||
if vv[0].(string) != "ssdb" {
|
||||
t.Error(getMultiError)
|
||||
}
|
||||
if vv[1].(string) != "ssdb1" {
|
||||
t.Error(getMultiError)
|
||||
}
|
||||
assert.Nil(t, ssdb.Put("ssdb1", "ssdb1", -10*time.Second))
|
||||
assert.True(t, ssdb.IsExist("ssdb1") )
|
||||
|
||||
vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"})
|
||||
assert.Equal(t, 2, len(vv))
|
||||
|
||||
assert.Equal(t, "ssdb", vv[0])
|
||||
assert.Equal(t, "ssdb1", vv[1])
|
||||
|
||||
assert.Nil(t, ssdb.ClearAll())
|
||||
assert.False(t, ssdb.IsExist("ssdb"))
|
||||
assert.False(t, ssdb.IsExist("ssdb1"))
|
||||
// test clear all done
|
||||
if err = ssdb.ClearAll(); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
if ssdb.IsExist("ssdb") || ssdb.IsExist("ssdb1") {
|
||||
t.Error(checkError)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user