diff --git a/batchMerge_test.go b/batchMerge_test.go index 165dac7..473ed38 100644 --- a/batchMerge_test.go +++ b/batchMerge_test.go @@ -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) +} diff --git a/graph.go b/graph.go index f91f579..854710e 100644 --- a/graph.go +++ b/graph.go @@ -2,6 +2,10 @@ package heartwood import ( "fmt" + "github.com/neo4j/neo4j-go-driver/v6/neo4j" + "maps" + "slices" + "strings" ) // ---------------------------------------------------------------------------- @@ -11,6 +15,36 @@ import ( // Properties represents a map of node or relationship props. type Properties map[string]any +func (p Properties) String() string { + var b strings.Builder + b.WriteString("{") + for i, k := range slices.Sorted(maps.Keys(p)) { + if i > 0 { + b.WriteString(", ") + } + b.WriteString(fmt.Sprintf("%s: %s", k, formatPropValue(p[k]))) + } + b.WriteString("}") + return b.String() +} + +func formatPropValue(v any) string { + switch v := v.(type) { + case nil: + return "nil" + case string: + return fmt.Sprintf("%q", v) + case bool: + return fmt.Sprintf("%t", v) + case int, int64: + return fmt.Sprintf("%d", v) + case float64: + return fmt.Sprintf("%g", v) + default: + return fmt.Sprintf("%v", v) + } +} + // ---------------------------------------------------------------------------- // Match Key Provider // ---------------------------------------------------------------------------- @@ -53,6 +87,8 @@ func (p *SimpleMatchKeys) GetKeys(label string) ([]string, bool) { // Node represents a Neo4j node entity, encapsulating its labels and // properties. type Node struct { + // Element ID of the node. + ElementID string // Set of labels on the node. Labels *StringSet // Mapping of properties on the node. @@ -60,16 +96,24 @@ type Node struct { } // NewNode creates a new node with the given label and properties. -func NewNode(label string, props Properties) *Node { +func NewNode(labels []string, props Properties) *Node { if props == nil { props = make(Properties) } return &Node{ - Labels: NewStringSet(label), + Labels: NewStringSet(labels...), Props: props, } } +// NewNodeFromNeo4j creates a new node from a Neo4j Node. +func NewNodeFromNeo4j(n neo4j.Node) *Node { + return &Node{ + ElementID: n.ElementId, + Labels: NewStringSet(n.Labels...), + Props: n.Props} +} + // MatchProps returns the node label and the property values to match it in the // database. func (n *Node) MatchProps( @@ -105,6 +149,14 @@ func (n *Node) MatchProps( return "", nil, fmt.Errorf("no recognized label found in %v", n.Labels) } +func (n *Node) String() string { + if n.ElementID != "" && len(n.Labels.AsSortedArray()) == 0 { + return fmt.Sprintf("Node{element_id=%q}", n.ElementID) + } + return fmt.Sprintf("Node{element_id=%q labels=%v properties=%v}", + n.ElementID, n.Labels.AsSortedArray(), n.Props.String()) +} + type SerializedNode = Properties func (n *Node) Serialize() *SerializedNode { @@ -118,6 +170,8 @@ func (n *Node) Serialize() *SerializedNode { // Relationship represents a Neo4j relationship between two nodes, including // its type and properties. type Relationship struct { + // The element ID of the relationship. + ElementID string // The relationship type. Type string // The start node for the relationship. @@ -144,6 +198,23 @@ func NewRelationship( } } +// NewRelationshipFromNeo4j creates a new relationship from a Neo4j Relationship. +func NewRelationshipFromNeo4j(r neo4j.Relationship) *Relationship { + return &Relationship{ + ElementID: r.ElementId, + Type: r.Type, + Start: &Node{ElementID: r.StartElementId}, + End: &Node{ElementID: r.EndElementId}, + Props: r.Props, + } +} + +func (r *Relationship) String() string { + return fmt.Sprintf( + "Relationship{element_id=%q type=%q start=%v end=%v properties=%v}", + r.ElementID, r.Type, r.Start, r.End, r.Props.String()) +} + type SerializedRel = map[string]Properties func (r *Relationship) Serialize() *SerializedRel { diff --git a/graph_test.go b/graph_test.go index 1c68e7a..a738137 100644 --- a/graph_test.go +++ b/graph_test.go @@ -5,6 +5,10 @@ import ( "testing" ) +// ---------------------------------------------------------------------------- +// Match Keys +// ---------------------------------------------------------------------------- + func TestSimpleMatchKeys(t *testing.T) { matchKeys := &SimpleMatchKeys{ Keys: map[string][]string{ @@ -58,13 +62,13 @@ func TestMatchProps(t *testing.T) { }, { name: "matching label, required prop missing", - node: NewNode("Event", Properties{}), + node: NewNode([]string{"Event"}, Properties{}), wantErr: true, wantErrText: "missing property", }, { name: "no recognized label", - node: NewNode("Tag", Properties{"name": "e", "value": "abc"}), + node: NewNode([]string{"Tag"}, Properties{"name": "e", "value": "abc"}), wantErr: true, wantErrText: "no recognized label", }, diff --git a/neo4j_test.go b/neo4j_test.go new file mode 100644 index 0000000..ec2166c --- /dev/null +++ b/neo4j_test.go @@ -0,0 +1,424 @@ +package heartwood + +import ( + "context" + "errors" + "fmt" + "github.com/neo4j/neo4j-go-driver/v6/neo4j" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "maps" + "slices" + "strings" + "testing" +) + +// ---------------------------------------------------------------------------- +// Assertions +// ---------------------------------------------------------------------------- + +func assertSubgraph(t *testing.T, d neo4j.Driver, s *Subgraph) { + t.Helper() + ctx := t.Context() + + // get all graph entities + result, err := execute(ctx, d, "MATCH (n) RETURN n", nil) + require.NoError(t, err) + + allNodes := map[string]*Node{} + for _, r := range result.Records { + rn, _ := r.Get("n") + nn, ok := rn.(neo4j.Node) + require.True(t, ok) + allNodes[nn.ElementId] = NewNodeFromNeo4j(nn) + } + + result, err = execute(ctx, d, "MATCH ()-[r]->() RETURN r", nil) + require.NoError(t, err) + + allRels := map[string]*Relationship{} + for _, r := range result.Records { + rr, _ := r.Get("r") + nr, ok := rr.(neo4j.Relationship) + require.True(t, ok) + allRels[nr.ElementId] = NewRelationshipFromNeo4j(nr) + } + + // match nodes + for _, n := range s.nodes { + nodes, err := matchNodes(ctx, d, n.Labels.AsSortedArray(), n.Props) + require.NoError(t, err) + assert.NotEmpty(t, nodes, + fmt.Sprintf("Expected node: %s", n)) + if len(nodes) > 0 { + delete(allNodes, nodes[0].ElementID) + } + } + + // match relationships + for _, r := range s.rels { + // get start node + nodes, err := matchNodes(ctx, d, r.Start.Labels.AsSortedArray(), r.Start.Props) + require.NoError(t, err) + assert.NotEmpty(t, nodes, + fmt.Sprintf("Expected start node: %s", r.Start)) + var startID string + if len(nodes) > 0 { + startID = nodes[0].ElementID + } + + // get end node + nodes, err = matchNodes(ctx, d, r.End.Labels.AsSortedArray(), r.End.Props) + require.NoError(t, err) + assert.NotEmpty(t, nodes, + fmt.Sprintf("Expected end node: %s", r.End)) + var endID string + if len(nodes) > 0 { + endID = nodes[0].ElementID + } + + // match relationship + rels, err := matchRels(ctx, d, r.Type, startID, endID, r.Props) + require.NoError(t, err) + assert.NotEmpty(t, rels, + fmt.Sprintf("Expected relationship: %s", r)) + if len(rels) > 0 { + delete(allRels, rels[0].ElementID) + } + } + + // report extras + assert.Equal(t, 0, len(allNodes), + fmt.Sprintf("Graph contains unexpected nodes:\n\n%s", formatMap(allNodes))) + assert.Equal(t, 0, len(allRels), + fmt.Sprintf("Graph contains unexpected relationships:\n\n%s", formatMap(allRels))) +} + +func assertNoNodes(t *testing.T, d neo4j.Driver, labels []string, props Properties) { + t.Helper() + ctx := t.Context() + + nodes, err := matchNodes(ctx, d, labels, props) + require.NoError(t, err) + + assert.Empty(t, nodes, fmt.Sprintf( + "Expected no nodes with labels=%s and props=%s\n\nGot nodes:\n\n%s", + labels, props, formatList(nodes), + )) +} + +// ---------------------------------------------------------------------------- +// Matchers +// ---------------------------------------------------------------------------- + +func matchNodes( + ctx context.Context, + d neo4j.Driver, + labels []string, + props Properties, +) ([]*Node, error) { + l := ToCypherLabels(labels) + keys := make([]string, 0, len(props)) + for k := range props { + keys = append(keys, k) + } + p := ToCypherProps(keys, "") + + query := fmt.Sprintf("MATCH (n%s { %s }) RETURN n", l, p) + result, err := execute(ctx, d, query, props) + + if err != nil { + return nil, err + } + + nodes := make([]*Node, 0, len(result.Records)) + for _, r := range result.Records { + rn, _ := r.Get("n") + nn, ok := rn.(neo4j.Node) + if !ok { + return nil, errors.New("record is not a node") + } + nodes = append(nodes, NewNodeFromNeo4j(nn)) + } + + return nodes, nil +} + +func matchRels( + ctx context.Context, + d neo4j.Driver, + rtype string, + startID string, + endID string, + props Properties, +) ([]*Relationship, error) { + t := ToCypherLabel(rtype) + + keys := make([]string, 0, len(props)) + for k := range props { + keys = append(keys, k) + } + p := ToCypherProps(keys, "$props.") + + queryProps := map[string]any{ + "props": props, + "start_id": startID, + "end_id": endID, + } + + query := fmt.Sprintf(` + MATCH (start)-[r%s { %s }]->(end) + WHERE elementId(start) = $start_id AND elementId(end) = $end_id + RETURN r + `, t, p) + result, err := neo4j.ExecuteQuery(ctx, d, query, queryProps, + neo4j.EagerResultTransformer, + neo4j.ExecuteQueryWithDatabase("neo4j")) + + if err != nil { + return nil, err + } + + rels := make([]*Relationship, 0, len(result.Records)) + for _, r := range result.Records { + rr, _ := r.Get("r") + nr, ok := rr.(neo4j.Relationship) + if !ok { + return nil, errors.New("record is not a relationship") + } + rels = append(rels, NewRelationshipFromNeo4j(nr)) + } + + return rels, nil +} + +func TestMatchNodes(t *testing.T) { + s := freshStores(t) + ctx := t.Context() + + // seed graph + seed := ` + CREATE (n1:Event { id: "1", pubkey: "alice" }) + CREATE (n2:Event { id: "2", pubkey: "bob" }) + CREATE (n3:Tag { name: "a", value: "1" }) + CREATE (n4:Tag { name: "b", value: "2" }) + CREATE (n5:Relay:LiveRelay { url: "relay.com" }) + RETURN [ + elementId(n1), + elementId(n2), + elementId(n3), + elementId(n4), + elementId(n5) + ] AS ids + ` + result, err := execute(ctx, s.graph, seed, nil) + require.NoError(t, err) + require.Len(t, result.Records, 1) + + createdRaw, _ := result.Records[0].Get("ids") + created := createdRaw.([]interface{}) + require.Len(t, created, 5) + + // run tests + cases := []struct { + name string + labels []string + props Properties + expected []int + }{ + { + "event nodes", + []string{"Event"}, + nil, + []int{0, 1}, + }, + { + "tag nodes", + []string{"Tag"}, + nil, + []int{2, 3}, + }, + { + "multilabel nodes", + []string{"Relay", "LiveRelay"}, + nil, + []int{4}, + }, + { + "by property", + []string{"Event"}, + map[string]any{"pubkey": "alice"}, + []int{0}, + }, + { + "no matches on label", + []string{"Nonexisting"}, + nil, + []int{}, + }, + { + "no matches on props", + []string{"Event"}, + map[string]any{"nonexisting": "key"}, + []int{}, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + nodes, err := matchNodes(ctx, s.graph, tc.labels, tc.props) + assert.NoError(t, err) + assert.Len(t, nodes, len(tc.expected)) + + got := map[string]struct{}{} + for _, n := range nodes { + got[n.ElementID] = struct{}{} + } + + for _, i := range tc.expected { + assert.Contains(t, got, created[i]) + } + }) + } +} + +func TestMatchRels(t *testing.T) { + s := freshStores(t) + ctx := t.Context() + + // seed graph + seed := ` + CREATE (n1:Event { id: "1", pubkey: "alice" }) + CREATE (n2:Event { id: "2", pubkey: "bob" }) + CREATE (n3:Tag { name: "a", value: "1" }) + CREATE (n4:Tag { name: "b", value: "2" }) + CREATE (n1)-[r1:TAGGED]->(n3) + CREATE (n1)-[r2:TAGGED { extra: true }]->(n3) + CREATE (n2)-[r3:TAGGED]->(n4) + RETURN [ elementId(n1), elementId(n2), elementId(n3), elementId(n4) ] AS node_ids, + [ elementId(r1), elementId(r2), elementId(r3) ] AS rel_ids + ` + result, err := execute(ctx, s.graph, seed, nil) + require.NoError(t, err) + require.Len(t, result.Records, 1) + + createdNodesRaw, _ := result.Records[0].Get("node_ids") + createdRelsRaw, _ := result.Records[0].Get("rel_ids") + createdNodes := createdNodesRaw.([]interface{}) + createdRels := createdRelsRaw.([]interface{}) + require.Len(t, createdNodes, 4) + require.Len(t, createdRels, 3) + + // run tests + cases := []struct { + name string + rtype string + startId interface{} + endId interface{} + props Properties + expected []int + }{ + { + "tagged rel", + "TAGGED", + createdNodes[1], + createdNodes[3], + nil, + []int{2}, + }, + { + "multiple rels", + "TAGGED", + createdNodes[0], + createdNodes[2], + nil, + []int{0, 1}, + }, + { + "by property", + "TAGGED", + createdNodes[0], + createdNodes[2], + map[string]any{"extra": true}, + []int{1}, + }, + { + "nonexisting property", + "TAGGED", + createdNodes[0], + createdNodes[2], + map[string]any{"nonexisting": "prop"}, + []int{}, + }, + { + "nonexisting start", + "TAGGED", + "nonexisting", + createdNodes[0], + nil, + []int{}, + }, + { + "nonexisting end", + "TAGGED", + createdNodes[0], + "nonexisting", + nil, + []int{}, + }, + { + "nonexisting start and end", + "TAGGED", + "nonexisting1", + "nonexisting2", + nil, + []int{}, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rels, err := matchRels(ctx, s.graph, + tc.rtype, tc.startId.(string), tc.endId.(string), tc.props) + assert.NoError(t, err) + assert.Len(t, rels, len(tc.expected)) + + got := map[string]struct{}{} + for _, r := range rels { + got[r.ElementID] = struct{}{} + } + + for _, i := range tc.expected { + assert.Contains(t, got, createdRels[i]) + } + }) + } +} + +// ---------------------------------------------------------------------------- +// Helpers +// ---------------------------------------------------------------------------- + +func execute( + ctx context.Context, d neo4j.Driver, q string, p map[string]any, +) (*neo4j.EagerResult, error) { + return neo4j.ExecuteQuery(ctx, d, q, p, + neo4j.EagerResultTransformer, neo4j.ExecuteQueryWithDatabase("neo4j")) +} + +func formatMap[T fmt.Stringer](data map[string]T) string { + items := make([]T, 0, len(data)) + for _, k := range slices.Sorted(maps.Keys(data)) { + items = append(items, data[k]) + } + return formatList[T](items) +} + +func formatList[T fmt.Stringer](items []T) string { + var b strings.Builder + fmt.Fprintf(&b, "len=%d", len(items)) + for _, item := range items { + fmt.Fprintf(&b, "\n %v", item) + } + return b.String() +} diff --git a/schema.go b/schema.go index ca70447..c95fd0f 100644 --- a/schema.go +++ b/schema.go @@ -26,21 +26,21 @@ func NewSimpleMatchKeys() *SimpleMatchKeys { // ---------------------------------------------------------------------------- func NewUserNode(pubkey string) *Node { - return NewNode("User", Properties{"pubkey": pubkey}) + return NewNode([]string{"User"}, Properties{"pubkey": pubkey}) } func NewEventNode(id string) *Node { - return NewNode("Event", Properties{"id": id}) + return NewNode([]string{"Event"}, Properties{"id": id}) } func NewTagNode(name string, value string) *Node { - return NewNode("Tag", Properties{ + return NewNode([]string{"Tag"}, Properties{ "name": name, "value": value}) } func NewReplacementKeyNode(pubkey string, kind int) *Node { - return NewNode("ReplacementKey", Properties{ + return NewNode([]string{"ReplacementKey"}, Properties{ "pubkey": pubkey, "kind": kind, }) diff --git a/subgraph.go b/subgraph.go index 5dc8115..8d84c2c 100644 --- a/subgraph.go +++ b/subgraph.go @@ -6,35 +6,35 @@ import ( // Types -type EventSubgraph struct { +type Subgraph struct { nodes []*Node rels []*Relationship } -func NewEventSubgraph() *EventSubgraph { - return &EventSubgraph{ +func NewSubgraph() *Subgraph { + return &Subgraph{ nodes: []*Node{}, rels: []*Relationship{}, } } -func (s *EventSubgraph) AddNode(node *Node) { +func (s *Subgraph) AddNode(node *Node) { s.nodes = append(s.nodes, node) } -func (s *EventSubgraph) AddRel(rel *Relationship) { +func (s *Subgraph) AddRel(rel *Relationship) { s.rels = append(s.rels, rel) } -func (s *EventSubgraph) Nodes() []*Node { +func (s *Subgraph) Nodes() []*Node { return s.nodes } -func (s *EventSubgraph) Rels() []*Relationship { +func (s *Subgraph) Rels() []*Relationship { return s.rels } -func (s *EventSubgraph) NodesByLabel(label string) []*Node { +func (s *Subgraph) NodesByLabel(label string) []*Node { var nodes []*Node for _, node := range s.nodes { if node.Labels.Contains(label) { @@ -44,7 +44,7 @@ func (s *EventSubgraph) NodesByLabel(label string) []*Node { return nodes } -func (s *EventSubgraph) FirstNodeByLabel(label string) *Node { +func (s *Subgraph) FirstNodeByLabel(label string) *Node { for _, node := range s.nodes { if node.Labels.Contains(label) { return node @@ -65,8 +65,8 @@ func IsValidTag(t roots.Tag) bool { // Event to subgraph pipeline -func EventToSubgraph(e roots.ValidatedEvent, p ExpanderPipeline) *EventSubgraph { - s := NewEventSubgraph() +func EventToSubgraph(e roots.ValidatedEvent, p ExpanderPipeline) *Subgraph { + s := NewSubgraph() // Create core entities eventNode := newEventNode(e.ID(), e.CreatedAt(), e.Kind(), e.Content()) @@ -133,7 +133,7 @@ func newTagRels(event *Node, tags []*Node) []*Relationship { // Expander Pipeline -type Expander func(e roots.ValidatedEvent, s *EventSubgraph) +type Expander func(e roots.ValidatedEvent, s *Subgraph) type ExpanderPipeline []Expander func NewExpanderPipeline(expanders ...Expander) ExpanderPipeline { @@ -148,7 +148,7 @@ func DefaultExpanders() []Expander { } } -func ExpandTaggedEvents(e roots.ValidatedEvent, s *EventSubgraph) { +func ExpandTaggedEvents(e roots.ValidatedEvent, s *Subgraph) { for _, tag := range s.NodesByLabel("Tag") { if tag.Props["name"] != "e" { continue @@ -166,7 +166,7 @@ func ExpandTaggedEvents(e roots.ValidatedEvent, s *EventSubgraph) { } } -func ExpandTaggedUsers(e roots.ValidatedEvent, s *EventSubgraph) { +func ExpandTaggedUsers(e roots.ValidatedEvent, s *Subgraph) { for _, tag := range s.NodesByLabel("Tag") { if tag.Props["name"] != "p" { continue @@ -184,7 +184,7 @@ func ExpandTaggedUsers(e roots.ValidatedEvent, s *EventSubgraph) { } } -func ExpandReplaceableEvents(e roots.ValidatedEvent, s *EventSubgraph) { +func ExpandReplaceableEvents(e roots.ValidatedEvent, s *Subgraph) { if e.Kind() != 0 && e.Kind() != 3 && !(e.Kind() >= 10000 && e.Kind() < 20000) { return } @@ -211,7 +211,7 @@ func ExpandReplaceableEvents(e roots.ValidatedEvent, s *EventSubgraph) { s.AddRel(NewForUserRel(rk, authorNode, nil)) } -func ExpandAddressableEvents(e roots.ValidatedEvent, s *EventSubgraph) { +func ExpandAddressableEvents(e roots.ValidatedEvent, s *Subgraph) { if !(e.Kind() >= 30000 && e.Kind() < 40000) { return } diff --git a/subgraph_test.go b/subgraph_test.go index da37f1d..7382954 100644 --- a/subgraph_test.go +++ b/subgraph_test.go @@ -8,8 +8,8 @@ import ( "testing" ) -func baseSubgraph(e roots.ValidatedEvent) (*EventSubgraph, *Node, *Node) { - s := NewEventSubgraph() +func baseSubgraph(e roots.ValidatedEvent) (*Subgraph, *Node, *Node) { + s := NewSubgraph() eventNode := newEventNode(e.ID(), e.CreatedAt(), e.Kind(), e.Content()) userNode := NewUserNode(e.PubKey()) s.AddNode(eventNode) @@ -24,12 +24,12 @@ func TestEventToSubgraph(t *testing.T) { cases := []struct { name string event roots.ValidatedEvent - expected *EventSubgraph + expected *Subgraph }{ { name: "bare event", event: fx.ValidatedEvent(t, "bare"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "bare") s, _, _ := baseSubgraph(e) return s @@ -38,7 +38,7 @@ func TestEventToSubgraph(t *testing.T) { { name: "single generic tag", event: fx.ValidatedEvent(t, "generic_tag"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "generic_tag") s, eventNode, _ := baseSubgraph(e) tagNode := NewTagNode("t", "bitcoin") @@ -50,7 +50,7 @@ func TestEventToSubgraph(t *testing.T) { { name: "e tag with valid hex64", event: fx.ValidatedEvent(t, "e_tag_valid"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "e_tag_valid") carolID := fx.ValidatedEvent(t, "carol_placeholder").ID() s, eventNode, _ := baseSubgraph(e) @@ -66,7 +66,7 @@ func TestEventToSubgraph(t *testing.T) { { name: "e tag with invalid value", event: fx.ValidatedEvent(t, "e_tag_invalid"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "e_tag_invalid") s, eventNode, _ := baseSubgraph(e) tagNode := NewTagNode("e", "notvalid") @@ -78,7 +78,7 @@ func TestEventToSubgraph(t *testing.T) { { name: "p tag with valid hex64", event: fx.ValidatedEvent(t, "p_tag_valid"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "p_tag_valid") bobPubkey := fx.Keys["bob"] s, eventNode, _ := baseSubgraph(e) @@ -94,7 +94,7 @@ func TestEventToSubgraph(t *testing.T) { { name: "p tag with invalid value", event: fx.ValidatedEvent(t, "p_tag_invalid"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "p_tag_invalid") s, eventNode, _ := baseSubgraph(e) tagNode := NewTagNode("p", "notvalid") @@ -106,7 +106,7 @@ func TestEventToSubgraph(t *testing.T) { { name: "replaceable kind 0", event: fx.ValidatedEvent(t, "replaceable_k0"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "replaceable_k0") s, eventNode, userNode := baseSubgraph(e) rk := NewReplacementKeyNode(e.PubKey(), e.Kind()) @@ -119,7 +119,7 @@ func TestEventToSubgraph(t *testing.T) { { name: "replaceable kind 3", event: fx.ValidatedEvent(t, "replaceable_k3"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "replaceable_k3") s, eventNode, userNode := baseSubgraph(e) rk := NewReplacementKeyNode(e.PubKey(), e.Kind()) @@ -132,7 +132,7 @@ func TestEventToSubgraph(t *testing.T) { { name: "replaceable kind 10000-19999", event: fx.ValidatedEvent(t, "replaceable_k10k"), - expected: func() *EventSubgraph { + expected: func() *Subgraph { e := fx.ValidatedEvent(t, "replaceable_k10k") s, eventNode, userNode := baseSubgraph(e) rk := NewReplacementKeyNode(e.PubKey(), e.Kind()) @@ -220,7 +220,7 @@ func propsEqual(expected, got Properties) error { return nil } -func assertSubgraphsEqual(t *testing.T, expected, got *EventSubgraph) { +func assertSubgraphsEqual(t *testing.T, expected, got *Subgraph) { t.Helper() gotNodes := make([]*Node, len(got.Nodes())) diff --git a/write.go b/write.go index fcf54b0..33006f8 100644 --- a/write.go +++ b/write.go @@ -18,7 +18,7 @@ type WriteOptions struct { type preparedWrite struct { Event roots.ValidatedEvent - Subgraph *EventSubgraph + Subgraph *Subgraph } type ExcludedEvent struct {