Search Apps Documentation Source Content File Folder Download Copy Actions Download

proposal_storage_test.gno

1.97 Kb ยท 76 lines
 1package commondao_test
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert"
 7	"gno.land/p/nt/urequire"
 8
 9	"gno.land/p/devx000/wip/nt/commondao"
10)
11
12func TestProposalStorageAdd(t *testing.T) {
13	p, _ := commondao.NewProposal(1, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", testPropDef{})
14	s := commondao.NewProposalStorage()
15	initialSize := s.Size()
16
17	s.Add(p)
18
19	uassert.Equal(t, 0, initialSize, "expect initial storage to be empty")
20	uassert.Equal(t, 1, s.Size(), "expect storage to have one proposal")
21	uassert.True(t, s.Has(p.ID()), "expect proposal to be found")
22}
23
24func TestProposalStorageGet(t *testing.T) {
25	p, _ := commondao.NewProposal(1, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", testPropDef{})
26	s := commondao.NewProposalStorage()
27	s.Add(p)
28
29	p2 := s.Get(p.ID())
30
31	urequire.NotEqual(t, nil, p2, "expect proposal to be found")
32	uassert.Equal(t, p.ID(), p2.ID(), "expect proposal ID to match")
33}
34
35func TestProposalStorageRemove(t *testing.T) {
36	p, _ := commondao.NewProposal(1, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", testPropDef{})
37	s := commondao.NewProposalStorage()
38	s.Add(p)
39
40	s.Remove(p.ID())
41
42	uassert.Equal(t, 0, s.Size(), "expect storage to be empty")
43	uassert.False(t, s.Has(p.ID()), "expect proposal to be not found")
44}
45
46func TestProposalStorageIterate(t *testing.T) {
47	var (
48		i   int
49		ids = []uint64{22, 33, 44}
50		s   = commondao.NewProposalStorage()
51	)
52
53	for _, id := range ids {
54		p, _ := commondao.NewProposal(id, "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", testPropDef{})
55		s.Add(p)
56	}
57
58	s.Iterate(0, s.Size(), false, func(p *commondao.Proposal) bool {
59		uassert.Equal(t, ids[i], p.ID(), "expect proposal ID to match")
60
61		i++
62		return i == s.Size()
63	})
64
65	uassert.Equal(t, len(ids), i, "expect storage to iterate all proposals")
66
67	i = s.Size() - 1
68	s.Iterate(0, s.Size(), true, func(p *commondao.Proposal) bool {
69		uassert.Equal(t, ids[i], p.ID(), "expect proposal ID to match")
70
71		i--
72		return i == -1
73	})
74
75	uassert.Equal(t, -1, i, "expect storage to iterate all proposals in reverse order")
76}