From dc61d055844fffa71cab6c96893436f14360fa68 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 12:16:28 +0000 Subject: [PATCH] WIP revert connstr gdbm to dir only, add file as table spec --- internal/storage/storageservice.go | 8 +++++-- .../testutil/{TestEngine.go => engine.go} | 22 +++++++++++-------- internal/testutil/engine_test.go | 15 +++++++++++++ menutraversal_test/menu_traversal_test.go | 11 +++++++--- 4 files changed, 42 insertions(+), 14 deletions(-) rename internal/testutil/{TestEngine.go => engine.go} (82%) create mode 100644 internal/testutil/engine_test.go diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 6a42bac..e14b7a4 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "path" "git.defalsify.org/vise.git/db" fsdb "git.defalsify.org/vise.git/db/fs" @@ -66,7 +67,7 @@ func (ms *MenuStorageService) SetConn(connStr string) error { return nil } -func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, fileName string) (db.Db, error) { +func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { var newDb db.Db var err error // database, ok := ctx.Value("Database").(string) @@ -79,6 +80,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D } + connStr := ms.conn.String() dbTyp := ms.conn.DbType() if dbTyp == DBTYPE_POSTGRES { newDb = postgres.NewPgDb() @@ -87,11 +89,13 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D if err != nil { return nil, err } + connStr = path.Join(connStr, section) newDb = gdbmstorage.NewThreadGdbmDb() } else { return nil, fmt.Errorf("unsupported connection string: %s", ms.conn.String()) } - err = newDb.Connect(ctx, ms.conn.String()) + logg.DebugCtxf(ctx, "connecting to db", "conn", connStr) + err = newDb.Connect(ctx, connStr) if err != nil { return nil, err } diff --git a/internal/testutil/TestEngine.go b/internal/testutil/engine.go similarity index 82% rename from internal/testutil/TestEngine.go rename to internal/testutil/engine.go index 63e9e84..4b02e32 100644 --- a/internal/testutil/TestEngine.go +++ b/internal/testutil/engine.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "path/filepath" "time" "git.defalsify.org/vise.git/engine" @@ -27,7 +28,6 @@ var ( func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) - ctx = context.WithValue(ctx, "Database", "gdbm") pfp := path.Join(scriptDir, "pp.csv") var eventChannel = make(chan bool) @@ -39,39 +39,43 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { FlagCount: uint32(128), } - dbDir := ".test_state" + connStr, err := filepath.Abs(".test_state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) + } resourceDir := scriptDir - //menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + //menuStorageService := storage.NewMenuStorageService(connStr, resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir) //err := menuStorageService.EnsureDbDir() - err := menuStorageService.SetConn(dbDir) + err = menuStorageService.SetConn(connStr) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "conn error: %v", err) os.Exit(1) } rs, err := menuStorageService.GetResource(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "resource error: %v", err) os.Exit(1) } pe, err := menuStorageService.GetPersister(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "persister error: %v", err) os.Exit(1) } userDataStore, err := menuStorageService.GetUserdataDb(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "userdb error: %v", err) os.Exit(1) } dbResource, ok := rs.(*resource.DbResource) if !ok { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "dbresource cast error") os.Exit(1) } diff --git a/internal/testutil/engine_test.go b/internal/testutil/engine_test.go new file mode 100644 index 0000000..f747468 --- /dev/null +++ b/internal/testutil/engine_test.go @@ -0,0 +1,15 @@ +package testutil + +import ( + "testing" +) + +func TestCreateEngine(t *testing.T) { + o, clean, eventC := TestEngine("foo") + defer clean() + defer func() { + <-eventC + close(eventC) + }() + _ = o +} diff --git a/menutraversal_test/menu_traversal_test.go b/menutraversal_test/menu_traversal_test.go index 6b6b3da..52e2273 100644 --- a/menutraversal_test/menu_traversal_test.go +++ b/menutraversal_test/menu_traversal_test.go @@ -7,6 +7,7 @@ import ( "log" "math/rand" "os" + "path/filepath" "regexp" "testing" @@ -17,7 +18,6 @@ import ( var ( testData = driver.ReadData() - testStore = ".test_state" sessionID string src = rand.NewSource(42) g = rand.New(src) @@ -25,6 +25,11 @@ var ( var groupTestFile = flag.String("test-file", "group_test.json", "The test file to use for running the group tests") +func testStore() string { + v, _ := filepath.Abs(".test_state/state.gdbm") + return v +} + func GenerateSessionId() string { uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g)) v, err := uu.NewV4() @@ -81,8 +86,8 @@ func extractSendAmount(response []byte) string { func TestMain(m *testing.M) { sessionID = GenerateSessionId() defer func() { - if err := os.RemoveAll(testStore); err != nil { - log.Fatalf("Failed to delete state store %s: %v", testStore, err) + if err := os.RemoveAll(testStore()); err != nil { + log.Fatalf("Failed to delete state store %s: %v", testStore(), err) } }() m.Run()