All checks were successful
release-tag / release-image (push) Successful in 2m30s
65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package persist
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/local/glpi-neural-brain/internal/graph"
|
|
"github.com/local/glpi-neural-brain/internal/model"
|
|
)
|
|
|
|
func TestCoordinatorBatchesFilesAndSQLiteGraph(t *testing.T) {
|
|
dir := t.TempDir()
|
|
g, err := graph.Open(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := New(g, nil, time.Minute)
|
|
g.UpsertNode(model.Node{ID: "n1", Kind: "knowledge", Label: "One", Origin: "test"})
|
|
path := filepath.Join(dir, "staging", "draft.json")
|
|
if _, err := c.QueueFile(path, []byte("first"), 0o640); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := c.QueueFile(path, []byte("second"), 0o640); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Fatalf("file should remain buffered before flush: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "graph.db")); err != nil {
|
|
t.Fatalf("SQLite database should be initialized at open: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "graph-state.json")); !os.IsNotExist(err) {
|
|
t.Fatalf("legacy JSON graph must not be written: %v", err)
|
|
}
|
|
if err := c.Flush(context.Background(), "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(data) != "second" {
|
|
t.Fatalf("unexpected file data %q", data)
|
|
}
|
|
status := c.Status()
|
|
if status.PendingFiles != 0 || status.GraphDirty || status.SuccessfulFlushes != 1 {
|
|
t.Fatalf("unexpected status: %+v", status)
|
|
}
|
|
|
|
if err := g.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reopened, err := graph.Open(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer reopened.Close()
|
|
if _, ok := reopened.GetNode("n1"); !ok {
|
|
t.Fatal("graph row was not persisted by coordinator")
|
|
}
|
|
}
|