-
Notifications
You must be signed in to change notification settings - Fork 199
/
chainStorerMock.go
114 lines (99 loc) · 4.01 KB
/
chainStorerMock.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package mock
import (
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/storage"
"github.com/pkg/errors"
)
// ChainStorerMock is a mock implementation of the ChainStorer interface
type ChainStorerMock struct {
AddStorerCalled func(key dataRetriever.UnitType, s storage.Storer)
GetStorerCalled func(unitType dataRetriever.UnitType) storage.Storer
HasCalled func(unitType dataRetriever.UnitType, key []byte) error
GetCalled func(unitType dataRetriever.UnitType, key []byte) ([]byte, error)
PutCalled func(unitType dataRetriever.UnitType, key []byte, value []byte) error
GetAllCalled func(unitType dataRetriever.UnitType, keys [][]byte) (map[string][]byte, error)
DestroyCalled func() error
CloseAllCalled func() error
SetEpochForPutOperationCalled func(epoch uint32)
GetAllStorersCalled func() map[dataRetriever.UnitType]storage.Storer
}
// SetEpochForPutOperation -
func (csm *ChainStorerMock) SetEpochForPutOperation(epoch uint32) {
if csm.SetEpochForPutOperationCalled != nil {
csm.SetEpochForPutOperationCalled(epoch)
}
}
// CloseAll -
func (csm *ChainStorerMock) CloseAll() error {
if csm.CloseAllCalled != nil {
return csm.CloseAllCalled()
}
return nil
}
// AddStorer will add a new storer to the chain map
func (csm *ChainStorerMock) AddStorer(key dataRetriever.UnitType, s storage.Storer) {
if csm.AddStorerCalled != nil {
csm.AddStorerCalled(key, s)
}
}
// GetStorer returns the storer from the chain map or nil if the storer was not found
func (csm *ChainStorerMock) GetStorer(unitType dataRetriever.UnitType) storage.Storer {
if csm.GetStorerCalled != nil {
return csm.GetStorerCalled(unitType)
}
return &StorerMock{}
}
// Has returns true if the key is found in the selected Unit or false otherwise
// It can return an error if the provided unit type is not supported or if the
// underlying implementation of the storage unit reports an error.
func (csm *ChainStorerMock) Has(unitType dataRetriever.UnitType, key []byte) error {
if csm.HasCalled != nil {
return csm.HasCalled(unitType, key)
}
return errors.New("Key not found")
}
// Get returns the value for the given key if found in the selected storage unit,
// nil otherwise. It can return an error if the provided unit type is not supported
// or if the storage unit underlying implementation reports an error
func (csm *ChainStorerMock) Get(unitType dataRetriever.UnitType, key []byte) ([]byte, error) {
if csm.GetCalled != nil {
return csm.GetCalled(unitType, key)
}
return nil, nil
}
// Put stores the key, value pair in the selected storage unit
// It can return an error if the provided unit type is not supported
// or if the storage unit underlying implementation reports an error
func (csm *ChainStorerMock) Put(unitType dataRetriever.UnitType, key []byte, value []byte) error {
if csm.PutCalled != nil {
return csm.PutCalled(unitType, key, value)
}
return nil
}
// GetAllStorers -
func (csm *ChainStorerMock) GetAllStorers() map[dataRetriever.UnitType]storage.Storer {
if csm.GetAllStorersCalled != nil {
return csm.GetAllStorersCalled()
}
return nil
}
// GetAll gets all the elements with keys in the keys array, from the selected storage unit
// It can report an error if the provided unit type is not supported, if there is a missing
// key in the unit, or if the underlying implementation of the storage unit reports an error.
func (csm *ChainStorerMock) GetAll(unitType dataRetriever.UnitType, keys [][]byte) (map[string][]byte, error) {
if csm.GetAllCalled != nil {
return csm.GetAllCalled(unitType, keys)
}
return nil, nil
}
// Destroy removes the underlying files/resources used by the storage service
func (csm *ChainStorerMock) Destroy() error {
if csm.DestroyCalled != nil {
return csm.DestroyCalled()
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (csm *ChainStorerMock) IsInterfaceNil() bool {
return csm == nil
}