Search Apps Documentation Source Content File Folder Download Copy Actions Download

proposal_members_test.gno

5.90 Kb · 235 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 = (*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			ctx := commondao.MustNewVotingContext(&record, members)
114
115			success, err := p.Tally(ctx)
116
117			if tc.err != nil {
118				urequire.ErrorIs(t, err, tc.err, "expect an error")
119				uassert.False(t, success, "expect tally to fail")
120				return
121			}
122
123			urequire.NoError(t, err, "expect no error")
124			uassert.Equal(t, tc.success, success, "expect tally success to match")
125		})
126	}
127}
128
129func TestMembersPropDefinitionExecute(t *testing.T) {
130	cases := []struct {
131		name                 string
132		dao                  *commondao.CommonDAO
133		members, add, remove []address
134		errMsg               string
135	}{
136		{
137			name: "add member",
138			dao: commondao.New(
139				commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
140				commondao.WithMember("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
141			),
142			add: []address{"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"},
143			members: []address{
144				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
145				"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
146				"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae",
147			},
148		},
149		{
150			name: "add multiple members",
151			dao: commondao.New(
152				commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
153			),
154			add: []address{
155				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
156				"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
157			},
158			members: []address{
159				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
160				"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
161				"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae",
162			},
163		},
164		{
165			name: "remove member",
166			dao: commondao.New(
167				commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
168				commondao.WithMember("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
169			),
170			remove:  []address{"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"},
171			members: []address{"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"},
172		},
173		{
174			name: "remove multiple members",
175			dao: commondao.New(
176				commondao.WithMember("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
177				commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
178				commondao.WithMember("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
179			),
180			remove: []address{
181				"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
182				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
183			},
184			members: []address{"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"},
185		},
186		{
187			name: "add and remove members",
188			dao: commondao.New(
189				commondao.WithMember("g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae"),
190				commondao.WithMember("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"),
191			),
192			add:    []address{"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"},
193			remove: []address{"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"},
194			members: []address{
195				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
196				"g1xe2ljac8256rwxxytqddvrjsj2tyv90fvgeaae",
197			},
198		},
199	}
200
201	for _, tc := range cases {
202		t.Run(tc.name, func(t *testing.T) {
203			var add, remove addrset.Set
204
205			for _, addr := range tc.add {
206				add.Add(addr)
207			}
208
209			for _, addr := range tc.remove {
210				remove.Add(addr)
211			}
212
213			p := newMembersUpdatePropDefinition(tc.dao, add, remove)
214			fn := p.Executor()
215
216			err := fn(cross)
217
218			if tc.errMsg != "" {
219				uassert.ErrorContains(t, err, tc.errMsg, "expect error")
220				return
221			}
222
223			urequire.NoError(t, err, "expect no error")
224			urequire.Equal(t, len(tc.members), tc.dao.Members().Size(), "number of members must match")
225
226			var i int
227			tc.dao.Members().IterateByOffset(0, tc.dao.Members().Size(), func(addr address) bool {
228				urequire.Equal(t, tc.members[i], addr, "member address must match")
229
230				i++
231				return false
232			})
233		})
234	}
235}