hof_test.gno
9.03 Kb ยท 344 lines
1package hor
2
3import (
4 "std"
5 "testing"
6
7 "gno.land/p/moul/addrset"
8 "gno.land/p/nt/testutils"
9 "gno.land/p/nt/uassert"
10 "gno.land/p/nt/urequire"
11)
12
13const (
14 rlmPath = "gno.land/r/gnoland/home"
15 rlmPath2 = "gno.land/r/gnoland/test2"
16
17 rlmPath3 = "gno.land/r/gnoland/test3"
18 rlmPath4 = "gno.land/r/gnoland/test4"
19 rlmPath5 = "gno.land/r/gnoland/test5"
20
21 validTitle = "valid title"
22 invalidTitle = "This title is very very very long, longer than 30 characters"
23 validDesc = "valid description"
24 invalidDescription = "This description is very very very long, longer than 50 characters"
25)
26
27var (
28 admin std.Address = "g125em6arxsnj49vx35f0n0z34putv5ty3376fg5" // hardcoded from config.OwnableMain
29 adminRealm std.Realm
30 alice = testutils.TestAddress("alice")
31)
32
33func init() {
34 adminRealm = std.NewUserRealm(admin)
35}
36
37func TestRegister(t *testing.T) {
38 // Test user realm register
39 aliceRealm := std.NewUserRealm(alice)
40 testing.SetRealm(aliceRealm)
41
42 Register(cross, validTitle, validDesc)
43 uassert.False(t, itemExists(t, rlmPath))
44
45 // Test register while paused
46 testing.SetRealm(adminRealm)
47 err := Pausable.Pause()
48 uassert.NoError(t, err)
49
50 // Set legitimate caller
51 testing.SetRealm(std.NewCodeRealm(rlmPath))
52
53 Register(cross, validTitle, validDesc)
54 uassert.False(t, itemExists(t, rlmPath))
55
56 // Unpause
57 testing.SetRealm(adminRealm)
58 err = Pausable.Unpause()
59 uassert.NoError(t, err)
60
61 // Set legitimate caller
62 testing.SetRealm(std.NewCodeRealm(rlmPath))
63 Register(cross, validTitle, validDesc)
64
65 // Find registered items
66 uassert.True(t, itemExists(t, rlmPath))
67
68 // Test register with invalid title
69 testing.SetRealm(std.NewCodeRealm(rlmPath2))
70 Register(cross, invalidTitle, validDesc)
71 uassert.False(t, itemExists(t, rlmPath2))
72
73 // Test register with invalid description
74 testing.SetRealm(std.NewCodeRealm(rlmPath2))
75 Register(cross, validTitle, invalidDescription)
76 uassert.False(t, itemExists(t, rlmPath2))
77}
78
79func TestUpvote(t *testing.T) {
80 raw, _ := exhibition.items.Get(rlmPath)
81 item := raw.(*Item)
82
83 // 0 upvotes by default
84 urequire.Equal(t, item.upvote.Size(), 0)
85
86 testing.SetRealm(adminRealm)
87
88 urequire.NotPanics(t, func() {
89 Upvote(cross, rlmPath)
90 })
91
92 // Check both trees for 1 upvote
93 uassert.Equal(t, item.upvote.Size(), 1)
94
95 // Check double upvote
96 uassert.AbortsWithMessage(t, ErrDoubleUpvote.Error(), func() {
97 Upvote(cross, rlmPath)
98 })
99}
100
101func TestDownvote(t *testing.T) {
102 raw, _ := exhibition.items.Get(rlmPath)
103 item := raw.(*Item)
104
105 // 0 downvotes by default
106 urequire.Equal(t, item.downvote.Size(), 0)
107
108 userRealm := std.NewUserRealm(alice)
109 testing.SetRealm(userRealm)
110
111 urequire.NotPanics(t, func() {
112 Downvote(cross, rlmPath)
113 })
114
115 // Check both trees for 1 upvote
116 uassert.Equal(t, item.downvote.Size(), 1)
117
118 // Check double downvote
119 uassert.AbortsWithMessage(t, ErrDoubleDownvote.Error(), func() {
120 Downvote(cross, rlmPath)
121 })
122}
123
124func TestDelete(t *testing.T) {
125 userRealm := std.NewUserRealm(admin)
126 testing.SetRealm(userRealm)
127 testing.SetOriginCaller(admin)
128
129 uassert.AbortsWithMessage(t, ErrNoSuchItem.Error(), func() {
130 Delete(cross, "nonexistentpkgpath")
131 })
132
133 i, _ := exhibition.items.Get(rlmPath)
134 _ = i.(*Item).id
135
136 uassert.NotPanics(t, func() {
137 Delete(cross, rlmPath)
138 })
139
140 uassert.False(t, exhibition.items.Has(rlmPath))
141}
142
143func itemExists(t *testing.T, rlmPath string) bool {
144 t.Helper()
145
146 _, ok1 := exhibition.items.Get(rlmPath)
147
148 return ok1
149}
150
151func TestgetVoteSortKey(t *testing.T) {
152 i := &Item{
153 id: 1,
154 title: validTitle,
155 description: validDesc,
156 pkgpath: rlmPath,
157 blockNum: std.ChainHeight(),
158 upvote: &addrset.Set{},
159 downvote: &addrset.Set{},
160 }
161
162 i.upvote.Add(alice)
163
164 generatedKey := getVoteSortKey(i.upvote.Size(), i.id)
165 expectedKey := "0000000001:1"
166
167 urequire.Equal(t, generatedKey, expectedKey)
168}
169
170func TestSortByUpvote(t *testing.T) {
171 // Remove all items from all trees
172 exhibition.items.Iterate("", "", func(key string, value interface{}) bool {
173 exhibition.items.Remove(key)
174 return false
175 })
176 exhibition.itemsSortedByUpvotes.Iterate("", "", func(key string, value interface{}) bool {
177 exhibition.itemsSortedByUpvotes.Remove(key)
178 return false
179 })
180 exhibition.itemsSortedByDownvotes.Iterate("", "", func(key string, value interface{}) bool {
181 exhibition.itemsSortedByDownvotes.Remove(key)
182 return false
183 })
184 exhibition.itemsSortedByCreation.Iterate("", "", func(key string, value interface{}) bool {
185 exhibition.itemsSortedByCreation.Remove(key)
186 return false
187 })
188
189 // Add items
190 testing.SetRealm(std.NewCodeRealm(rlmPath3))
191 Register(cross, validTitle, validDesc)
192
193 testing.SetRealm(std.NewCodeRealm(rlmPath4))
194 Register(cross, validTitle, validDesc)
195
196 testing.SetRealm(std.NewCodeRealm(rlmPath5))
197 Register(cross, validTitle, validDesc)
198
199 user1 := testutils.TestAddress("user1")
200 user2 := testutils.TestAddress("user2")
201 user3 := testutils.TestAddress("user3")
202
203 testing.SetOriginCaller(user1)
204 testing.SetRealm(std.NewUserRealm(user1))
205 Upvote(cross, rlmPath3)
206 Upvote(cross, rlmPath4)
207 Upvote(cross, rlmPath5)
208
209 testing.SetOriginCaller(user2)
210 testing.SetRealm(std.NewUserRealm(user2))
211 Upvote(cross, rlmPath4)
212 Upvote(cross, rlmPath5)
213
214 testing.SetOriginCaller(user3)
215 testing.SetRealm(std.NewUserRealm(user3))
216 Upvote(cross, rlmPath5)
217
218 // We are displaying data in reverse order in render, so items should be sorted in reverse order
219 _, firstRawValue := exhibition.itemsSortedByUpvotes.GetByIndex(0)
220 firstValue := firstRawValue.(*Item)
221 uassert.Equal(t, firstValue.pkgpath, rlmPath3)
222
223 _, secondRawValue := exhibition.itemsSortedByUpvotes.GetByIndex(1)
224 secondValue := secondRawValue.(*Item)
225 uassert.Equal(t, secondValue.pkgpath, rlmPath4)
226}
227
228func TestSortByDownvote(t *testing.T) {
229 // Remove all items from all trees
230 exhibition.items.Iterate("", "", func(key string, value interface{}) bool {
231 exhibition.items.Remove(key)
232 return false
233 })
234 exhibition.itemsSortedByUpvotes.Iterate("", "", func(key string, value interface{}) bool {
235 exhibition.itemsSortedByUpvotes.Remove(key)
236 return false
237 })
238 exhibition.itemsSortedByDownvotes.Iterate("", "", func(key string, value interface{}) bool {
239 exhibition.itemsSortedByDownvotes.Remove(key)
240 return false
241 })
242 exhibition.itemsSortedByCreation.Iterate("", "", func(key string, value interface{}) bool {
243 exhibition.itemsSortedByCreation.Remove(key)
244 return false
245 })
246
247 // Add items
248 testing.SetRealm(std.NewCodeRealm(rlmPath3))
249 Register(cross, validTitle, validDesc)
250
251 testing.SetRealm(std.NewCodeRealm(rlmPath4))
252 Register(cross, validTitle, validDesc)
253
254 testing.SetRealm(std.NewCodeRealm(rlmPath5))
255 Register(cross, validTitle, validDesc)
256
257 user1 := testutils.TestAddress("user1")
258 user2 := testutils.TestAddress("user2")
259 user3 := testutils.TestAddress("user3")
260
261 testing.SetOriginCaller(user1)
262 testing.SetRealm(std.NewUserRealm(user1))
263 Downvote(cross, rlmPath3)
264 Downvote(cross, rlmPath4)
265 Downvote(cross, rlmPath5)
266
267 testing.SetOriginCaller(user2)
268 testing.SetRealm(std.NewUserRealm(user2))
269 Downvote(cross, rlmPath4)
270 Downvote(cross, rlmPath5)
271
272 testing.SetOriginCaller(user3)
273 testing.SetRealm(std.NewUserRealm(user3))
274 Downvote(cross, rlmPath5)
275
276 // We are dispalying data is reverse order in render, so items should be sorted in reverse order
277 _, firstRawValue := exhibition.itemsSortedByDownvotes.GetByIndex(0)
278
279 firstValue := firstRawValue.(*Item)
280
281 uassert.Equal(t, firstValue.pkgpath, rlmPath3)
282
283 _, secondRawValue := exhibition.itemsSortedByDownvotes.GetByIndex(1)
284
285 secondValue := secondRawValue.(*Item)
286
287 uassert.Equal(t, secondValue.pkgpath, rlmPath4)
288}
289
290func TestSortByCreation(t *testing.T) {
291 // Remove all items from all trees
292 exhibition.items.Iterate("", "", func(key string, value interface{}) bool {
293 exhibition.items.Remove(key)
294 return false
295 })
296 exhibition.itemsSortedByUpvotes.Iterate("", "", func(key string, value interface{}) bool {
297 exhibition.itemsSortedByUpvotes.Remove(key)
298 return false
299 })
300 exhibition.itemsSortedByDownvotes.Iterate("", "", func(key string, value interface{}) bool {
301 exhibition.itemsSortedByDownvotes.Remove(key)
302 return false
303 })
304 exhibition.itemsSortedByCreation.Iterate("", "", func(key string, value interface{}) bool {
305 exhibition.itemsSortedByCreation.Remove(key)
306 return false
307 })
308
309 testing.SkipHeights(10)
310 testing.SetRealm(std.NewCodeRealm(rlmPath3))
311 Register(cross, validTitle, validDesc)
312
313 testing.SkipHeights(10)
314 testing.SetRealm(std.NewCodeRealm(rlmPath4))
315 Register(cross, validTitle, validDesc)
316
317 testing.SkipHeights(10)
318 testing.SetRealm(std.NewCodeRealm(rlmPath5))
319 Register(cross, validTitle, validDesc)
320
321 // We are dispalying data is reverse order in render, so items should be sorted in reverse order
322 _, firstRawValue := exhibition.itemsSortedByCreation.GetByIndex(0)
323
324 firstValue := firstRawValue.(*Item)
325
326 uassert.Equal(t, firstValue.pkgpath, rlmPath3)
327
328 _, secondRawValue := exhibition.itemsSortedByCreation.GetByIndex(1)
329
330 secondValue := secondRawValue.(*Item)
331
332 uassert.Equal(t, secondValue.pkgpath, rlmPath4)
333}
334
335/* XXX fix @moul
336func TestGetScore(t *testing.T) {
337 score, err := h.GetScore(42)
338 uassert.Nil(t, err)
339 uassert.Equal(t, 0, score)
340 uassert.NotPanics(t, func() {
341 uassert.Equal(t, 0, h.MustGetScore(42))
342 })
343}
344*/