Wrote merge nodes test and neo4j testing infrastructure.

Added entity formatting functionality.
Renamed the EventSubgraph to Subgraph because it is generic.
This commit is contained in:
jay
2026-11-26 11:59:31 -05:00
parent 54f3f81add
commit ffe11f9c36
8 changed files with 600 additions and 39 deletions
+63 -1
View File
@@ -1,10 +1,15 @@
package heartwood
import (
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
"github.com/stretchr/testify/assert"
"testing"
)
// ----------------------------------------------------------------------------
// Batch Keys
// ----------------------------------------------------------------------------
func TestNodeBatchKey(t *testing.T) {
matchLabel := "Event"
sortedLabels := []string{"AddressableEvent", "Event"}
@@ -38,6 +43,10 @@ func TestRelBatchKey(t *testing.T) {
assert.Equal(t, endLabel, returnedEndLabel)
}
// ----------------------------------------------------------------------------
// Batch Subgraph
// ----------------------------------------------------------------------------
func TestBatchSubgraphAddNode(t *testing.T) {
matchKeys := NewSimpleMatchKeys()
subgraph := NewBatchSubgraph(matchKeys)
@@ -53,7 +62,7 @@ func TestBatchSubgraphAddNode(t *testing.T) {
func TestBatchSubgraphAddNodeInvalid(t *testing.T) {
matchKeys := NewSimpleMatchKeys()
subgraph := NewBatchSubgraph(matchKeys)
node := NewNode("Event", Properties{})
node := NewNode([]string{"Event"}, Properties{})
err := subgraph.AddNode(node)
@@ -111,3 +120,56 @@ func TestRelBatches(t *testing.T) {
assert.ElementsMatch(t, []string{"id"}, batches[0].EndMatchKeys)
assert.Equal(t, []*Relationship{rel}, batches[0].Rels)
}
// ----------------------------------------------------------------------------
// Merge Nodes
// ----------------------------------------------------------------------------
func TestMergeNodes(t *testing.T) {
s := freshStores(t)
ctx := t.Context()
session := s.graph.NewSession(ctx,
neo4j.SessionConfig{DatabaseName: "neo4j"},
)
defer session.Close(ctx)
batch := NodeBatch{
MatchLabel: "User",
Labels: []string{"User", "Admin"},
MatchKeys: []string{"id"},
Nodes: []*Node{
NewNode([]string{"User"}, Properties{"id": "1", "name": "Alice"}),
NewNode([]string{"User"}, Properties{"id": "2", "name": "Bob"}),
NewNode([]string{"User"}, Properties{"id": "3", "name": "Carol"}),
},
}
resultSummariesAny, err := session.ExecuteWrite(ctx,
func(tx neo4j.ManagedTransaction) (any, error) {
var resultSummaries []neo4j.ResultSummary
nodeResultSummary, err := MergeNodes(ctx, tx, batch)
if err != nil {
return nil, err
}
resultSummaries = append(resultSummaries, nodeResultSummary)
return resultSummaries, nil
},
)
assert.NoError(t, err)
resultSummaries, ok := resultSummariesAny.([]neo4j.ResultSummary)
assert.True(t, ok)
assert.Len(t, resultSummaries, 1)
summary := resultSummaries[0]
assert.Equal(t, 3, summary.Counters().NodesCreated())
expected := NewSubgraph()
for _, n := range batch.Nodes {
expected.AddNode(n)
}
assertSubgraph(t, s.graph, expected)
}