Shivanshu Raj Shrivastava efd4e30edf
fix: publish signoz as package (#7378)
Signed-off-by: Shivanshu Raj Shrivastava <shivanshu1333@gmail.com>
2025-03-20 15:31:41 +00:00

92 lines
2.5 KiB
Go

package redis
import (
"testing"
"time"
"github.com/SigNoz/signoz/pkg/query-service/cache/status"
"github.com/go-redis/redismock/v8"
)
func TestStore(t *testing.T) {
db, mock := redismock.NewClientMock()
c := WithClient(db)
mock.ExpectSet("key", []byte("value"), 10*time.Second).RedisNil()
_ = c.Store("key", []byte("value"), 10*time.Second)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestRetrieve(t *testing.T) {
db, mock := redismock.NewClientMock()
c := WithClient(db)
mock.ExpectSet("key", []byte("value"), 10*time.Second).RedisNil()
_ = c.Store("key", []byte("value"), 10*time.Second)
mock.ExpectGet("key").SetVal("value")
data, retrieveStatus, err := c.Retrieve("key", false)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if retrieveStatus != status.RetrieveStatusHit {
t.Errorf("expected status %d, got %d", status.RetrieveStatusHit, retrieveStatus)
}
if string(data) != "value" {
t.Errorf("expected value %s, got %s", "value", string(data))
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestSetTTL(t *testing.T) {
db, mock := redismock.NewClientMock()
c := WithClient(db)
mock.ExpectSet("key", []byte("value"), 10*time.Second).RedisNil()
_ = c.Store("key", []byte("value"), 10*time.Second)
mock.ExpectExpire("key", 4*time.Second).RedisNil()
c.SetTTL("key", 4*time.Second)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestRemove(t *testing.T) {
db, mock := redismock.NewClientMock()
c := WithClient(db)
mock.ExpectSet("key", []byte("value"), 10*time.Second).RedisNil()
_ = c.Store("key", []byte("value"), 10*time.Second)
mock.ExpectDel("key").RedisNil()
c.Remove("key")
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestBulkRemove(t *testing.T) {
db, mock := redismock.NewClientMock()
c := WithClient(db)
mock.ExpectSet("key", []byte("value"), 10*time.Second).RedisNil()
_ = c.Store("key", []byte("value"), 10*time.Second)
mock.ExpectSet("key2", []byte("value2"), 10*time.Second).RedisNil()
_ = c.Store("key2", []byte("value2"), 10*time.Second)
mock.ExpectDel("key", "key2").RedisNil()
c.BulkRemove([]string{"key", "key2"})
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}