Skip to content

Commit

Permalink
improve testing computing exact yes percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Feb 1, 2025
1 parent 7bdb4ca commit 3176f51
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions gno/p/daocond/cond_govdao.gno
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (m *govDaoCondThresholdState) HandleEvent(_ Event, _ map[string]Vote) {
}
func (m *govDaoCondThresholdState) RenderJSON(votes map[string]Vote) *json.Node {
return json.ObjectNode("", map[string]*json.Node{
"type": json.StringNode("", "members-threshold"),
"yesRatio": json.NumberNode("", float64(m.yesRatio(votes))),
"type": json.StringNode("", "govdao-threshold"),
"totalYes": json.NumberNode("", m.yesRatio(votes)),
})
}

Expand Down
18 changes: 15 additions & 3 deletions gno/p/daocond/cond_govdao_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -112,39 +112,44 @@ func TestEval(t *testing.T){
votesT3 []Vote
threshold float64
expectedEval bool
expectedYes float64
}
tests:= map[string]govDaoVotes{
"40% Yes": {
"2/6% Yes": { //0.3333
votesT1: []Vote{VoteNo}, // 3 voting power
votesT2: []Vote{VoteYes,VoteYes,VoteYes,VoteYes}, // 2 voting power combined
votesT3: []Vote{VoteNo},
expectedEval: false,
threshold:0.45,
expectedYes: 2.0/6.0,
},
"50% Yes": {
votesT1: []Vote{VoteNo}, // 3 voting power
votesT2: []Vote{VoteYes,VoteYes,VoteYes,VoteYes}, // 2 voting power combined
votesT3: []Vote{VoteYes},
expectedEval: true,
threshold:0.45,
expectedYes: 3.0/6.0,
},
"several T2 & T3": {
votesT1: []Vote{VoteNo,VoteNo}, // 6 voting power
// 10 T2 total power (2/3) powerT1 = 4, 4/10 0.4 each
votesT2: []Vote{VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes},
// 4 T3 total power (1/3) powerT1 = 2, 2/5 0.5 each
// 4 T3 total power (1/3) powerT1 = 2, 2/4 0.5 each
votesT3: []Vote{VoteYes,VoteNo,VoteYes,VoteNo},
expectedEval: false,
threshold:0.42, //total power = 6+4+2 T3yes = 1, T2yes = 4 T1yes = 0 totalYes = 0.41666666666 (5/12)
expectedYes: 5.0/12.0,
},
"several T2 & T3 eval true": {
votesT1: []Vote{VoteNo,VoteNo}, // 6 voting power
// 10 T2 total power (2/3) powerT1 = 4, 4/10 0.4 each
votesT2: []Vote{VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes,VoteYes},
// 4 T3 total power (1/3) powerT1 = 2, 2/5 0.5 each
// 4 T3 total power (1/3) powerT1 = 2, 2/4 0.5 each
votesT3: []Vote{VoteYes,VoteYes,VoteYes,VoteNo},
expectedEval: true,
threshold:0.42, //total power = 6+4+2 T3yes = 1.5, T2yes = 4 T1yes = 0 totalYes = 0.45833333333 (5.5/12)
expectedYes: 5.5/12.0,
},
}

Expand Down Expand Up @@ -178,7 +183,14 @@ func TestEval(t *testing.T){
cond := GovDaoCondThreshold(tdata.threshold, dao.hasRole, dao.usersWithRoleCount)
state := cond.NewState()

// Get percent of total yes
renderData:=state.RenderJSON(votes)
totalYesNode,err:=renderData.GetKey("totalYes")
urequire.NoError(t, err)

urequire.Equal(t, tdata.expectedEval, state.Eval(votes))
// require totalYes to be equal to expected
urequire.Equal(t, tdata.expectedYes, totalYesNode.MustNumeric())
})
}
}
Expand Down

0 comments on commit 3176f51

Please sign in to comment.