From e6f33ca1d43fd2fd78709c6ffe015114432322b2 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Tue, 16 Nov 2021 12:13:49 +0700 Subject: [PATCH] test: add more test cases --- append_test.go | 15 +++++++++++++++ reader_test.go | 15 +++++++++++++++ writer_test.go | 17 +++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/append_test.go b/append_test.go index 10eb215..ac1c9d5 100644 --- a/append_test.go +++ b/append_test.go @@ -1,6 +1,7 @@ package cheapcash_test import ( + "errors" "math/rand" "strconv" "testing" @@ -31,3 +32,17 @@ func TestAppend(t *testing.T) { t.Errorf("expected %s, got %v", "HelloWorld", string(r)) } } + +func TestAppend_NotExists(t *testing.T) { + rand := strconv.Itoa(rand.Int()) + c := cheapcash.Default() + + err := c.Append(rand, []byte("Hello")) + if err == nil { + t.Error("expected an error, got nil") + } + + if !errors.Is(err, cheapcash.ErrNotExists) { + t.Errorf("expected %v, got %v", cheapcash.ErrNotExists, err) + } +} diff --git a/reader_test.go b/reader_test.go index a2690b9..86d6527 100644 --- a/reader_test.go +++ b/reader_test.go @@ -1,6 +1,7 @@ package cheapcash_test import ( + "errors" "math/rand" "strconv" "sync" @@ -57,3 +58,17 @@ func TestRead_Concurrency(t *testing.T) { wg.Wait() } + +func TestRead_NotExists(t *testing.T) { + rand := strconv.Itoa(rand.Int()) + c := cheapcash.Default() + + _, err := c.Read(rand) + if err == nil { + t.Error("expected an error, got nil") + } + + if !errors.Is(err, cheapcash.ErrNotExists) { + t.Errorf("expected %v, got %v", cheapcash.ErrNotExists, err) + } +} diff --git a/writer_test.go b/writer_test.go index 1ba6df5..97fc6c3 100644 --- a/writer_test.go +++ b/writer_test.go @@ -1,6 +1,8 @@ package cheapcash_test import ( + "math/rand" + "strconv" "sync" "testing" @@ -37,3 +39,18 @@ func TestWrite_Concurrency(t *testing.T) { wg.Wait() } + +func TestWrite_Exists(t *testing.T) { + rand := strconv.Itoa(rand.Int()) + c := cheapcash.Default() + + err := c.Write(rand, []byte("value")) + if err != nil { + t.Error("an error was thrown:", err) + } + + err = c.Write(rand, []byte("another value")) + if err != nil { + t.Error("an error was thrown:", err) + } +}