Skip to content

Commit

Permalink
test(gno/dao): add unit test for init dao
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelVallenet committed Jan 15, 2025
1 parent d221981 commit 4f7fab2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 125 deletions.
16 changes: 8 additions & 8 deletions gno/p/dao_roles_based/members.gno
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ func (m *MemberModule) MembersCount() uint64 {
return uint64(m.members.Size())
}

func (m *MemberModule) GetRoles() []string {
return m.roleManager.GetRoles()
}

func (m *MemberModule) GetUserRoles(memberId string) []string {
return m.roleManager.GetUserRoles(std.Address(memberId))
}

func (m *MemberModule) setRoles(roles []string) {
caller := std.CurrentRealm().Addr()
for _, role := range roles {
m.roleManager.CreateNewRole(role, []string{}, caller)
}
}

func (m *MemberModule) getRoles() []string {
return m.roleManager.GetRoles()
}

func (m *MemberModule) getUserRoles(memberId string) []string {
return m.roleManager.GetUserRoles(std.Address(memberId))
}

func (m *MemberModule) setMembers(members [][]string) {
caller := std.CurrentRealm().Addr()
for _, member := range members {
Expand Down
4 changes: 2 additions & 2 deletions gno/p/dao_roles_based/render.gno
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (d *DaoRolesBased) renderHomePage(res *mux.ResponseWriter, req *mux.Request

func (d *DaoRolesBased) renderConfigPage(res *mux.ResponseWriter, req *mux.Request) {
res.Write(ufmt.Sprintf("# %s - Config ⚙️\n\n", d.Name))
roles := d.MemberModule.getRoles()
roles := d.MemberModule.GetRoles()
res.Write(ufmt.Sprintf("## Roles 🏷️\n\n"))
for _, role := range roles {
res.Write(ufmt.Sprintf("- %s\n\n", role))
Expand Down Expand Up @@ -108,7 +108,7 @@ func (d *DaoRolesBased) renderProposalHistoryPage(res *mux.ResponseWriter, req *

func (d *DaoRolesBased) renderMemberDetailPage(res *mux.ResponseWriter, req *mux.Request) {
res.Write(ufmt.Sprintf("# %s - Member Detail - %s\n\n", d.Name, req.GetVar("address")))
roles := d.MemberModule.getUserRoles(req.GetVar("address"))
roles := d.MemberModule.GetUserRoles(req.GetVar("address"))
res.Write(ufmt.Sprintf("## Roles 🏷️\n\n"))
for _, role := range roles {
res.Write(ufmt.Sprintf("- %s\n\n", role))
Expand Down
141 changes: 26 additions & 115 deletions gno/r/dao_realm/dao_realm_test.gno
Original file line number Diff line number Diff line change
@@ -1,117 +1,28 @@
package dao_realm

// import (
// "testing"

// "gno.land/p/demo/json"
// "gno.land/p/demo/ufmt"
// "gno.land/p/teritori/dao_voting_group"
// "gno.land/p/teritori/havl"
// )

// func TestInit(t *testing.T) {
// {
// proposalsJSON := getProposalsJSON(0, 42, "TODO", false)
// expected := `[]`
// if proposalsJSON != expected {
// t.Fatalf("Expected %s, got %s", expected, proposalsJSON)
// }
// }

// {
// members := group.GetMembers("", "", 0, havl.Latest)
// iSlice := make([]*json.Node, len(members))
// for i, v := range members {
// iSlice[i] = v.ToJSON()
// }

// membersJSON := json.ArrayNode("", iSlice).String()
// expected := `[{"address":"g108cszmcvs4r3k67k7h5zuhm4el3qhlrxzhshtv","power":"1"},{"address":"g14u5eaheavy0ux4dmpykg2gvxpvqvexm9cyg58a","power":"1"},{"address":"g1747t5m2f08plqjlrjk2q0qld7465hxz8gkx59c","power":"1"},{"address":"g1ckn395mpttp0vupgtratyufdaakgh8jgkmr3ym","power":"1"},{"address":"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm","power":"1"}]`
// if membersJSON != expected {
// t.Fatalf("Expected:\n%s\nGot:\n%s", expected, membersJSON)
// }
// }
// }

// func TestUpdateMembers(t *testing.T) {
// var membersJSON string

// {
// id := ProposeJSON(0, ufmt.Sprintf(`{"title": "Test prop", "description": "A description", "messages": [{"type": "gno.land/p/teritori/dao_voting_group.UpdateMembers", "payload": [{"power": "2", "address": "g18syxa0vh0vmne90mwhtynjet0zgeqf6prh3ryy"}]}]}`))
// VoteJSON(0, id, `{"vote": "Yes", "rationale": "testing"}`)
// Execute(0, id)
// members := group.GetMembers("", "", 0, havl.Latest)
// iSlice := make([]*json.Node, len(members))
// for i, v := range members {
// iSlice[i] = v.ToJSON()
// }

// membersJSON = json.ArrayNode("", iSlice).String()
// expected := ufmt.Sprintf(`[{"address":"g108cszmcvs4r3k67k7h5zuhm4el3qhlrxzhshtv","power":"1"},{"address":"g14u5eaheavy0ux4dmpykg2gvxpvqvexm9cyg58a","power":"1"},{"address":"g1747t5m2f08plqjlrjk2q0qld7465hxz8gkx59c","power":"1"},{"address":"g18syxa0vh0vmne90mwhtynjet0zgeqf6prh3ryy","power":"2"},{"address":"g1ckn395mpttp0vupgtratyufdaakgh8jgkmr3ym","power":"1"},{"address":"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm","power":"1"}]`)
// if membersJSON != expected {
// t.Errorf("Expected:\n%s\nGot:\n%s", expected, membersJSON)
// }

// totalPower := group.TotalPowerAtHeight(havl.Latest)
// if totalPower != 7 {
// t.Errorf("Expected total power to be 6, got %d", totalPower)
// }
// }

// {
// children := json.Must(json.Unmarshal([]byte(membersJSON))).MustArray()
// if len(children) != 6 {
// t.Errorf("Expected 6 members, got %d", len(children))
// }

// var member dao_voting_group.Member
// member.FromJSON(children[0])

// id := ProposeJSON(0, ufmt.Sprintf(`{"title": "Test prop 2", "description": "A description", "messages": [{"type": "gno.land/p/teritori/dao_voting_group.UpdateMembers", "payload": [{"address": "%s", "power": "0"}]}]}`, member.Address.String()))
// VoteJSON(0, id, `{"vote": "Yes", "rationale": "testing"}`)
// Execute(0, id)
// members := group.GetMembers("", "", 0, havl.Latest)
// iSlice := make([]*json.Node, len(members))
// for i, v := range members {
// iSlice[i] = v.ToJSON()
// }

// membersJSON := json.ArrayNode("", iSlice).String()
// expected := `[{"address":"g14u5eaheavy0ux4dmpykg2gvxpvqvexm9cyg58a","power":"1"},{"address":"g1747t5m2f08plqjlrjk2q0qld7465hxz8gkx59c","power":"1"},{"address":"g18syxa0vh0vmne90mwhtynjet0zgeqf6prh3ryy","power":"2"},{"address":"g1ckn395mpttp0vupgtratyufdaakgh8jgkmr3ym","power":"1"},{"address":"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm","power":"1"}]`
// if membersJSON != expected {
// t.Errorf("Expected:\n%s\nGot:\n%s", expected, membersJSON)
// }

// totalPower := group.TotalPowerAtHeight(havl.Latest)
// if totalPower != 6 {
// t.Errorf("Expected total power to be 6, got %d", totalPower)
// }
// }
// }

// func TestUpdateSettings(t *testing.T) {
// // not sure why but in this test the proposal ids start at 3 and the voting power is 5 when all tests are run, shouldn't tests be isolated? TODO: investigate

// {
// id := ProposeJSON(0, ufmt.Sprintf(`{"title": "Test prop", "description": "A description", "messages": [{"type": "gno.land/p/teritori/dao_proposal_single.UpdateSettings", "payload": {"threshold": {"thresholdQuorum": {"threshold": {"percent": 200}, "quorum": {"percent": 200}}}}}]}`))
// VoteJSON(0, id, `{"vote": "Yes", "rationale": "testing"}`)
// Execute(0, id)
// proposalJSON := getProposalJSON(0, id)
// expected := ufmt.Sprintf(`{"id":"%d","title":"Test prop","description":"A description","proposer":"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm","startHeight":"123","totalPower":"6","messages":[{"type":"gno.land/p/teritori/dao_proposal_single.UpdateSettings","payload":{"threshold":{"thresholdQuorum":{"threshold":{"percent":200},"quorum":{"percent":200}}}}}],"status":"Executed","votes":{"yes":"1","no":"0","abstain":"0"},"allowRevoting":false,"ballots":{"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm":{"power":"1","vote":"Yes","rationale":"testing"}},"expiration":{"atTime":"2009-03-27T23:31:30Z"},"threshold":{"thresholdQuorum":{"threshold":{"percent":100},"quorum":{"percent":100}}}}`, id)
// if proposalJSON != expected {
// t.Fatalf("Expected:\n%s\nGot:\n%s", expected, proposalJSON)
// }
// }

// {
// // make sentiment proposal
// id := ProposeJSON(0, ufmt.Sprintf(`{"title": "Test prop", "description": "A description", "messages": []}`))
// VoteJSON(0, id, `{"vote": "Yes", "rationale": "testing"}`)
// Execute(0, id)
// proposalJSON := getProposalJSON(0, id)
// expected := ufmt.Sprintf(`{"id":"%d","title":"Test prop","description":"A description","proposer":"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm","startHeight":"123","totalPower":"6","messages":[],"status":"Executed","votes":{"yes":"1","no":"0","abstain":"0"},"allowRevoting":false,"ballots":{"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm":{"power":"1","vote":"Yes","rationale":"testing"}},"expiration":{"atTime":"2009-03-27T23:31:30Z"},"threshold":{"thresholdQuorum":{"threshold":{"percent":200},"quorum":{"percent":200}}}}`, id)
// if proposalJSON != expected {
// t.Fatalf("Expected:\n%s\nGot:\n%s", expected, proposalJSON)
// }
// }
// }
import "testing"

func TestInit(t *testing.T) {
membersCount := dao.MembersCount()
if membersCount != 4 {
t.Fatalf("Expected 4 members, got %d", membersCount)
}

roles := dao.MemberModule.GetRoles()
expectedRoles := []string{"admin", "public-relationships", "finance-officer"}
if len(roles) != len(expectedRoles) {
t.Fatalf("Expected %d roles, got %d", len(expectedRoles), len(roles))
}
for i, role := range roles {
err := true
for j, expectedRole := range expectedRoles {
if role == expectedRole {
err = false
break
}
}
if err {
t.Fatalf("Expected roles %v, got %v", expectedRoles, roles)
}
}
}

0 comments on commit 4f7fab2

Please sign in to comment.