spoc-bot-vr/driver_test.go

68 lines
1.4 KiB
Go

package main
import (
"context"
"errors"
"io"
"path"
"testing"
)
func TestDriverBBolt(t *testing.T) {
bb, err := NewDB(path.Join(t.TempDir(), "bb"))
if err != nil {
t.Fatal(err)
}
defer bb.Close()
testDriver(t, bb)
}
func testDriver(t *testing.T, d Driver) {
defer d.Close()
if b, err := d.Get(nil, "db", "id"); err != nil {
t.Error("cannot get from empty", err)
} else if b != nil {
t.Error("got fake from empty")
}
if err := d.ForEach(context.Background(), "db", func(string, []byte) error {
return errors.New("should have no hits")
}); err != nil {
t.Error("failed to forEach empty", err)
}
if err := d.Set(nil, "db", "id", []byte("hello world")); err != nil {
t.Error("cannot set from empty", err)
}
if b, err := d.Get(nil, "db", "id"); err != nil {
t.Error("cannot get from full", err)
} else if string(b) != "hello world" {
t.Error("got fake from full")
}
if err := d.ForEach(context.Background(), "db", func(id string, v []byte) error {
if id != "id" {
t.Error(id)
}
if string(v) != "hello world" {
t.Error(string(v))
}
return io.EOF
}); err != io.EOF {
t.Error("failed to forEach full", err)
}
if err := d.Set(nil, "db", "id", nil); err != nil {
t.Error("cannot set from full", err)
}
if b, err := d.Get(nil, "db", "id"); err != nil {
t.Error("cannot get from deleted", err)
} else if b != nil {
t.Error("got fake from deleted")
}
}