-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_test.go
124 lines (111 loc) · 3.49 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
package goraff_test
import (
"testing"
"github.com/lordtatty/goraff"
"github.com/lordtatty/goraff/mocks"
"github.com/stretchr/testify/assert"
)
func TestNodeState(t *testing.T) {
assert := assert.New(t)
n := goraff.Node{}
n.SetStr("key1", "value1")
n.SetStr("key2", "value2")
r := n.Get()
assert.Equal("value1", r.FirstStr("key1"))
assert.Equal("value2", r.FirstStr("key2"))
n.SetStr("key3", "value3")
assert.Equal("value1", r.FirstStr("key1"))
assert.Equal("value2", r.FirstStr("key2"))
assert.Equal("value3", r.FirstStr("key3"))
}
func TestNodeState_GetOnUninitialisedState(t *testing.T) {
assert := assert.New(t)
n := goraff.Node{}
assert.Equal("", n.Get().FirstStr("key1"))
}
func TestNodeState_ID(t *testing.T) {
assert := assert.New(t)
n := goraff.Node{}
assert.Regexp("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", n.Get().ID())
}
func TestState_NodeStateByName(t *testing.T) {
assert := assert.New(t)
s := goraff.Graph{}
// Test that this creates a new node state
n := s.NewNode("node1", nil)
n.SetStr("key1", "value1")
n.SetStr("key2", "value2")
r := n.Get()
assert.Equal("value1", r.FirstStr("key1"))
assert.Equal("value2", r.FirstStr("key2"))
// Test that this returns the same already-created state
n2 := s.NewNode("node1", nil)
assert.Equal("value1", r.FirstStr("key1"))
assert.Equal("value2", r.FirstStr("key2"))
// Test the id
r2 := n2.Get()
assert.Equal("node1", r2.Name())
}
func TestState_NodeStateByID(t *testing.T) {
assert := assert.New(t)
s := goraff.Graph{}
// Test that this creates a new node state
n := s.NewNode("node1", nil)
n.SetStr("key1", "value1")
n.SetStr("key", "value2")
r := n.Get()
assert.Equal("value1", r.FirstStr("key1"))
assert.Equal("value2", r.FirstStr("key"))
// Test that this returns the same already-created state
n2 := s.NodeByID(n.Get().ID())
assert.Equal("value1", r.FirstStr("key1"))
assert.Equal("value2", r.FirstStr("key"))
// Test the id
r2 := n2.Get()
assert.Equal("node1", r2.Name())
}
func TestState_StateReadOnly(t *testing.T) {
assert := assert.New(t)
s := &goraff.Graph{}
// Test that this creates a new node state
n := s.NewNode("node1", nil)
n.SetStr("key1", "value1")
n.SetStr("key2", "value2")
r := goraff.NewReadableGraph(s)
nr, err := r.Node(n.Get().ID())
assert.Nil(err)
assert.Equal("value1", nr.FirstStr("key1"))
assert.Equal("value2", nr.FirstStr("key2"))
}
func TestState_StateReadOnly_ID(t *testing.T) {
assert := assert.New(t)
s := &goraff.Graph{}
// Test that this creates a new node state
n := s.NewNode("node1", nil)
n.SetStr("key1", "value1")
n.SetStr("key2", "value2")
r := goraff.NewReadableGraph(s)
nr, err := r.Node(n.Get().ID())
assert.Nil(err)
assert.Equal("node1", nr.Name())
}
func TestState_Notifier(t *testing.T) {
// Make sure this fires when updating a node
mNotifier := mocks.NewChangeNotifier(t)
mNotifier.EXPECT().Notify(goraff.GraphChangeNotification{NodeID: "node1"}).Times(1)
// And let's check a second node too to be sure
mNotifier.EXPECT().Notify(goraff.GraphChangeNotification{NodeID: "node2"}).Times(1)
// Create the SUT and trigger the first node to update
s := &goraff.Graph{Notifier: mNotifier}
n := s.NewNode("node1", nil)
n.SetStr("key", "value")
// Trigger the second node to update
n2 := s.NewNode("node2", nil)
n2.SetStr("key", "value")
}
func TestStateReader_ID(t *testing.T) {
assert := assert.New(t)
s := &goraff.Graph{}
r := goraff.NewReadableGraph(s)
assert.Regexp("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", r.ID())
}