61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
func TestDriverBBolt(t *testing.T) {
|
|
testDriver(t, NewTestDBIn(t.TempDir()))
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|