-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_test.go
159 lines (139 loc) · 3.69 KB
/
graph_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
djlogic "github.com/campadrenalin/go-deje/logic"
djmodel "github.com/campadrenalin/go-deje/model"
"reflect"
"testing"
)
type GraphTest interface {
GetExpected() GraphNode
GetResult() GraphNode
GetMessage() string
}
type GraphTestNode struct {
Node GraphNode
Expected GraphNode
FailureMsg string
}
func (gtn *GraphTestNode) GetResult() GraphNode { return gtn.Node }
func (gtn *GraphTestNode) GetExpected() GraphNode { return gtn.Expected }
func (gtn *GraphTestNode) GetMessage() string { return gtn.FailureMsg }
type GraphTestDocument struct {
Doc djlogic.Document
Expected GraphNode
FailureMsg string
}
func (gtd *GraphTestDocument) GetExpected() GraphNode { return gtd.Expected }
func (gtd *GraphTestDocument) GetMessage() string { return gtd.FailureMsg }
func (gtd *GraphTestDocument) GetResult() GraphNode {
root_node := NewRootNode()
root_node.PopulateRoot(gtd.Doc)
return root_node
}
func RunGraphTest(test GraphTest, t *testing.T) {
expected, result := test.GetExpected(), test.GetResult()
if !reflect.DeepEqual(expected, result) {
t.Log(expected)
t.Log(result)
t.Fatal(test.GetMessage())
}
}
func TestGraph_NewRootNode(t *testing.T) {
test := &GraphTestNode{
NewRootNode(),
GraphNode{
"root",
"root",
map[string]interface{}{
"about": graphRootExplanation,
},
make([]GraphNode, 0),
},
"Expected different value for root node",
}
RunGraphTest(test, t)
}
func TestGraph_NewEventNode(t *testing.T) {
event := djmodel.NewEvent("moose")
event.ParentHash = "applesauce"
event.Arguments["hello"] = "world"
test := &GraphTestNode{
NewEventNode(event),
GraphNode{
"moose",
"event",
map[string]interface{}{
"handler_name": "moose",
"hash": event.Hash(),
"parent_hash": "applesauce",
"arguments": map[string]interface{}{
"hello": "world",
},
},
make([]GraphNode, 0),
},
"Expected different value for event node",
}
RunGraphTest(test, t)
}
func TestGraph_Root(t *testing.T) {
test := &GraphTestDocument{
djlogic.NewDocument(),
NewRootNode(),
"Empty document should just result in root node alone",
}
RunGraphTest(test, t)
}
func TestGraph_GetRootEvents(t *testing.T) {
doc := djlogic.NewDocument()
event := djmodel.NewEvent("example")
event.Arguments["hello"] = "world"
doc.Events.Register(event)
root := NewRootNode()
root_events := root.GetRootEvents(doc)
expected := []djmodel.Event{event}
if !reflect.DeepEqual(root_events, expected) {
t.Fatalf("Expected %#v, got %#v", expected, root_events)
}
}
func TestGraph_OneEvent(t *testing.T) {
doc := djlogic.NewDocument()
event := djmodel.NewEvent("example")
event.Arguments["hello"] = "world"
doc.Events.Register(event)
root := NewRootNode()
root.Children = append(root.Children, NewEventNode(event))
test := &GraphTestDocument{
doc,
root,
"Graph should have one event as child of root",
}
RunGraphTest(test, t)
}
func TestGraph_ChainAndFork(t *testing.T) {
doc := djlogic.NewDocument()
first := djmodel.NewEvent("first")
second := djmodel.NewEvent("second")
third := djmodel.NewEvent("third")
fork := djmodel.NewEvent("fork")
second.ParentHash = first.Hash()
third.ParentHash = second.Hash()
fork.ParentHash = first.Hash()
for _, ev := range []djmodel.Event{first, second, third, fork} {
doc.Events.Register(ev)
}
root := NewRootNode()
node1 := NewEventNode(first)
node2 := NewEventNode(second)
node3 := NewEventNode(third)
nodeF := NewEventNode(fork)
node2.Children = []GraphNode{node3}
node1.Children = []GraphNode{node2, nodeF}
root.Children = []GraphNode{node1}
test := &GraphTestDocument{
doc,
root,
"Graph should have structure matching event tree",
}
RunGraphTest(test, t)
}