proposal_members_test.gno
5.86 Kb · 232 lines
1package commondao
2
3import (
4 "testing"
5
6 "gno.land/p/moul/addrset"
7 "gno.land/p/nt/commondao"
8 "gno.land/p/nt/uassert"
9 "gno.land/p/nt/urequire"
10)
11
12var _ commondao.ProposalDefinition = (*membersUpdatePropDefinition)(nil)
13
14func TestMembersPropDefinitionTally(t *testing.T) {
15 cases := []struct {
16 name string
17 members []address
18 votes []commondao.Vote
19 err error
20 success bool
21 }{
22 {
23 name: "succeed",
24 members: []address{
25 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
26 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
27 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
28 },
29 votes: []commondao.Vote{
30 {
31 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
32 Choice: commondao.ChoiceYes,
33 },
34 {
35 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
36 Choice: commondao.ChoiceYes,
37 },
38 },
39 success: true,
40 },
41 {
42 name: "fail",
43 members: []address{
44 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
45 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
46 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
47 },
48 votes: []commondao.Vote{
49 {
50 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
51 Choice: commondao.ChoiceNo,
52 },
53 {
54 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
55 Choice: commondao.ChoiceNo,
56 },
57 },
58 },
59 {
60 name: "no quorum",
61 members: []address{
62 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
63 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
64 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
65 },
66 votes: []commondao.Vote{
67 {
68 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
69 Choice: commondao.ChoiceYes,
70 },
71 },
72 err: commondao.ErrNoQuorum,
73 },
74 {
75 name: "succeed with two members",
76 members: []address{
77 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
78 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
79 },
80 votes: []commondao.Vote{
81 {
82 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
83 Choice: commondao.ChoiceYes,
84 },
85 },
86 success: true,
87 },
88 {
89 name: "fail with two members",
90 members: []address{
91 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
92 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
93 },
94 },
95 }
96
97 for _, tc := range cases {
98 t.Run(tc.name, func(t *testing.T) {
99 var (
100 p membersUpdatePropDefinition
101 record commondao.VotingRecord
102 members = commondao.NewMemberStorage()
103 )
104
105 for _, m := range tc.members {
106 members.Add(m)
107 }
108
109 for _, v := range tc.votes {
110 record.AddVote(v)
111 }
112
113 success, err := p.Tally(record.Readonly(), commondao.NewMemberSet(members))
114
115 if tc.err != nil {
116 urequire.ErrorIs(t, err, tc.err, "expect an error")
117 uassert.False(t, success, "expect tally to fail")
118 return
119 }
120
121 urequire.NoError(t, err, "expect no error")
122 uassert.Equal(t, tc.success, success, "expect tally success to match")
123 })
124 }
125}
126
127func TestMembersPropDefinitionExecute(t *testing.T) {
128 cases := []struct {
129 name string
130 dao *commondao.CommonDAO
131 members, add, remove []address
132 errMsg string
133 }{
134 {
135 name: "add member",
136 dao: commondao.New(
137 commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
138 commondao.WithMember("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
139 ),
140 add: []address{"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"},
141 members: []address{
142 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
143 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
144 "g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae",
145 },
146 },
147 {
148 name: "add multiple members",
149 dao: commondao.New(
150 commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
151 ),
152 add: []address{
153 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
154 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
155 },
156 members: []address{
157 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
158 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
159 "g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae",
160 },
161 },
162 {
163 name: "remove member",
164 dao: commondao.New(
165 commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
166 commondao.WithMember("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
167 ),
168 remove: []address{"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"},
169 members: []address{"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"},
170 },
171 {
172 name: "remove multiple members",
173 dao: commondao.New(
174 commondao.WithMember("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
175 commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
176 commondao.WithMember("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
177 ),
178 remove: []address{
179 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
180 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
181 },
182 members: []address{"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"},
183 },
184 {
185 name: "add and remove members",
186 dao: commondao.New(
187 commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
188 commondao.WithMember("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
189 ),
190 add: []address{"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"},
191 remove: []address{"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"},
192 members: []address{
193 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
194 "g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae",
195 },
196 },
197 }
198
199 for _, tc := range cases {
200 t.Run(tc.name, func(t *testing.T) {
201 var add, remove addrset.Set
202
203 for _, addr := range tc.add {
204 add.Add(addr)
205 }
206
207 for _, addr := range tc.remove {
208 remove.Add(addr)
209 }
210
211 p := newMembersUpdatePropDefinition(tc.dao, add, remove)
212
213 err := p.Execute(cross)
214
215 if tc.errMsg != "" {
216 uassert.ErrorContains(t, err, tc.errMsg, "expect error")
217 return
218 }
219
220 urequire.NoError(t, err, "expect no error")
221 urequire.Equal(t, len(tc.members), tc.dao.Members().Size(), "number of members must match")
222
223 var i int
224 tc.dao.Members().IterateByOffset(0, tc.dao.Members().Size(), func(addr address) bool {
225 urequire.Equal(t, tc.members[i], addr, "member address must match")
226
227 i++
228 return false
229 })
230 })
231 }
232}