test: add more test cases

This commit is contained in:
Reinaldy Rafli 2021-11-16 12:13:49 +07:00
parent 07a1b112fe
commit e6f33ca1d4
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
3 changed files with 47 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package cheapcash_test package cheapcash_test
import ( import (
"errors"
"math/rand" "math/rand"
"strconv" "strconv"
"testing" "testing"
@ -31,3 +32,17 @@ func TestAppend(t *testing.T) {
t.Errorf("expected %s, got %v", "HelloWorld", string(r)) 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)
}
}

View File

@ -1,6 +1,7 @@
package cheapcash_test package cheapcash_test
import ( import (
"errors"
"math/rand" "math/rand"
"strconv" "strconv"
"sync" "sync"
@ -57,3 +58,17 @@ func TestRead_Concurrency(t *testing.T) {
wg.Wait() 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)
}
}

View File

@ -1,6 +1,8 @@
package cheapcash_test package cheapcash_test
import ( import (
"math/rand"
"strconv"
"sync" "sync"
"testing" "testing"
@ -37,3 +39,18 @@ func TestWrite_Concurrency(t *testing.T) {
wg.Wait() 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)
}
}