-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
244 lines (211 loc) · 4.25 KB
/
main_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main_test
// some integration tests to make my life easier
import (
"database/sql"
"fmt"
"lommix/wichtelbot/server/store"
"math/rand"
"testing"
_ "github.com/mattn/go-sqlite3"
)
const TestPartySize = 100
// love sqlite in tests. its so easy
func OpenTestDb(t *testing.T) *sql.DB{
db, err := sql.Open("sqlite3", "wichtel_test.db")
if err != nil {
t.Fatal(err)
}
err = store.SchemaDown(db)
if err != nil {
t.Fatal(err)
}
err = store.SchemaUp(db)
if err != nil {
t.Fatal(err)
}
return db
}
// simple test to make sure everybody has a partner
func TestPlay(t *testing.T) {
db := OpenTestDb(t)
party,err := store.CreateParty(db)
if err != nil {
t.Fatal(err)
}
for i := 0; i < TestPartySize; i {
_, err = store.CreateUser(
db,
party.Id,
fmt.Sprint("test_name_",i),
"test",
"test_notice",
store.DefaultUser,
)
if err != nil {
t.Fatal(err)
}
}
err = party.RollPartners(db)
if err != nil {
t.Fatal(err)
}
users, err := store.FindUsersByPartyId(db, party.Id)
if err != nil {
t.Fatal(err)
}
if len(users) != TestPartySize {
t.Fatal("less user in db than expected")
}
for _, user := range users {
if user.PartnerId == 0 {
t.Fatal("user has no partner")
}
}
unique := make(map[int64]bool)
for _, u := range users {
if u.PartnerId == 0 {
t.Fatal("no partner")
}
unique[u.PartnerId] = true
}
if len(unique) != TestPartySize {
t.Fatal("not all users have partners")
}
}
// simple test to make sure everybody still has a partner when using the blacklist
func TestBlacklistPlay(t *testing.T) {
db := OpenTestDb(t)
party,err := store.CreateParty(db)
if err != nil {
t.Fatal(err)
}
for i := 0; i < TestPartySize; i {
_, err = store.CreateUser(
db,
party.Id,
fmt.Sprint("test_name_",i),
"test",
"test_notice",
store.DefaultUser,
)
if err != nil {
t.Fatal(err)
}
}
users, err := store.FindUsersByPartyId(db, party.Id)
if err != nil {
t.Fatal(err)
}
// test blacklist
idStack := make([]int64, TestPartySize)
for i := 0; i < TestPartySize; i {
idStack[i] = int64(i 1)
}
rand.Shuffle(len(idStack), func(i, j int) {
idStack[i], idStack[j] = idStack[j], idStack[i]
})
for i, u := range users {
u.ExcludeId = idStack[i]
u.PartnerId = 0
u.Update(db)
}
party.Blacklist = true
err = party.RollPartners(db)
if err != nil {
t.Fatal(err)
}
users, err = store.FindUsersByPartyId(db, party.Id)
if err != nil {
t.Fatal(err)
}
unique := make(map[int64]bool)
for _, u := range users {
if u.PartnerId == 0 {
t.Fatal("no partner")
}
unique[u.PartnerId] = true
}
if len(unique) != TestPartySize {
t.Fatal("blacklist failed, not all users have partners")
}
}
// test if the blacklist fails correctly if impossible party
func TestBlacklistFail(t *testing.T) {
db := OpenTestDb(t)
party,err := store.CreateParty(db)
if err != nil {
t.Fatal(err)
}
for i := 0; i < 3; i {
_, err = store.CreateUser(
db,
party.Id,
fmt.Sprint("test_name_",i),
"test",
"test_notice",
store.DefaultUser,
)
if err != nil {
t.Fatal(err)
}
}
users, err := store.FindUsersByPartyId(db, party.Id)
if err != nil {
t.Fatal(err)
}
users[0].ExcludeId = users[2].Id
users[1].ExcludeId = users[0].Id
users[2].ExcludeId = users[0].Id
for _, u := range users {
u.Update(db)
}
party.Blacklist = true
err = party.RollPartners(db)
if err == nil {
t.Fatal("should have failed")
}
}
// testing single sql query page load
func TestFastQuery(t *testing.T){
db := OpenTestDb(t)
party,err := store.CreateParty(db)
if err != nil {
t.Fatal(err)
}
for i := 0; i < TestPartySize; i {
_, err = store.CreateUser(
db,
party.Id,
fmt.Sprint("test_name_",i),
"test",
"test_notice",
store.DefaultUser,
)
if err != nil {
t.Fatal(err)
}
}
err = party.RollPartners(db)
if err != nil {
t.Fatal(err)
}
user, err := store.FindUserWithPartyFast(db, 1)
if err != nil {
t.Fatal(err)
}
if len(*user.Party.Users) < TestPartySize {
t.Fatal("missing party members")
}
if user.PartnerId == 0 {
t.Fatal("no partner id")
}
if user.Party == nil {
t.Fatal("no party")
}
if user.Partner == nil {
t.Fatal("no partner")
}
if user.Partner.Id != user.PartnerId{
t.Fatal("partner id does not match partner")
}
}