/
model_helper_test.go
76 lines (68 loc) · 2.02 KB
/
model_helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gov
import (
"reflect"
"testing"
"github.com/iov-one/weave/errors"
"github.com/iov-one/weave/weavetest"
)
func TestMerger(t *testing.T) {
alice := weavetest.NewCondition().Address()
bobby := weavetest.NewCondition().Address()
charlie := weavetest.NewCondition().Address()
specs := map[string]struct {
diff []Elector
expValidateErr *errors.Error
expElectors []Elector
expTotalWeight uint64
}{
"Add elector": {
diff: []Elector{{Address: charlie, Weight: 2}},
expElectors: []Elector{{alice, 1}, {bobby, 10}, {Address: charlie, Weight: 2}},
expTotalWeight: 13,
},
"Remove elector": {
diff: []Elector{{Address: bobby, Weight: 0}},
expElectors: []Elector{{alice, 1}},
expTotalWeight: 1,
},
"Update elector": {
diff: []Elector{{Address: alice, Weight: 2}},
expElectors: []Elector{{alice, 2}, {bobby, 10}},
expTotalWeight: 12,
},
"Reject duplicates in diff": {
diff: []Elector{{Address: alice, Weight: 2}, {Address: alice, Weight: 3}},
expValidateErr: errors.ErrDuplicate,
},
"Reject existing values": {
diff: []Elector{{Address: alice, Weight: 1}},
expValidateErr: errors.ErrDuplicate,
},
"Reject no existing values": {
diff: []Elector{{Address: charlie, Weight: 0}},
expValidateErr: errors.ErrNotFound,
},
}
source := []Elector{{alice, 1}, {bobby, 10}}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
m := newMerger(source)
// when merge
if err := m.merge(spec.diff); !spec.expValidateErr.Is(err) {
t.Fatalf("unexpected error: %+v", err)
}
if spec.expValidateErr != nil {
return
}
// then
gotElectors, gotTotalWeight := m.serialize()
sortByAddress(spec.expElectors)
if exp, got := spec.expElectors, gotElectors; !reflect.DeepEqual(got, exp) {
t.Errorf("expected %v but got %v", exp, got)
}
if exp, got := spec.expTotalWeight, gotTotalWeight; exp != got {
t.Errorf("expected %v but got %v", exp, got)
}
})
}
}