record_test.gno
11.14 Kb ยท 452 lines
1package commondao_test
2
3import (
4 "testing"
5
6 "gno.land/p/nt/uassert"
7 "gno.land/p/nt/urequire"
8
9 "gno.land/p/devx000/wip/nt/commondao"
10)
11
12func TestVotingRecordDefaults(t *testing.T) {
13 var (
14 record commondao.VotingRecord
15 user = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
16 )
17
18 uassert.Equal(t, record.Size(), 0)
19 uassert.Equal(t, record.VoteCount(commondao.ChoiceYes), 0)
20 uassert.Equal(t, record.VoteCount(commondao.ChoiceNo), 0)
21 uassert.Equal(t, record.VoteCount(commondao.ChoiceAbstain), 0)
22 uassert.False(t, record.HasVoted(user))
23}
24
25func TestVotingRecordAddVote(t *testing.T) {
26 cases := []struct {
27 name string
28 setup func(*commondao.VotingRecord)
29 votes []commondao.Vote
30 yesCount, noCount, abstainCount int
31 updated bool
32 }{
33 {
34 name: "single vote",
35 votes: []commondao.Vote{
36 {
37 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
38 Choice: commondao.ChoiceYes,
39 },
40 },
41 yesCount: 1,
42 },
43 {
44 name: "multiple votes",
45 votes: []commondao.Vote{
46 {
47 Address: "g125t352u4pmdrr57emc4pe04y40sknr5ztng5mt",
48 Choice: commondao.ChoiceNo,
49 },
50 {
51 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
52 Choice: commondao.ChoiceYes,
53 },
54 {
55 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
56 Choice: commondao.ChoiceNo,
57 },
58 {
59 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
60 Choice: commondao.ChoiceAbstain,
61 },
62 },
63 yesCount: 1,
64 noCount: 2,
65 abstainCount: 1,
66 },
67 {
68 name: "vote exists",
69 votes: []commondao.Vote{
70 {
71 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
72 Choice: commondao.ChoiceYes,
73 },
74 },
75 setup: func(r *commondao.VotingRecord) {
76 r.AddVote(commondao.Vote{
77 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
78 Choice: commondao.ChoiceAbstain,
79 })
80 },
81 yesCount: 1,
82 abstainCount: 0,
83 updated: true,
84 },
85 }
86
87 for _, tc := range cases {
88 t.Run(tc.name, func(t *testing.T) {
89 var (
90 record commondao.VotingRecord
91 updated bool
92 )
93
94 if tc.setup != nil {
95 tc.setup(&record)
96 }
97
98 for _, v := range tc.votes {
99 updated = updated || record.AddVote(v)
100 }
101
102 urequire.Equal(t, updated, tc.updated, "expect vote to be updated")
103 urequire.Equal(t, record.Size(), len(tc.votes), "expect record size to match")
104
105 var i int
106 record.Iterate(0, record.Size(), false, func(v commondao.Vote) bool {
107 uassert.Equal(t, v.Address, tc.votes[i].Address)
108 uassert.Equal(t, string(v.Choice), string(tc.votes[i].Choice))
109 uassert.True(t, record.HasVoted(v.Address))
110
111 i++
112 return false
113 })
114
115 uassert.Equal(t, record.VoteCount(commondao.ChoiceYes), tc.yesCount, "expect YES vote count to match")
116 uassert.Equal(t, record.VoteCount(commondao.ChoiceNo), tc.noCount, "expect NO vote count to match")
117 uassert.Equal(t, record.VoteCount(commondao.ChoiceAbstain), tc.abstainCount, "expect ABSTAIN vote count to match")
118 })
119 }
120}
121
122func TestFindMostVotedChoice(t *testing.T) {
123 cases := []struct {
124 name string
125 setup func(*commondao.VotingRecord)
126 choice commondao.VoteChoice
127 }{
128 {
129 name: "no votes",
130 choice: commondao.ChoiceNone,
131 },
132 {
133 name: "one vote",
134 setup: func(r *commondao.VotingRecord) {
135 r.AddVote(commondao.Vote{
136 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
137 Choice: commondao.ChoiceYes,
138 })
139 },
140 choice: commondao.ChoiceYes,
141 },
142 {
143 name: "multiple votes",
144 setup: func(r *commondao.VotingRecord) {
145 r.AddVote(commondao.Vote{
146 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
147 Choice: commondao.ChoiceNo,
148 })
149 r.AddVote(commondao.Vote{
150 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
151 Choice: commondao.ChoiceYes,
152 })
153 r.AddVote(commondao.Vote{
154 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
155 Choice: commondao.ChoiceNo,
156 })
157 },
158 choice: commondao.ChoiceNo,
159 },
160 {
161 name: "tie",
162 setup: func(r *commondao.VotingRecord) {
163 r.AddVote(commondao.Vote{
164 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
165 Choice: commondao.ChoiceYes,
166 })
167 r.AddVote(commondao.Vote{
168 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
169 Choice: commondao.ChoiceNo,
170 })
171 },
172 choice: commondao.ChoiceNone,
173 },
174 }
175
176 for _, tc := range cases {
177 t.Run(tc.name, func(t *testing.T) {
178 var record commondao.VotingRecord
179
180 if tc.setup != nil {
181 tc.setup(&record)
182 }
183
184 choice := commondao.FindMostVotedChoice(record.Readonly())
185
186 uassert.Equal(t, string(choice), string(tc.choice))
187 })
188 }
189}
190
191func TestSelectChoiceByAbsoluteMajority(t *testing.T) {
192 cases := []struct {
193 name string
194 setup func(*commondao.VotingRecord)
195 choice commondao.VoteChoice
196 membersCount int
197 success bool
198 }{
199 {
200 name: "no votes",
201 choice: commondao.ChoiceNone,
202 membersCount: 3,
203 success: false,
204 },
205 {
206 name: "majority",
207 setup: func(r *commondao.VotingRecord) {
208 r.AddVote(commondao.Vote{
209 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
210 Choice: commondao.ChoiceYes,
211 })
212 r.AddVote(commondao.Vote{
213 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
214 Choice: commondao.ChoiceYes,
215 })
216 r.AddVote(commondao.Vote{
217 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
218 Choice: commondao.ChoiceNo,
219 })
220 },
221 choice: commondao.ChoiceYes,
222 membersCount: 3,
223 success: true,
224 },
225 {
226 name: "no majority",
227 setup: func(r *commondao.VotingRecord) {
228 r.AddVote(commondao.Vote{
229 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
230 Choice: commondao.ChoiceYes,
231 })
232 r.AddVote(commondao.Vote{
233 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
234 Choice: commondao.ChoiceNo,
235 })
236 },
237 choice: "",
238 membersCount: 3,
239 success: false,
240 },
241 {
242 name: "majority with abstain vote",
243 setup: func(r *commondao.VotingRecord) {
244 r.AddVote(commondao.Vote{
245 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
246 Choice: commondao.ChoiceYes,
247 })
248 r.AddVote(commondao.Vote{
249 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
250 Choice: commondao.ChoiceYes,
251 })
252 r.AddVote(commondao.Vote{
253 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
254 Choice: commondao.ChoiceAbstain,
255 })
256 },
257 choice: commondao.ChoiceYes,
258 membersCount: 3,
259 success: true,
260 },
261 }
262
263 for _, tc := range cases {
264 t.Run(tc.name, func(t *testing.T) {
265 var record commondao.VotingRecord
266
267 if tc.setup != nil {
268 tc.setup(&record)
269 }
270
271 choice, success := commondao.SelectChoiceByAbsoluteMajority(record.Readonly(), tc.membersCount)
272
273 uassert.Equal(t, string(tc.choice), string(choice), "choice")
274 uassert.Equal(t, tc.success, success, "success")
275 })
276 }
277}
278
279func TestSelectChoiceBySuperMajority(t *testing.T) {
280 cases := []struct {
281 name string
282 setup func(*commondao.VotingRecord)
283 choice commondao.VoteChoice
284 membersCount int
285 success bool
286 }{
287 {
288 name: "no votes",
289 choice: commondao.ChoiceNone,
290 membersCount: 3,
291 success: false,
292 },
293 {
294 name: "majority",
295 setup: func(r *commondao.VotingRecord) {
296 r.AddVote(commondao.Vote{
297 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
298 Choice: commondao.ChoiceYes,
299 })
300 r.AddVote(commondao.Vote{
301 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
302 Choice: commondao.ChoiceYes,
303 })
304 r.AddVote(commondao.Vote{
305 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
306 Choice: commondao.ChoiceNo,
307 })
308 },
309 choice: commondao.ChoiceYes,
310 membersCount: 3,
311 success: true,
312 },
313 {
314 name: "no majority",
315 setup: func(r *commondao.VotingRecord) {
316 r.AddVote(commondao.Vote{
317 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
318 Choice: commondao.ChoiceYes,
319 })
320 r.AddVote(commondao.Vote{
321 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
322 Choice: commondao.ChoiceNo,
323 })
324 },
325 choice: "",
326 membersCount: 3,
327 success: false,
328 },
329 {
330 name: "majority with abstain vote",
331 setup: func(r *commondao.VotingRecord) {
332 r.AddVote(commondao.Vote{
333 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
334 Choice: commondao.ChoiceYes,
335 })
336 r.AddVote(commondao.Vote{
337 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
338 Choice: commondao.ChoiceYes,
339 })
340 r.AddVote(commondao.Vote{
341 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
342 Choice: commondao.ChoiceAbstain,
343 })
344 },
345 choice: commondao.ChoiceYes,
346 membersCount: 3,
347 success: true,
348 },
349 }
350
351 for _, tc := range cases {
352 t.Run(tc.name, func(t *testing.T) {
353 var record commondao.VotingRecord
354
355 if tc.setup != nil {
356 tc.setup(&record)
357 }
358
359 choice, success := commondao.SelectChoiceBySuperMajority(record.Readonly(), tc.membersCount)
360
361 uassert.Equal(t, string(tc.choice), string(choice), "choice")
362 uassert.Equal(t, tc.success, success, "success")
363 })
364 }
365}
366
367func TestSelectChoiceByPlurality(t *testing.T) {
368 cases := []struct {
369 name string
370 setup func(*commondao.VotingRecord)
371 choice commondao.VoteChoice
372 success bool
373 }{
374 {
375 name: "no votes",
376 choice: commondao.ChoiceNone,
377 success: false,
378 },
379 {
380 name: "plurality",
381 setup: func(r *commondao.VotingRecord) {
382 r.AddVote(commondao.Vote{
383 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
384 Choice: commondao.ChoiceYes,
385 })
386 r.AddVote(commondao.Vote{
387 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
388 Choice: commondao.ChoiceYes,
389 })
390 r.AddVote(commondao.Vote{
391 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
392 Choice: commondao.ChoiceNo,
393 })
394 },
395 choice: commondao.ChoiceYes,
396 success: true,
397 },
398 {
399 name: "no plurality",
400 setup: func(r *commondao.VotingRecord) {
401 r.AddVote(commondao.Vote{
402 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
403 Choice: commondao.ChoiceYes,
404 })
405 r.AddVote(commondao.Vote{
406 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
407 Choice: commondao.ChoiceNo,
408 })
409 },
410 choice: "",
411 success: false,
412 },
413 {
414 name: "plurality with abstain vote",
415 setup: func(r *commondao.VotingRecord) {
416 r.AddVote(commondao.Vote{
417 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
418 Choice: commondao.ChoiceYes,
419 })
420 r.AddVote(commondao.Vote{
421 Address: "g12chzmwxw8sezcxe9h2csp0tck76r4ptwdlyyqk",
422 Choice: commondao.ChoiceYes,
423 })
424 r.AddVote(commondao.Vote{
425 Address: "g1vh7krmmzfua5xjmkatvmx09z37w34lsvd2mxa5",
426 Choice: commondao.ChoiceNo,
427 })
428 r.AddVote(commondao.Vote{
429 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
430 Choice: commondao.ChoiceAbstain,
431 })
432 },
433 choice: commondao.ChoiceYes,
434 success: true,
435 },
436 }
437
438 for _, tc := range cases {
439 t.Run(tc.name, func(t *testing.T) {
440 var record commondao.VotingRecord
441
442 if tc.setup != nil {
443 tc.setup(&record)
444 }
445
446 choice, success := commondao.SelectChoiceByPlurality(record.Readonly())
447
448 uassert.Equal(t, string(tc.choice), string(choice), "choice")
449 uassert.Equal(t, tc.success, success, "success")
450 })
451 }
452}