go-vise

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

commit 0102efa7edb8c671b05e73b328c28ed3e947be63
parent 1f2af8672dc7c39995915056faa0f9e9b588fdde
Author: lash <dev@holbrook.no>
Date:   Wed, 11 Sep 2024 21:23:02 +0100

Add tests for ensure state/cache in engine, and firstfunc

Diffstat:
MMakefile | 3++-
Mengine/db.go | 4----
Mengine/db_test.go | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 103 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -31,5 +31,6 @@ doc: make -C doc cov: - go test -cover ./... -coverprofile=cover.out + #go test -cover ./... -coverprofile=cover.out + go test -cover ./engine/... -coverprofile=cover.out go tool cover -html=cover.out diff --git a/engine/db.go b/engine/db.go @@ -176,8 +176,6 @@ func(en *DefaultEngine) preparePersist() error { } else { if en.st == nil { logg.Debugf("defer persist state set until state set in engine") - } else { - en.st = st } } @@ -195,8 +193,6 @@ func(en *DefaultEngine) preparePersist() error { } else { if en.ca == nil { logg.Debugf("defer persist memory set until memory set in engine") - } else { - en.ca = cac } } return nil diff --git a/engine/db_test.go b/engine/db_test.go @@ -286,3 +286,104 @@ func TestDbConfigString(t *testing.T) { t.Fatalf("expected contains 'tinky', got: '%s'", s) } } + +func TestDbEnsure(t *testing.T) { + var err error + var cfg Config + ctx := context.Background() + rs := resource.NewMenuResource() + store := memdb.NewMemDb() + store.Connect(ctx, "") + pe := persist.NewPersister(store) + en := NewEngine(cfg, rs).WithPersister(pe) + _, err = en.Init(ctx) + if err != nil { + t.Fatal(err) + } + if pe.GetState() == nil { + t.Fatal("expected persister state") + } + if pe.GetMemory() == nil { + t.Fatal("expected persister memory") + } +} + +func TestDbKeepPersisterContent(t *testing.T) { + var err error + var cfg Config + ctx := context.Background() + rs := resource.NewMenuResource() + st := state.NewState(0) + ca := cache.NewCache() + store := memdb.NewMemDb() + store.Connect(ctx, "") + pe := persist.NewPersister(store) + pe = pe.WithContent(st, ca) + en := NewEngine(cfg, rs).WithPersister(pe) + _, err = en.Init(ctx) + if err != nil { + t.Fatal(err) + } + pest := pe.GetState() + if pest != st { + t.Fatalf("expected persisted state %p same as engine %p", pest, st) + } + peca := pe.GetMemory() + if peca != ca { + t.Fatalf("expected persisted cache %p same as engine %p", peca, st) + } +} + +func TestDbKeepState(t *testing.T) { + var err error + var cfg Config + ctx := context.Background() + rs := resource.NewMenuResource() + st := state.NewState(0) + ca := cache.NewCache() + store := memdb.NewMemDb() + store.Connect(ctx, "") + pe := persist.NewPersister(store) + en := NewEngine(cfg, rs) + en = en.WithState(st) + en = en.WithMemory(ca) + en = en.WithPersister(pe) + _, err = en.Init(ctx) + if err != nil { + t.Fatal(err) + } + pest := pe.GetState() + if pest != st { + t.Fatalf("expected persisted state %p same as engine preset %p", pest, st) + } + peca := pe.GetMemory() + if peca != ca { + t.Fatalf("expected persisted cache %p same as engine preset %p", peca, st) + } +} + +func TestDbFirst(t *testing.T) { + var err error + var cfg Config + ctx := context.Background() + rs := resource.NewMenuResource() + st := state.NewState(1) + store := memdb.NewMemDb() + store.Connect(ctx, "") + + v := st.GetFlag(state.FLAG_USERSTART) + if v { + t.Fatal("expected flag unset") + } + en := NewEngine(cfg, rs) + en = en.WithState(st) + en = en.WithFirst(flagSet) + _, err = en.Init(ctx) + if err != nil { + t.Fatal(err) + } + v = st.GetFlag(state.FLAG_USERSTART) + if !v { + t.Fatal("expected flag set") + } +}