more unittests implemented

This commit is contained in:
Erkan Ozsoy
2023-07-03 16:31:04 +03:00
parent 4137501136
commit e42a4816ac
2 changed files with 100 additions and 6 deletions

View File

@@ -36,10 +36,15 @@ func (s *Storage) Get(key string) ([]byte, error) {
return nil, nil
}
item, err := s.db.Get(context.TODO(), key)
if err != nil {
return nil, err
}
if len(item.Kvs) <= 0 {
return nil, err
}
return item.Kvs[0].Value, nil
}
@@ -49,7 +54,8 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
return nil
}
lease, err := s.db.Grant(context.TODO(), exp.Nanoseconds())
lease, err := s.db.Grant(context.TODO(), int64(exp.Seconds()))
if err != nil {
return err
}

View File

@@ -1,14 +1,13 @@
package etcd
import (
"fmt"
"github.com/gofiber/utils"
"testing"
"time"
)
func Test_Etcd_Set(t *testing.T) {
fmt.Println("==> Etcd_Set")
testStore := New(Config{
func TestSetEtcd_ShouldReturnNoError(t *testing.T) {
testStorage := New(Config{
Endpoints: []string{"localhost:2379"},
})
@@ -17,6 +16,95 @@ func Test_Etcd_Set(t *testing.T) {
val = []byte("doe")
)
err := testStore.Set(key, val, 0)
err := testStorage.Set(key, val, 0)
utils.AssertEqual(t, nil, err)
}
func TestGetEtcd_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) {
testStorage := New(Config{
Endpoints: []string{"localhost:2379"},
})
val, err := testStorage.Get("not_found_key")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 0, len(val))
}
func TestSetAndGet_GetShouldReturn_SettedValueWithoutError(t *testing.T) {
testStorage := New(Config{
Endpoints: []string{"localhost:2379"},
})
err := testStorage.Set("test", []byte("fiber_test_value"), 0)
utils.AssertEqual(t, nil, err)
val, err := testStorage.Get("test")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, val, []byte("fiber_test_value"))
}
func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
testStorage := New(Config{
Endpoints: []string{"localhost:2379"},
})
err := testStorage.Set("test", []byte("fiber_test_value"), 3*time.Second)
utils.AssertEqual(t, nil, err)
time.Sleep(6 * time.Second)
val, err := testStorage.Get("test")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 0, len(val))
}
func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
testStorage := New(Config{
Endpoints: []string{"localhost:2379"},
})
err := testStorage.Set("test", []byte("fiber_test_value"), 0)
utils.AssertEqual(t, nil, err)
err = testStorage.Delete("test")
utils.AssertEqual(t, nil, err)
_, err = testStorage.Get("test")
utils.AssertEqual(t, nil, err)
}
func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
testStorage := New(Config{
Endpoints: []string{"localhost:2379"},
})
err := testStorage.Set("test", []byte("fiber_test_value"), 0)
utils.AssertEqual(t, nil, err)
err = testStorage.Reset()
utils.AssertEqual(t, nil, err)
_, err = testStorage.Get("test")
utils.AssertEqual(t, nil, err)
}
func TestClose_CloseShouldReturn_NoError(t *testing.T) {
testStorage := New(Config{
Endpoints: []string{"localhost:2379"},
})
err := testStorage.Close()
utils.AssertEqual(t, nil, err)
}
func TestGetConn_ReturnsNotNill(t *testing.T) {
testStorage := New(Config{
Endpoints: []string{"localhost:2379"},
})
conn := testStorage.Conn()
utils.AssertEqual(t, true, conn != nil)
}