-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_test.go
165 lines (139 loc) · 3.22 KB
/
db_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
160
161
162
163
164
165
package main
import (
"bytes"
"testing"
)
// Creates a memory-backed DB for testing
func CreateDB(events []TalkEvent) *Talks {
// Create a writer to save the events
buf := bytes.NewBuffer(nil)
// Create the DB
db, err := NewTalks(bytes.NewReader(nil), buf)
if err != nil {
panic(err)
}
// Apply the events
for _, event := range events {
if err := db.event(event); err != nil {
panic(err)
}
// Write the event to the buffer
if err := db.write(event); err != nil {
panic(err)
}
}
return db
}
func TestDB(t *testing.T) {
events := []TalkEvent{
{Type: Create, Create: &CreateTalkEvent{
ID: 0,
Name: "",
Type: 0,
Description: "",
Week: "20230705",
}},
{Type: Create, Create: &CreateTalkEvent{
ID: 1,
Name: "Test Talk",
Type: 0,
Description: "This is a test talk",
Week: "20230705",
}},
{Type: Hide, Hide: &HideTalkEvent{
ID: 1,
}},
{Type: Delete, Delete: &DeleteTalkEvent{
ID: 0,
}},
{Type: Create, Create: &CreateTalkEvent{
ID: 3,
Name: "Test Talk 2",
Type: 0,
Description: "This is a test talk",
Week: "20230705",
}},
}
db := CreateDB(events)
// Check the state
if len(db.talks) != 2 {
t.Fatal("Expected 2 talks")
}
// Verify that the talk was hidden
if !db.talks[1].Hidden {
t.Fatal("Expected talk to be hidden")
}
// Verify that the talk was deleted
if _, ok := db.talks[0]; ok {
t.Fatal("Expected talk to be deleted")
}
// Verify that talk 2 doesn't exist
if _, ok := db.talks[2]; ok {
t.Fatal("Expected talk to be missing")
}
}
// Verify that deleting a talk that doesn't exist doesn't cause a panic
func TestDeleteNonExistentTalk(t *testing.T) {
events := []TalkEvent{
{Type: Delete, Delete: &DeleteTalkEvent{
ID: 0,
}},
}
db := CreateDB(events)
// Check the state
if len(db.talks) != 0 {
t.Fatal("Expected 0 talks")
}
}
// Verify that hiding a talk that doesn't exist doesn't cause a panic
func TestHideNonExistentTalk(t *testing.T) {
events := []TalkEvent{
{Type: Hide, Hide: &HideTalkEvent{
ID: 0,
}},
}
db := CreateDB(events)
// Check the state
if len(db.talks) != 0 {
t.Fatal("Expected 0 talks")
}
}
// Verify that leaving holes in the ID sequence doesn't cause a panic
func TestHolesInIDSequence(t *testing.T) {
events := []TalkEvent{
{Type: Create, Create: &CreateTalkEvent{
ID: 0,
Name: "",
Type: 0,
Description: "",
Week: "20230705",
}},
{Type: Create, Create: &CreateTalkEvent{
ID: 2,
Name: "Test Talk",
Type: 0,
Description: "This is a test talk",
Week: "20230705",
}},
}
db := CreateDB(events)
// Check the state
if len(db.talks) != 2 {
t.Fatal("Expected 2 talks")
}
// assert that talk 0 has the correct info
if db.talks[0] == nil {
t.Fatal("Expected talk 0 to exist")
}
if db.talks[1] != nil {
t.Fatal("Expected talk 1 to not exist")
}
if db.talks[2] == nil {
t.Fatal("Expected talk 2 to exist")
}
// Create a new event and expect it to have ID 3
db.Create("Test Talk 2", ForumTopic, "This is a test talk", "20230705")
if db.talks[3] == nil {
t.Fatal("Expected talk 3 to exist")
}
}