2021-11-16 04:56:33 +00:00
|
|
|
package cheapcash_test
|
|
|
|
|
|
|
|
import (
|
2021-11-16 05:13:49 +00:00
|
|
|
"errors"
|
2021-11-16 04:56:33 +00:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aldy505/cheapcash"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAppend(t *testing.T) {
|
2021-11-16 13:18:23 +00:00
|
|
|
randomValue := strconv.Itoa(rand.Int())
|
2021-11-16 04:56:33 +00:00
|
|
|
c := cheapcash.Default()
|
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
err := c.Write(randomValue, []byte("Hello"))
|
2021-11-16 04:56:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
err = c.Append(randomValue, []byte("World"))
|
2021-11-16 04:56:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
r, err := c.Read(randomValue)
|
2021-11-16 04:56:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(r) != "HelloWorld" {
|
|
|
|
t.Errorf("expected %s, got %v", "HelloWorld", string(r))
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 05:13:49 +00:00
|
|
|
|
|
|
|
func TestAppend_NotExists(t *testing.T) {
|
2021-11-16 13:18:23 +00:00
|
|
|
randomValue := strconv.Itoa(rand.Int())
|
2021-11-16 05:13:49 +00:00
|
|
|
c := cheapcash.Default()
|
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
err := c.Append(randomValue, []byte("Hello"))
|
2021-11-16 05:13:49 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|