Search Apps Documentation Source Content File Folder Download Copy Actions Download

proposal_text_test.gno

4.06 Kb · 178 lines
  1package commondao
  2
  3import (
  4	"strings"
  5	"testing"
  6	"time"
  7
  8	"gno.land/p/nt/commondao/v0"
  9	"gno.land/p/nt/uassert/v0"
 10	"gno.land/p/nt/urequire/v0"
 11)
 12
 13var _ commondao.ProposalDefinition = (*TextPropDefinition)(nil)
 14
 15func TestTextPropDefinitionNew(t *testing.T) {
 16	cases := []struct {
 17		name         string
 18		title, body  string
 19		quorum       float64
 20		votingPeriod time.Duration
 21		panicMsg     string
 22	}{
 23		{
 24			name:         "ok",
 25			title:        "Test",
 26			body:         "Foo",
 27			votingPeriod: time.Hour * 24 * 7,
 28			quorum:       commondao.QuorumOneThird,
 29		},
 30		{
 31			name:     "empty proposal title",
 32			title:    "",
 33			panicMsg: "proposal title is empty",
 34		},
 35		{
 36			name:     "long proposal title",
 37			title:    strings.Repeat("A", 256),
 38			panicMsg: "proposal title is too long, max length is 255 chars",
 39		},
 40		{
 41			name:     "empty proposal body",
 42			title:    "Test",
 43			body:     "",
 44			panicMsg: "proposal body is empty",
 45		},
 46		{
 47			name:     "long proposal body",
 48			title:    "Test",
 49			body:     strings.Repeat("A", 15001),
 50			panicMsg: "proposal body is too long, max length is 15000 chars",
 51		},
 52		{
 53			name:         "invalid voting period",
 54			title:        "Test",
 55			body:         "Foo",
 56			votingPeriod: time.Hour * 10,
 57			panicMsg:     "minimum proposal voting period is one day",
 58		},
 59		{
 60			name:         "invalid quorum",
 61			title:        "Test",
 62			body:         "Foo",
 63			votingPeriod: time.Hour * 24 * 7,
 64			quorum:       0.32,
 65			panicMsg:     "minimum quorum allowed is one third (0.33)",
 66		},
 67	}
 68
 69	for _, tc := range cases {
 70		t.Run(tc.name, func(t *testing.T) {
 71			if tc.panicMsg != "" {
 72				urequire.PanicsWithMessage(t, tc.panicMsg, func() {
 73					NewTextPropDefinition(tc.title, tc.body, tc.quorum, tc.votingPeriod)
 74				}, "expected definition to panic with message")
 75
 76				return
 77			}
 78
 79			urequire.NotPanics(t, func() {
 80				NewTextPropDefinition(tc.title, tc.body, tc.quorum, tc.votingPeriod)
 81			}, "expected definition to be created")
 82		})
 83	}
 84}
 85
 86func TestTextPropDefinitionTally(t *testing.T) {
 87	cases := []struct {
 88		name    string
 89		members []address
 90		votes   []commondao.Vote
 91		err     error
 92		success bool
 93	}{
 94		{
 95			name: "succeed",
 96			members: []address{
 97				"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
 98				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
 99				"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
100			},
101			votes: []commondao.Vote{
102				{
103					Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
104					Choice:  commondao.ChoiceYes,
105				},
106				{
107					Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
108					Choice:  commondao.ChoiceYes,
109				},
110			},
111			success: true,
112		},
113		{
114			name: "fail",
115			members: []address{
116				"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
117				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
118				"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
119			},
120			votes: []commondao.Vote{
121				{
122					Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
123					Choice:  commondao.ChoiceNo,
124				},
125				{
126					Address: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
127					Choice:  commondao.ChoiceNo,
128				},
129			},
130		},
131		{
132			name: "no quorum",
133			members: []address{
134				"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
135				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
136				"g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq",
137			},
138			votes: []commondao.Vote{
139				{
140					Address: "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj",
141					Choice:  commondao.ChoiceYes,
142				},
143			},
144			err: commondao.ErrNoQuorum,
145		},
146	}
147
148	for _, tc := range cases {
149		t.Run(tc.name, func(t *testing.T) {
150			var (
151				p       = TextPropDefinition{quorum: commondao.QuorumTwoThirds}
152				record  commondao.VotingRecord
153				members = commondao.NewMemberStorage()
154			)
155
156			for _, m := range tc.members {
157				members.Add(m)
158			}
159
160			for _, v := range tc.votes {
161				record.AddVote(v)
162			}
163
164			ctx := commondao.MustNewVotingContext(&record, members)
165
166			success, err := p.Tally(ctx)
167
168			if tc.err != nil {
169				urequire.ErrorIs(t, err, tc.err, "expect an error")
170				uassert.False(t, success, "expect tally to fail")
171				return
172			}
173
174			urequire.NoError(t, err, "expect no error")
175			uassert.Equal(t, tc.success, success, "expect tally success to match")
176		})
177	}
178}