proposal_storage_test.gno

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