proposal_subdao_test.gno
7.03 Kb · 284 lines
1package commondao
2
3import (
4 "testing"
5
6 "gno.land/p/moul/addrset"
7 "gno.land/p/nt/commondao/v0"
8 "gno.land/p/nt/uassert/v0"
9 "gno.land/p/nt/urequire/v0"
10)
11
12var _ commondao.ProposalDefinition = (*subDAOPropDefinition)(nil)
13
14func TestSubDAOPropDefinitionTally(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 Address: "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
40 Choice: commondao.ChoiceYes,
41 },
42 },
43 success: true,
44 },
45 {
46 name: "fail",
47 members: []address{
48 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
49 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
50 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
51 },
52 votes: []commondao.Vote{
53 {
54 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
55 Choice: commondao.ChoiceNo,
56 },
57 {
58 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
59 Choice: commondao.ChoiceNo,
60 },
61 {
62 Address: "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
63 Choice: commondao.ChoiceNo,
64 },
65 },
66 },
67 {
68 name: "fail with no votes",
69 members: []address{
70 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
71 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
72 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
73 },
74 err: commondao.ErrNoQuorum,
75 },
76 {
77 name: "no quorum",
78 members: []address{
79 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
80 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
81 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
82 },
83 votes: []commondao.Vote{
84 {
85 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
86 Choice: commondao.ChoiceYes,
87 },
88 },
89 err: commondao.ErrNoQuorum,
90 },
91 }
92
93 for _, tc := range cases {
94 t.Run(tc.name, func(t *testing.T) {
95 var (
96 p subDAOPropDefinition
97 record commondao.VotingRecord
98 members = commondao.NewMemberStorage()
99 )
100
101 for _, m := range tc.members {
102 members.Add(m)
103 }
104
105 for _, v := range tc.votes {
106 record.AddVote(v)
107 }
108
109 ctx := commondao.MustNewVotingContext(&record, members)
110
111 success, err := p.Tally(ctx)
112
113 if tc.err != nil {
114 urequire.ErrorIs(t, err, tc.err, "expect an error")
115 uassert.False(t, success, "expect tally to fail")
116 return
117 }
118
119 urequire.NoError(t, err, "expect no error")
120 uassert.Equal(t, tc.success, success, "expect tally success to match")
121 })
122 }
123}
124
125func TestSubDAOPropDefinitionExecute(t *testing.T) {
126 // Create a parent DAO
127 parentDAO := createDAO("Foo", "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj", nil)
128 urequire.Equal(t, 0, parentDAO.Children().Len(), "expected no initial SubDAOs")
129
130 // Initialize some members for the SubDAO
131 var members addrset.Set
132 members.Add("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
133 members.Add("g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq")
134
135 // Create the proposal and execute it
136 name := "Bar"
137 p := newSubDAOPropDefinition(parentDAO, name, &members)
138 fn := p.Executor()
139 err := fn(cross)
140 urequire.NoError(t, err, "expected no execution error")
141
142 // Expect SubDAO to be created
143 urequire.Equal(t, 1, parentDAO.Children().Len(), "expected SubDAO to be created")
144 subDAO := parentDAO.Children().Get(0).(*commondao.CommonDAO)
145
146 // Make sure SubDAO is indexed properly
147 subDAO = getDAO(subDAO.ID())
148 urequire.True(t, subDAO != nil, "expected SubDAO to be indexed by the realm")
149
150 // Make sure SubDAO values are the right ones
151 uassert.Equal(t, name, subDAO.Name(), "expected SubDAO name to match")
152 uassert.Equal(t, 2, subDAO.Members().Size(), "expected SubDAO to have two members")
153
154 var i int
155 subDAO.Members().IterateByOffset(0, subDAO.Members().Size(), func(addr address) bool {
156 s, _ := members.Tree().GetByIndex(i)
157 urequire.Equal(t, address(s), addr, "expected member to be found")
158 i++
159 return false
160 })
161}
162
163func TestDissolvePropDefinitionTally(t *testing.T) {
164 cases := []struct {
165 name string
166 members []address
167 votes []commondao.Vote
168 err error
169 success bool
170 }{
171 {
172 name: "succeed",
173 members: []address{
174 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
175 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
176 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
177 },
178 votes: []commondao.Vote{
179 {
180 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
181 Choice: commondao.ChoiceYes,
182 },
183 {
184 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
185 Choice: commondao.ChoiceYes,
186 },
187 {
188 Address: "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
189 Choice: commondao.ChoiceYes,
190 },
191 },
192 success: true,
193 },
194 {
195 name: "fail",
196 members: []address{
197 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
198 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
199 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
200 },
201 votes: []commondao.Vote{
202 {
203 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
204 Choice: commondao.ChoiceNo,
205 },
206 {
207 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
208 Choice: commondao.ChoiceNo,
209 },
210 {
211 Address: "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
212 Choice: commondao.ChoiceNo,
213 },
214 },
215 },
216 {
217 name: "fail with no votes",
218 members: []address{
219 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
220 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
221 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
222 },
223 err: commondao.ErrNoQuorum,
224 },
225 {
226 name: "no quorum",
227 members: []address{
228 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
229 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
230 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
231 },
232 votes: []commondao.Vote{
233 {
234 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
235 Choice: commondao.ChoiceYes,
236 },
237 },
238 err: commondao.ErrNoQuorum,
239 },
240 }
241
242 for _, tc := range cases {
243 t.Run(tc.name, func(t *testing.T) {
244 var (
245 p dissolvePropDefinition
246 record commondao.VotingRecord
247 members = commondao.NewMemberStorage()
248 )
249
250 for _, m := range tc.members {
251 members.Add(m)
252 }
253
254 for _, v := range tc.votes {
255 record.AddVote(v)
256 }
257
258 ctx := commondao.MustNewVotingContext(&record, members)
259
260 success, err := p.Tally(ctx)
261
262 if tc.err != nil {
263 urequire.ErrorIs(t, err, tc.err, "expect an error")
264 uassert.False(t, success, "expect tally to fail")
265 return
266 }
267
268 urequire.NoError(t, err, "expect no error")
269 uassert.Equal(t, tc.success, success, "expect tally success to match")
270 })
271 }
272}
273
274func TestDissolvePropDefinitionExecute(t *testing.T) {
275 dao := commondao.New()
276 urequire.False(t, dao.IsDeleted(), "expected DAO not to be soft deleted by default")
277
278 p := newDissolvePropDefinition(dao)
279 fn := p.Executor()
280 err := fn(cross)
281 urequire.NoError(t, err, "expected no execution error")
282
283 uassert.True(t, dao.IsDeleted(), "expected DAO to be soft deleted")
284}