From e42a4816ac8391cadee928b79eb3fb096b8ae026 Mon Sep 17 00:00:00 2001 From: Erkan Ozsoy Date: Mon, 3 Jul 2023 16:31:04 +0300 Subject: [PATCH] more unittests implemented --- etcd/etcd.go | 8 +++- etcd/etcd_test.go | 98 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 100 insertions(+), 6 deletions(-) diff --git a/etcd/etcd.go b/etcd/etcd.go index 4b3648a0..e6a02a6a 100644 --- a/etcd/etcd.go +++ b/etcd/etcd.go @@ -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 } diff --git a/etcd/etcd_test.go b/etcd/etcd_test.go index 21cc8c17..f73e78d6 100644 --- a/etcd/etcd_test.go +++ b/etcd/etcd_test.go @@ -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) +}