-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase_test.go
More file actions
172 lines (154 loc) · 4.96 KB
/
Copy pathdatabase_test.go
File metadata and controls
172 lines (154 loc) · 4.96 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package handlers_test
import (
"context"
"fmt"
"time"
"github.com/Cogwheel-Validator/spectra-gnoland-indexer/pkgs/database"
)
type MockDatabase struct {
blocks map[uint64]*database.BlockData
transactions map[string]*database.Transaction
addressTxs map[string]*[]database.AddressTx
blockSigners map[uint64]*database.BlockSigners
latestBlock *database.BlockData
bankSend map[string]*database.BankSend
msgCall map[string]*database.MsgCall
msgAddPackage map[string]*database.MsgAddPackage
msgRun map[string]*database.MsgRun
msgTypes map[string][]string
shouldError bool
errorMsg string
}
func (m *MockDatabase) GetBlock(ctx context.Context, height uint64, chainName string) (*database.BlockData, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
block, ok := m.blocks[height]
if !ok {
return nil, fmt.Errorf("block not found")
}
return block, nil
}
func (m *MockDatabase) GetFromToBlocks(ctx context.Context, fromHeight uint64, toHeight uint64, chainName string) ([]*database.BlockData, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
var result []*database.BlockData
for i := fromHeight; i <= toHeight; i++ {
block, ok := m.blocks[i]
if !ok {
return nil, fmt.Errorf("block not found")
}
result = append(result, block)
}
return result, nil
}
func (m *MockDatabase) GetAllBlockSigners(ctx context.Context, chainName string, blockHeight uint64) (*database.BlockSigners, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
blockSigners, ok := m.blockSigners[blockHeight]
if !ok {
return nil, fmt.Errorf("block signers not found")
}
return blockSigners, nil
}
func (m *MockDatabase) GetTransaction(ctx context.Context, txHash string, chainName string) (*database.Transaction, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
transaction, ok := m.transactions[txHash]
if !ok {
return nil, fmt.Errorf("transaction not found")
}
return transaction, nil
}
func (m *MockDatabase) GetAddressTxs(ctx context.Context, address string, chainName string, fromTimestamp time.Time, toTimestamp time.Time) (*[]database.AddressTx, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
addressTxs, ok := m.addressTxs[address]
if !ok {
return nil, fmt.Errorf("address transactions not found")
}
return addressTxs, nil
}
func (m *MockDatabase) GetLatestBlock(ctx context.Context, chainName string) (*database.BlockData, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
if m.latestBlock == nil {
return nil, fmt.Errorf("latest block not found")
}
return m.latestBlock, nil
}
func (m *MockDatabase) GetLastXBlocks(ctx context.Context, chainName string, x uint64) ([]*database.BlockData, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
blocks := make([]*database.BlockData, 0, len(m.blocks))
for _, block := range m.blocks {
blocks = append(blocks, block)
}
return blocks, nil
}
func (m *MockDatabase) GetLastXTransactions(ctx context.Context, chainName string, x uint64) ([]*database.Transaction, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
transactions := make([]*database.Transaction, 0, len(m.transactions))
for _, transaction := range m.transactions {
transactions = append(transactions, transaction)
}
return transactions, nil
}
func (m *MockDatabase) GetMsgTypes(ctx context.Context, txHash string, chainName string) ([]string, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
msgTypes, ok := m.msgTypes[txHash]
if !ok {
return nil, fmt.Errorf("message type not found")
}
return msgTypes, nil
}
func (m *MockDatabase) GetBankSend(ctx context.Context, txHash string, chainName string) ([]*database.BankSend, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
bankSend, ok := m.bankSend[txHash]
if !ok {
return nil, fmt.Errorf("bank send not found")
}
return []*database.BankSend{bankSend}, nil
}
func (m *MockDatabase) GetMsgCall(ctx context.Context, txHash string, chainName string) ([]*database.MsgCall, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
msgCall, ok := m.msgCall[txHash]
if !ok {
return nil, fmt.Errorf("message call not found")
}
return []*database.MsgCall{msgCall}, nil
}
func (m *MockDatabase) GetMsgAddPackage(ctx context.Context, txHash string, chainName string) ([]*database.MsgAddPackage, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
msgAddPackage, ok := m.msgAddPackage[txHash]
if !ok {
return nil, fmt.Errorf("message add package not found")
}
return []*database.MsgAddPackage{msgAddPackage}, nil
}
func (m *MockDatabase) GetMsgRun(ctx context.Context, txHash string, chainName string) ([]*database.MsgRun, error) {
if m.shouldError {
return nil, fmt.Errorf("%s", m.errorMsg)
}
msgRun, ok := m.msgRun[txHash]
if !ok {
return nil, fmt.Errorf("message run not found")
}
return []*database.MsgRun{msgRun}, nil
}