go-vise

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

db_test.go (2986B)


      1 package resource
      2 
      3 import (
      4 	"bytes"
      5 	"context"
      6 	"testing"
      7 
      8 	"git.defalsify.org/vise.git/db"
      9 	"git.defalsify.org/vise.git/db/mem"
     10 )
     11 
     12 func TestDb(t *testing.T) {
     13 	var rsifc Resource
     14 	ctx := context.Background()
     15 	store := mem.NewMemDb()
     16 	store.Connect(ctx, "")
     17 	tg := NewDbResource(store)
     18 	tg.Without(db.DATATYPE_BIN)
     19 	tg.Without(db.DATATYPE_MENU)
     20 	tg.Without(db.DATATYPE_TEMPLATE)
     21 	// check that it fulfills interface
     22 	rsifc = tg
     23 	_ = rsifc
     24 	rs := NewMenuResource()
     25 	rs.WithTemplateGetter(tg.GetTemplate)
     26 
     27 	s, err := rs.GetTemplate(ctx, "foo")
     28 	if err == nil {
     29 		t.Fatal("expected error")
     30 	}
     31 
     32 	store.SetPrefix(db.DATATYPE_TEMPLATE)
     33 	err = store.Put(ctx, []byte("foo"), []byte("bar"))
     34 	if err == nil {
     35 		t.Fatal("expected error")
     36 	}
     37 	store.SetLock(db.DATATYPE_TEMPLATE, false)
     38 	err = store.Put(ctx, []byte("foo"), []byte("bar"))
     39 	if err != nil {
     40 		t.Fatal(err)
     41 	}
     42 	store.SetLock(db.DATATYPE_TEMPLATE, true)
     43 	tg.With(db.DATATYPE_TEMPLATE)
     44 	s, err = rs.GetTemplate(ctx, "foo")
     45 	if err != nil {
     46 		t.Fatal(err)
     47 	}
     48 	if s != "bar" {
     49 		t.Fatalf("expected 'bar', got %s", s)
     50 	}
     51 
     52 	// test support check
     53 	store.SetPrefix(db.DATATYPE_BIN)
     54 	store.SetLock(db.DATATYPE_BIN, false)
     55 	err = store.Put(ctx, []byte("xyzzy"), []byte("deadbeef"))
     56 	if err != nil {
     57 		t.Fatal(err)
     58 	}
     59 	store.SetLock(db.DATATYPE_BIN, true)
     60 
     61 	rs.WithCodeGetter(tg.GetCode)
     62 	b, err := rs.GetCode(ctx, "xyzzy")
     63 	if err == nil {
     64 		t.Fatalf("expected error")
     65 	}
     66 	tg.With(db.DATATYPE_BIN)
     67 	b, err = rs.GetCode(ctx, "xyzzy")
     68 	if err != nil {
     69 		t.Fatal(err)
     70 	}
     71 
     72 	tg = NewDbResource(store)
     73 	rs.WithTemplateGetter(tg.GetTemplate)
     74 
     75 	rs.WithCodeGetter(tg.GetCode)
     76 	b, err = rs.GetCode(ctx, "xyzzy")
     77 	if err != nil {
     78 		t.Fatal(err)
     79 	}
     80 	if !bytes.Equal(b, []byte("deadbeef")) {
     81 		t.Fatalf("expected 'deadbeef', got %x", b)
     82 	}
     83 
     84 	tg = NewDbResource(store)
     85 	store.SetPrefix(db.DATATYPE_MENU)
     86 	store.SetLock(db.DATATYPE_MENU, false)
     87 	err = store.Put(ctx, []byte("inky"), []byte("pinky"))
     88 	if err != nil {
     89 		t.Fatal(err)
     90 	}
     91 	store.SetLock(db.DATATYPE_MENU, true)
     92 	rs.WithMenuGetter(tg.GetMenu)
     93 
     94 }
     95 
     96 func TestDbGetterDirect(t *testing.T) {
     97 	ctx := context.Background()
     98 	store := mem.NewMemDb()
     99 	store.Connect(ctx, "")
    100 	tg := NewDbResource(store)
    101 
    102 	store.SetLock(db.DATATYPE_MENU, false)
    103 	store.SetPrefix(db.DATATYPE_MENU)
    104 	err := store.Put(ctx, []byte("foo"), []byte("bar"))
    105 	if err != nil {
    106 		t.Fatal(err)
    107 	}
    108 	store.SetLock(db.DATATYPE_MENU, true)
    109 	v, err := tg.GetMenu(ctx, "foo")
    110 	if err != nil {
    111 		t.Fatal(err)
    112 	}
    113 	if v != "foo" {
    114 		t.Fatalf("expected 'foo', got '%s'", v)
    115 	}
    116 
    117 	tg.With(db.DATATYPE_STATICLOAD)
    118 	store.SetLock(db.DATATYPE_STATICLOAD, false)
    119 	store.SetPrefix(db.DATATYPE_STATICLOAD)
    120 	err = store.Put(ctx, []byte("inky.txt"), []byte("blinky"))
    121 	if err != nil {
    122 		t.Fatal(err)
    123 	}
    124 	store.SetLock(db.DATATYPE_STATICLOAD, true)
    125 	fn, err := tg.DbFuncFor(ctx, "inky")
    126 	if err != nil {
    127 		t.Fatal(err)
    128 	}
    129 	r, err := fn(ctx, "", nil)
    130 if err != nil {
    131 		t.Fatal(err)
    132 	}
    133 	if r.Content != "blinky" {
    134 		t.Fatalf("expected 'foo', got '%s'", v)
    135 	}
    136 }