Added entity formatting functionality. Renamed the EventSubgraph to Subgraph because it is generic.
176 lines
5.1 KiB
Go
176 lines
5.1 KiB
Go
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"}
|
|
expectedKey := "Event:AddressableEvent,Event"
|
|
|
|
// Test Serialization
|
|
// labels are expected to be pre-sorted
|
|
batchKey := createNodeBatchKey(matchLabel, sortedLabels)
|
|
assert.Equal(t, expectedKey, batchKey)
|
|
|
|
// Test Deserialization
|
|
returnedMatchLabel, returnedLabels, err := deserializeNodeBatchKey(batchKey)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, matchLabel, returnedMatchLabel)
|
|
assert.ElementsMatch(t, sortedLabels, returnedLabels)
|
|
}
|
|
|
|
func TestRelBatchKey(t *testing.T) {
|
|
rtype, startLabel, endLabel := "SIGNED", "User", "Event"
|
|
expectedKey := "SIGNED,User,Event"
|
|
|
|
// Test Serialization
|
|
batchKey := createRelBatchKey(rtype, startLabel, endLabel)
|
|
assert.Equal(t, expectedKey, batchKey)
|
|
|
|
// Test Deserialization
|
|
returnedRtype, returnedStartLabel, returnedEndLabel, err := deserializeRelBatchKey(batchKey)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, rtype, returnedRtype)
|
|
assert.Equal(t, startLabel, returnedStartLabel)
|
|
assert.Equal(t, endLabel, returnedEndLabel)
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// Batch Subgraph
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func TestBatchSubgraphAddNode(t *testing.T) {
|
|
matchKeys := NewSimpleMatchKeys()
|
|
subgraph := NewBatchSubgraph(matchKeys)
|
|
node := NewEventNode("abc123")
|
|
|
|
err := subgraph.AddNode(node)
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, subgraph.NodeCount())
|
|
assert.Equal(t, []*Node{node}, subgraph.nodes["Event:Event"])
|
|
}
|
|
|
|
func TestBatchSubgraphAddNodeInvalid(t *testing.T) {
|
|
matchKeys := NewSimpleMatchKeys()
|
|
subgraph := NewBatchSubgraph(matchKeys)
|
|
node := NewNode([]string{"Event"}, Properties{})
|
|
|
|
err := subgraph.AddNode(node)
|
|
|
|
assert.ErrorContains(t, err, "invalid node: missing property id")
|
|
assert.Equal(t, 0, subgraph.NodeCount())
|
|
}
|
|
|
|
func TestBatchSubgraphAddRel(t *testing.T) {
|
|
matchKeys := NewSimpleMatchKeys()
|
|
subgraph := NewBatchSubgraph(matchKeys)
|
|
|
|
userNode := NewUserNode("pubkey1")
|
|
eventNode := NewEventNode("abc123")
|
|
rel := NewSignedRel(userNode, eventNode, nil)
|
|
|
|
err := subgraph.AddRel(rel)
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, subgraph.RelCount())
|
|
assert.Equal(t, []*Relationship{rel}, subgraph.rels["SIGNED,User,Event"])
|
|
}
|
|
|
|
func TestNodeBatches(t *testing.T) {
|
|
matchKeys := NewSimpleMatchKeys()
|
|
subgraph := NewBatchSubgraph(matchKeys)
|
|
node := NewEventNode("abc123")
|
|
subgraph.AddNode(node)
|
|
|
|
batches, err := subgraph.NodeBatches()
|
|
|
|
assert.NoError(t, err)
|
|
assert.Len(t, batches, 1)
|
|
assert.Equal(t, "Event", batches[0].MatchLabel)
|
|
assert.ElementsMatch(t, []string{"Event"}, batches[0].Labels)
|
|
assert.ElementsMatch(t, []string{"id"}, batches[0].MatchKeys)
|
|
assert.Equal(t, []*Node{node}, batches[0].Nodes)
|
|
}
|
|
|
|
func TestRelBatches(t *testing.T) {
|
|
matchKeys := NewSimpleMatchKeys()
|
|
subgraph := NewBatchSubgraph(matchKeys)
|
|
userNode := NewUserNode("pubkey1")
|
|
eventNode := NewEventNode("abc123")
|
|
rel := NewSignedRel(userNode, eventNode, nil)
|
|
subgraph.AddRel(rel)
|
|
|
|
batches, err := subgraph.RelBatches()
|
|
|
|
assert.NoError(t, err)
|
|
assert.Len(t, batches, 1)
|
|
assert.Equal(t, "SIGNED", batches[0].Type)
|
|
assert.Equal(t, "User", batches[0].StartLabel)
|
|
assert.ElementsMatch(t, []string{"pubkey"}, batches[0].StartMatchKeys)
|
|
assert.Equal(t, "Event", batches[0].EndLabel)
|
|
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)
|
|
}
|