proposal_subdao_test.gno
6.97 Kb · 278 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 = (*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 success, err := p.Tally(record.Readonly(), commondao.NewMemberSet(members))
110
111 if tc.err != nil {
112 urequire.ErrorIs(t, err, tc.err, "expect an error")
113 uassert.False(t, success, "expect tally to fail")
114 return
115 }
116
117 urequire.NoError(t, err, "expect no error")
118 uassert.Equal(t, tc.success, success, "expect tally success to match")
119 })
120 }
121}
122
123func TestSubDAOPropDefinitionExecute(t *testing.T) {
124 // Create a parent DAO
125 parentDAO := createDAO("Foo", "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj", nil)
126 urequire.Equal(t, 0, parentDAO.Children().Len(), "expected no initial SubDAOs")
127
128 // Initialize some members for the SubDAO
129 var members addrset.Set
130 members.Add("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
131 members.Add("g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq")
132
133 // Create the proposal and execute it
134 name := "Bar"
135 p := newSubDAOPropDefinition(parentDAO, name, &members)
136 err := p.Execute(cross)
137 urequire.NoError(t, err, "expected no execution error")
138
139 // Expect SubDAO to be created
140 urequire.Equal(t, 1, parentDAO.Children().Len(), "expected SubDAO to be created")
141 subDAO := parentDAO.Children().Get(0).(*commondao.CommonDAO)
142
143 // Make sure SubDAO is indexed properly
144 subDAO = getDAO(subDAO.ID())
145 urequire.True(t, subDAO != nil, "expected SubDAO to be indexed by the realm")
146
147 // Make sure SubDAO values are the right ones
148 uassert.Equal(t, name, subDAO.Name(), "expected SubDAO name to match")
149 uassert.Equal(t, 2, subDAO.Members().Size(), "expected SubDAO to have two members")
150
151 var i int
152 subDAO.Members().IterateByOffset(0, subDAO.Members().Size(), func(addr address) bool {
153 s, _ := members.Tree().GetByIndex(i)
154 urequire.Equal(t, address(s), addr, "expected member to be found")
155 i++
156 return false
157 })
158}
159
160func TestDissolvePropDefinitionTally(t *testing.T) {
161 cases := []struct {
162 name string
163 members []address
164 votes []commondao.Vote
165 err error
166 success bool
167 }{
168 {
169 name: "succeed",
170 members: []address{
171 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
172 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
173 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
174 },
175 votes: []commondao.Vote{
176 {
177 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
178 Choice: commondao.ChoiceYes,
179 },
180 {
181 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
182 Choice: commondao.ChoiceYes,
183 },
184 {
185 Address: "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
186 Choice: commondao.ChoiceYes,
187 },
188 },
189 success: true,
190 },
191 {
192 name: "fail",
193 members: []address{
194 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
195 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
196 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
197 },
198 votes: []commondao.Vote{
199 {
200 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
201 Choice: commondao.ChoiceNo,
202 },
203 {
204 Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
205 Choice: commondao.ChoiceNo,
206 },
207 {
208 Address: "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
209 Choice: commondao.ChoiceNo,
210 },
211 },
212 },
213 {
214 name: "fail with no votes",
215 members: []address{
216 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
217 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
218 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
219 },
220 err: commondao.ErrNoQuorum,
221 },
222 {
223 name: "no quorum",
224 members: []address{
225 "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
226 "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
227 "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
228 },
229 votes: []commondao.Vote{
230 {
231 Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
232 Choice: commondao.ChoiceYes,
233 },
234 },
235 err: commondao.ErrNoQuorum,
236 },
237 }
238
239 for _, tc := range cases {
240 t.Run(tc.name, func(t *testing.T) {
241 var (
242 p dissolvePropDefinition
243 record commondao.VotingRecord
244 members = commondao.NewMemberStorage()
245 )
246
247 for _, m := range tc.members {
248 members.Add(m)
249 }
250
251 for _, v := range tc.votes {
252 record.AddVote(v)
253 }
254
255 success, err := p.Tally(record.Readonly(), commondao.NewMemberSet(members))
256
257 if tc.err != nil {
258 urequire.ErrorIs(t, err, tc.err, "expect an error")
259 uassert.False(t, success, "expect tally to fail")
260 return
261 }
262
263 urequire.NoError(t, err, "expect no error")
264 uassert.Equal(t, tc.success, success, "expect tally success to match")
265 })
266 }
267}
268
269func TestDissolvePropDefinitionExecute(t *testing.T) {
270 dao := commondao.New()
271 urequire.False(t, dao.IsDeleted(), "expected DAO not to be soft deleted by default")
272
273 p := newDissolvePropDefinition(dao)
274 err := p.Execute(cross)
275 urequire.NoError(t, err, "expected no execution error")
276
277 uassert.True(t, dao.IsDeleted(), "expected DAO to be soft deleted")
278}