-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_store_test.go
76 lines (68 loc) · 1.82 KB
/
message_store_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
package arbor_test
import (
"math/rand"
"strconv"
"testing"
arbor "github.com/arborchat/arbor-go"
)
// TestNewStore ensures that NewStore returns a store.
func TestNewStore(t *testing.T) {
s := arbor.NewStore()
if s == nil {
t.Error("NewStore() returned a nil store")
}
}
func randomMessage() *arbor.ChatMessage {
m, _ := arbor.NewChatMessage(strconv.Itoa(rand.Int()))
_ = m.AssignID()
m.Username = strconv.Itoa(rand.Int())
return m
}
const iterations = 10000
// TestAddAndGet ensures that any message inserted into the store with "Add" can be
// retrieved with Get.
func TestAddAndGet(t *testing.T) {
s := arbor.NewStore()
if s == nil {
t.Skip("Got nil store")
}
msgs := make([]*arbor.ChatMessage, iterations)
for i := 0; i < iterations; i++ {
msgs[i] = randomMessage()
s.Add(msgs[i])
}
for i := 0; i < iterations; i++ {
m := s.Get(msgs[i].UUID)
if m == nil {
t.Error("Failed to retrieve message previously stored")
}
if m.UUID != msgs[i].UUID {
t.Errorf("Expected %s, found %s", msgs[i].UUID, m.UUID)
}
if m.Parent != msgs[i].Parent {
t.Errorf("Expected %s, found %s", msgs[i].Parent, m.Parent)
}
if m.Timestamp != msgs[i].Timestamp {
t.Errorf("Expected %d, found %d", msgs[i].Timestamp, m.Timestamp)
}
if m.Username != msgs[i].Username {
t.Errorf("Expected %s, found %s", msgs[i].Username, m.Username)
}
if m.Content != msgs[i].Content {
t.Errorf("Expected %s, found %s", msgs[i].Content, m.Content)
}
}
}
const nonexsitentID = "nonexistent"
// TestGetNotAdded ensures that Get() returns nil when you ask for a message that has
// never been Add()-ed
func TestGetNotAdded(t *testing.T) {
s := arbor.NewStore()
if s == nil {
t.Skip("Got nil store")
}
m := s.Get(nonexsitentID)
if m != nil {
t.Error("Recieved non-nil message when getting a non-existent message ID", m)
}
}