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