From 8883856a633441ec02e7239b701c2f6109f44dc5 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Fri, 12 Apr 2024 09:06:57 -0600 Subject: [PATCH] test driver --- driver_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 driver_test.go diff --git a/driver_test.go b/driver_test.go new file mode 100644 index 0000000..6aeecd5 --- /dev/null +++ b/driver_test.go @@ -0,0 +1,67 @@ +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") + } +}