/
transaction_test.go
176 lines (154 loc) · 5.2 KB
/
transaction_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
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
173
174
175
176
package event
import (
"fmt"
"os"
"testing"
"time"
"0chain.net/chaincore/config"
"0chain.net/smartcontract/common"
"0chain.net/smartcontract/dbs/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTagAddTransactions(t *testing.T) {
edb, clean := GetTestEventDB(t)
defer clean()
round := int64(7)
transactionsEvents := []Event{
{
BlockNumber: round,
Type: TypeStats,
Tag: TagAddTransactions,
Index: "one",
Data: Transaction{Hash: "one", Fee: 3},
},
{
BlockNumber: round,
Type: TypeStats,
Tag: TagAddTransactions,
Index: "one",
Data: Transaction{Hash: "one", Fee: 3},
},
{
BlockNumber: round,
TxHash: "2",
Type: TypeStats,
Tag: TagAddTransactions,
Index: "two",
Data: Transaction{Hash: "two", Fee: 5},
},
{
BlockNumber: round,
Type: TypeStats,
Tag: TagAddTransactions,
Index: "two",
Data: Transaction{Hash: "two", Fee: 7},
},
{
BlockNumber: round,
Type: TypeStats,
Tag: TagAddTransactions,
Index: "three",
Data: Transaction{Hash: "three", Fee: 11},
},
}
events, err := mergeEvents(round, "", transactionsEvents)
require.NoError(t, err)
require.Len(t, events, 1)
require.Len(t, events[0].Data, 3, "the five events should have been merged into three")
require.NoError(t, edb.addStat(events[0]))
var txs []Transaction
edb.Get().Find(&txs)
require.Len(t, txs, 3)
}
func TestFindTransactionByHash(t *testing.T) {
access := config.DbAccess{
Enabled: true,
Name: os.Getenv("POSTGRES_DB"),
User: os.Getenv("POSTGRES_USER"),
Password: os.Getenv("POSTGRES_PASSWORD"),
Host: os.Getenv("POSTGRES_HOST"),
Port: os.Getenv("POSTGRES_PORT"),
MaxIdleConns: 100,
MaxOpenConns: 200,
ConnMaxLifetime: 20 * time.Second,
}
t.Skip("only for local debugging, requires local postgresql")
eventDb, err := NewEventDb(access, config.DbSettings{})
if err != nil {
return
}
defer eventDb.Close()
err = eventDb.AutoMigrate()
defer eventDb.Drop()
require.NoError(t, err)
tr := Transaction{
ImmutableModel: model.ImmutableModel{ID: 1},
Hash: "something_0",
ClientId: "someClientID",
BlockHash: "blockHash",
}
SetUpTransactionData(t, eventDb)
t.Run("GetTransactionByHash", func(t *testing.T) {
gotTr, err := eventDb.GetTransactionByHash("something_0")
// To ignore createdAt and updatedAt
tr.ImmutableModel.ID = gotTr.ID
tr.CreatedAt = gotTr.CreatedAt
require.Equal(t, tr, gotTr, "Transaction not getting inserted")
gotTr, err = eventDb.GetTransactionByHash("some")
require.Error(t, err, "issue while getting the transaction by hash")
})
t.Run("GetTransactionByClientId", func(t *testing.T) {
gotTrs, err := eventDb.GetTransactionByClientId("someClientID", common.Pagination{Limit: 10, IsDescending: true})
require.NoError(t, err)
compareTransactions(t, gotTrs, 0, 10)
gotTrs, err = eventDb.GetTransactionByClientId("someClient", common.Pagination{Limit: 10, IsDescending: true})
require.NoError(t, err)
require.Equal(t, len(gotTrs), 0, "No Transaction should be returned")
gotTrs, err = eventDb.GetTransactionByClientId("someClientID", common.Pagination{Limit: 5, IsDescending: true})
require.NoError(t, err)
compareTransactions(t, gotTrs, 0, 5)
gotTrs, err = eventDb.GetTransactionByClientId("someClientID", common.Pagination{Offset: 5, Limit: 5, IsDescending: true})
require.NoError(t, err)
compareTransactions(t, gotTrs, 5, 5)
})
t.Run("GetTransactionByBlockHash", func(t *testing.T) {
gotTrs, err := eventDb.GetTransactionByBlockHash("blockHash", common.Pagination{Limit: 10, IsDescending: true})
require.NoError(t, err)
compareTransactions(t, gotTrs, 0, 10)
gotTrs, err = eventDb.GetTransactionByBlockHash("someHash", common.Pagination{Limit: 10, IsDescending: true})
assert.NoError(t, err)
require.Equal(t, len(gotTrs), 0, "No Transaction should be returned")
gotTrs, err = eventDb.GetTransactionByBlockHash("blockHash", common.Pagination{Limit: 5, IsDescending: true})
assert.NoError(t, err)
compareTransactions(t, gotTrs, 0, 5)
gotTrs, err = eventDb.GetTransactionByBlockHash("blockHash", common.Pagination{Offset: 5, Limit: 5, IsDescending: true})
assert.NoError(t, err)
compareTransactions(t, gotTrs, 5, 5)
})
}
func compareTransactions(t *testing.T, gotTr []Transaction, offset, limit int) {
require.Equal(t, limit, len(gotTr), "Not all transactions were returned")
i := 0
for i = offset; i < limit; i++ {
tr := Transaction{
Hash: fmt.Sprintf("something_%d", i),
ClientId: "someClientID",
BlockHash: "blockHash",
}
tr.ImmutableModel.ID = gotTr[i].ID
tr.CreatedAt = gotTr[i].CreatedAt
require.Equal(t, tr, gotTr[i], "Transaction not matching")
}
}
func SetUpTransactionData(t *testing.T, eventDb *EventDb) {
for i := 0; i < 10; i++ {
tr := Transaction{
Hash: fmt.Sprintf("something_%d", i),
ClientId: "someClientID",
BlockHash: "blockHash",
}
err := eventDb.addTransactions([]Transaction{tr})
require.NoError(t, err, "Error while inserting Transaction to event Database")
}
}