Search Apps Documentation Source Content File Folder Download Copy Actions Download

definition_test.gno

3.83 Kb · 170 lines
  1package definition_test
  2
  3import (
  4	"errors"
  5	"strings"
  6	"testing"
  7	"time"
  8
  9	"gno.land/p/nt/commondao/v0"
 10	"gno.land/p/nt/commondao/v0/exts/definition"
 11	"gno.land/p/nt/uassert/v0"
 12)
 13
 14func TestNew(t *testing.T) {
 15	tests := []struct {
 16		name  string
 17		title string
 18		body  string
 19		err   error
 20	}{
 21		{
 22			name:  "ok",
 23			title: "Proposal Title",
 24			body:  "Foo",
 25		},
 26		{
 27			name:  "empty title",
 28			title: "",
 29			err:   definition.ErrTitleIsRequired,
 30		},
 31		{
 32			name:  "empty title",
 33			title: "Proposal Title",
 34			body:  "",
 35			err:   definition.ErrBodyIsRequired,
 36		},
 37	}
 38
 39	for _, tt := range tests {
 40		t.Run(tt.name, func(t *testing.T) {
 41			// Act
 42			def, err := definition.New(tt.title, tt.body)
 43
 44			// Assert
 45			if tt.err != nil {
 46				uassert.ErrorIs(t, err, tt.err, "expect an error")
 47				return
 48			}
 49
 50			uassert.NoError(t, err, "expect no errors")
 51			uassert.Equal(t, tt.title, def.Title(), "expect title to match")
 52			uassert.Equal(t, tt.body, def.Body(), "expect body to match")
 53		})
 54	}
 55}
 56
 57func TestDefinitionCustomVoteChoices(t *testing.T) {
 58	// Default voting choices
 59	def := definition.MustNew("Title", "Body")
 60	uassert.Equal(t, "YES,NO,ABSTAIN", joinVoteChoices(def.CustomVoteChoices()), "expect default vote choces to match")
 61
 62	// Custom voting choices
 63	def = definition.MustNew("Title", "Body", definition.WithVoteChoices([]commondao.VoteChoice{"A", "B", "C"}))
 64	uassert.Equal(t, "A,B,C", joinVoteChoices(def.CustomVoteChoices()), "expect custom vote choces to match")
 65}
 66
 67func TestDefinitionVotingPeriod(t *testing.T) {
 68	// Default voting period
 69	def := definition.MustNew("Title", "Body")
 70	uassert.True(t, definition.DefaultVotingPeriod == def.VotingPeriod(), "expect default voting period to match")
 71
 72	// Custom voting period
 73	period := time.Hour * 24 * 30
 74	def = definition.MustNew("Title", "Body", definition.WithVotingPeriod(period))
 75	uassert.True(t, period == def.VotingPeriod(), "expect custom voting period to match")
 76
 77	// Zero voting period
 78	period = 0
 79	def = definition.MustNew("Title", "Body", definition.WithVotingPeriod(period))
 80	uassert.True(t, period == def.VotingPeriod(), "expect zero voting period to match")
 81}
 82
 83func TestDefinitionTally(t *testing.T) {
 84	tests := []struct {
 85		name   string
 86		passes bool
 87		err    error
 88	}{
 89		{
 90			name:   "proposal passes",
 91			passes: true,
 92		},
 93		{
 94			name:   "proposal fails",
 95			passes: false,
 96		},
 97		{
 98			name: "tally error",
 99			err:  errors.New("no quorum"),
100		},
101	}
102
103	for _, tt := range tests {
104		t.Run(tt.name, func(t *testing.T) {
105			// Arrange
106			fn := func(commondao.VotingContext) (bool, error) {
107				return tt.passes, tt.err
108			}
109
110			def := definition.MustNew("Title", "Body", definition.WithTally(fn))
111
112			// Act
113			passes, err := def.Tally(commondao.VotingContext{})
114
115			// Assert
116			if tt.err != nil {
117				uassert.ErrorIs(t, err, tt.err, "expect tally to fail")
118				return
119			}
120
121			uassert.NoError(t, err, "expect no errors")
122			uassert.Equal(t, tt.passes, passes, "expect tally result to match")
123		})
124	}
125}
126
127func TestDefinitionValidation(t *testing.T) {
128	errTest := errors.New("test error")
129	tests := []struct {
130		name      string
131		validator func() error
132		err       error
133	}{
134		{
135			name:      "ok",
136			validator: func() error { return nil },
137		},
138		{
139			name:      "validation error",
140			validator: func() error { return errTest },
141			err:       errTest,
142		},
143	}
144
145	for _, tt := range tests {
146		t.Run(tt.name, func(t *testing.T) {
147			// Arrange
148			def := definition.MustNew("Title", "Body", definition.WithValidation(tt.validator))
149
150			// Act
151			err := def.Validate()
152
153			// Assert
154			if tt.err != nil {
155				uassert.ErrorIs(t, err, tt.err, "expect validation to fail")
156				return
157			}
158
159			uassert.NoError(t, err, "expect no errors")
160		})
161	}
162}
163
164func joinVoteChoices(choices []commondao.VoteChoice) string {
165	values := make([]string, len(choices))
166	for i, c := range choices {
167		values[i] = string(c)
168	}
169	return strings.Join(values, ",")
170}