go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit d3f8ea8a80c74f5b96fa01549ee9e45de3123d44
parent fb918cc45fdcda000d4a0e235c6bef9b777d83a0
Author: lash <dev@holbrook.no>
Date:   Wed, 11 Sep 2024 03:36:59 +0100

Test coverage for fsdb and gdbm over 80%

Diffstat:
Mdb/fs/fs_test.go | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdb/gdbm/gdbm_test.go | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 192 insertions(+), 0 deletions(-)

diff --git a/db/fs/fs_test.go b/db/fs/fs_test.go @@ -100,3 +100,91 @@ func TestPutGetFsAlt(t *testing.T) { t.Fatalf("expected %x, got %x", b, v) } } + +func TestConnect(t *testing.T) { + ctx := context.Background() + store := NewFsDb() + err := store.Connect(ctx, "") + if err == nil { + t.Fatal("expected error") + } + err = store.SetLock(db.DATATYPE_USERDATA, true) + if err != nil { + t.Fatal(err) + } + store.SetPrefix(db.DATATYPE_USERDATA) + if store.CheckPut() { + t.Fatal("expected checkput false") + } + err = store.Put(ctx, []byte("foo"), []byte("bar")) + if err == nil { + t.Fatal("expected error") + } + if store.CheckPut() { + t.Fatal("expected checkput false") + } + err = store.SetLock(db.DATATYPE_USERDATA, false) + if err != nil { + t.Fatal(err) + } + if !store.CheckPut() { + t.Fatal("expected checkput false") + } +} + +func TestReopen(t *testing.T) { + ctx := context.Background() + store := NewFsDb() + d, err := ioutil.TempDir("", "vise-db-fs-*") + if err != nil { + t.Fatal(err) + } + err = store.Connect(ctx, d) + if err != nil { + t.Fatal(err) + } + err = store.Connect(ctx, d) + if err != nil { + t.Fatal(err) + } + store.SetPrefix(db.DATATYPE_USERDATA) + err = store.Put(ctx, []byte("foo"), []byte("bar")) + if err != nil { + t.Fatal(err) + } + err = store.Close() + if err != nil { + t.Fatal(err) + } + + store = NewFsDb() + err = store.Connect(ctx, d) + if err != nil { + t.Fatal(err) + } + store.SetPrefix(db.DATATYPE_USERDATA) + v, err := store.Get(ctx, []byte("foo")) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(v, []byte("bar")) { + t.Fatalf("expected 'bar', got: '%s'", v) + } +} + +func TestNoKey(t *testing.T) { + ctx := context.Background() + store := NewFsDb() + d, err := ioutil.TempDir("", "vise-db-fs-*") + if err != nil { + t.Fatal(err) + } + err = store.Connect(ctx, d) + if err != nil { + t.Fatal(err) + } + _, err = store.Get(ctx, []byte("xyzzy")) + if err == nil { + t.Fatal(err) + } +} diff --git a/db/gdbm/gdbm_test.go b/db/gdbm/gdbm_test.go @@ -65,3 +65,107 @@ func TestPutGetGdbm(t *testing.T) { } } + +func TestConnect(t *testing.T) { + ctx := context.Background() + store := NewGdbmDb() + err := store.Connect(ctx, "") + if err == nil { + t.Fatal("expected error") + } + f, err := ioutil.TempFile("", "vise-db-gdbm-*") + if err != nil { + t.Fatal(err) + } + err = store.Connect(ctx, f.Name()) + if err != nil { + t.Fatal(err) + } + err = store.SetLock(db.DATATYPE_USERDATA, true) + if err != nil { + t.Fatal(err) + } + store.SetPrefix(db.DATATYPE_USERDATA) + if store.CheckPut() { + t.Fatal("expected checkput false") + } + err = store.Put(ctx, []byte("foo"), []byte("bar")) + if err == nil { + t.Fatal("expected error") + } + if store.CheckPut() { + t.Fatal("expected checkput false") + } + err = store.SetLock(db.DATATYPE_USERDATA, false) + if err != nil { + t.Fatal(err) + } + if !store.CheckPut() { + t.Fatal("expected checkput false") + } + err = store.Close() + if err != nil { + t.Fatal(err) + } + err = store.Put(ctx, []byte("foo"), []byte("bar")) + if err == nil { + t.Fatal("expected error") + } +} + +func TestReopen(t *testing.T) { + ctx := context.Background() + store := NewGdbmDb() + f, err := ioutil.TempFile("", "vise-db-gdbm-*") + if err != nil { + t.Fatal(err) + } + err = store.Connect(ctx, f.Name()) + if err != nil { + t.Fatal(err) + } + err = store.Connect(ctx, f.Name()) + if err != nil { + t.Fatal(err) + } + store.SetPrefix(db.DATATYPE_USERDATA) + err = store.Put(ctx, []byte("foo"), []byte("bar")) + if err != nil { + t.Fatal(err) + } + err = store.Close() + if err != nil { + t.Fatal(err) + } + + store = NewGdbmDb() + err = store.Connect(ctx, f.Name()) + if err != nil { + t.Fatal(err) + } + store.SetPrefix(db.DATATYPE_USERDATA) + v, err := store.Get(ctx, []byte("foo")) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(v, []byte("bar")) { + t.Fatalf("expected 'bar', got: '%s'", v) + } +} + +func TestNoKey(t *testing.T) { + ctx := context.Background() + store := NewGdbmDb() + f, err := ioutil.TempFile("", "vise-db-gdbm-*") + if err != nil { + t.Fatal(err) + } + err = store.Connect(ctx, f.Name()) + if err != nil { + t.Fatal(err) + } + _, err = store.Get(ctx, []byte("xyzzy")) + if err == nil { + t.Fatal(err) + } +}