commit 2108b77e5392058c82acf98871aaf951a6bdc65e
parent 11aac315f0629a2a4a8d74d16cc49bb54a577778
Author: lash <dev@holbrook.no>
Date: Sun, 1 Sep 2024 17:00:20 +0100
WIP db test vectors
Diffstat:
M | db/db.go | | | 1 | + |
A | db/db_test.go | | | 109 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | db/mem_test.go | | | 42 | ++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 152 insertions(+), 0 deletions(-)
diff --git a/db/db.go b/db/db.go
@@ -45,6 +45,7 @@ type Db interface {
// * DATATYPE_STATE
// * DATATYPE_USERSTART
SetSession(sessionId string)
+ SetLock(typ uint8, locked bool)
}
type lookupKey struct {
diff --git a/db/db_test.go b/db/db_test.go
@@ -0,0 +1,109 @@
+package db
+
+import (
+ "context"
+ "encoding/hex"
+ "path"
+)
+
+type testCase struct {
+ typ uint8
+ s string
+ k []byte
+ v []byte
+}
+
+type testVector struct {
+ c map[string]*testCase
+ v []string
+ i int
+}
+
+func(tc *testCase) Key() []byte {
+ return tc.k
+}
+
+func(tc *testCase) Val() []byte {
+ return tc.v
+}
+
+func(tc *testCase) Typ() uint8 {
+ return tc.typ
+}
+
+func(tc *testCase) Session() string {
+ return tc.s
+}
+
+func(tv *testVector) add(typ uint8, k string, v string, session string) {
+ var b []byte
+ var err error
+
+ if typ == DATATYPE_BIN {
+ b, err = hex.DecodeString(v)
+ if err != nil {
+ panic(err)
+ }
+ } else {
+ b = []byte(v)
+ }
+
+ o := &testCase {
+ typ: typ,
+ k: []byte(k),
+ v: b,
+ s: session,
+ }
+ s := path.Join(session, k)
+ tv.c[s] = o
+ tv.v = append(tv.v, s)
+}
+
+func(tv *testVector) next() (int, *testCase) {
+ i := tv.i
+ if i == len(tv.v) {
+ return -1, nil
+ }
+ tv.i++
+ return i, tv.c[tv.v[i]]
+}
+
+func(tv *testVector) rewind() {
+ tv.i = 0
+}
+
+func(tv *testVector) put(ctx context.Context, db Db) error {
+ var i int
+ var tc *testCase
+ defer tv.rewind()
+
+ for true {
+ i, tc = tv.next()
+ if i == -1 {
+ break
+ }
+ db.SetPrefix(tc.Typ())
+ db.SetSession(tc.Session())
+ db.SetLock(tc.Typ(), false)
+ err := db.Put(ctx, tc.Key(), tc.Val())
+ if err != nil {
+ return err
+ }
+ db.SetLock(tc.Typ(), true)
+ }
+ return nil
+}
+
+func generateTestVectors() testVector {
+ tv := testVector{c: make(map[string]*testCase)}
+ tv.add(DATATYPE_BIN, "foo", "deadbeef", "")
+ tv.add(DATATYPE_BIN, "foo", "beeffeed", "tinkywinky")
+ tv.add(DATATYPE_TEMPLATE, "foo", "inky", "")
+ tv.add(DATATYPE_TEMPLATE, "foo", "pinky", "dipsy")
+ tv.add(DATATYPE_MENU, "foo", "blinky", "")
+ tv.add(DATATYPE_MENU, "foo", "clyde", "lala")
+ tv.add(DATATYPE_STATICLOAD, "foo", "bar", "")
+ tv.add(DATATYPE_STATICLOAD, "foo", "baz", "po")
+ return tv
+}
+
diff --git a/db/mem_test.go b/db/mem_test.go
@@ -3,9 +3,51 @@ package db
import (
"bytes"
"context"
+ "fmt"
"testing"
)
+func TestCasesMem(t *testing.T) {
+ var i int
+ var tc *testCase
+
+ ctx := context.Background()
+ vs := generateTestVectors()
+ db := NewMemDb()
+ err := db.Connect(ctx, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = vs.put(ctx, db)
+
+ for true {
+ i, tc = vs.next()
+ if i == -1 {
+ break
+ }
+ s := fmt.Sprintf("TestTyp%dKey%s", tc.Typ(), tc.Key())
+ if tc.Session() != "" {
+ s += "Session" + tc.Session()
+ } else {
+ s += "NoSession"
+ }
+ r := t.Run(s, func(t *testing.T) {
+ db.SetPrefix(tc.Typ())
+ db.SetSession(tc.Session())
+ v, err := db.Get(ctx, tc.Key())
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(tc.Val(), v) {
+ t.Fatalf("expected %x, got %x", tc.Val(), v)
+ }
+ })
+ if !r {
+ t.Fatalf("subtest fail")
+ }
+ }
+}
+
func TestPutGetMem(t *testing.T) {
var dbi Db
ctx := context.Background()