Skip to content

Commit a3e9d54

Browse files
committed
finish unit tests
1 parent 6c285d6 commit a3e9d54

File tree

4 files changed

+126
-65
lines changed

4 files changed

+126
-65
lines changed

statediff/indexer/database/file/indexer_test.go

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ var (
5252
ipfsPgGet = `SELECT data FROM public.blocks
5353
WHERE key = $1`
5454
tx1, tx2, tx3, tx4, tx5, rct1, rct2, rct3, rct4, rct5 []byte
55+
txs types.Transactions
56+
rcts types.Receipts
5557
mockBlock *types.Block
5658
headerCID, trx1CID, trx2CID, trx3CID, trx4CID, trx5CID cid.Cid
5759
rct1CID, rct2CID, rct3CID, rct4CID, rct5CID cid.Cid
@@ -65,7 +67,7 @@ func init() {
6567
}
6668

6769
mockBlock = mocks.MockBlock
68-
txs, rcts := mocks.MockBlock.Transactions(), mocks.MockReceipts
70+
txs, rcts = mocks.MockBlock.Transactions(), mocks.MockReceipts
6971

7072
buf := new(bytes.Buffer)
7173
txs.EncodeIndex(0, buf)
@@ -231,6 +233,10 @@ func TestFileIndexer(t *testing.T) {
231233
expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String()))
232234
expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String()))
233235
// and published
236+
type txResult struct {
237+
TxType uint8 `db:"tx_type"`
238+
Value string
239+
}
234240
for _, c := range trxs {
235241
dc, err := cid.Decode(c)
236242
if err != nil {
@@ -243,47 +249,59 @@ func TestFileIndexer(t *testing.T) {
243249
if err != nil {
244250
t.Fatal(err)
245251
}
246-
txTypePgStr := `SELECT tx_type FROM eth.transaction_cids WHERE cid = $1`
252+
txTypeAndValueStr := `SELECT tx_type, value FROM eth.transaction_cids WHERE cid = $1`
247253
switch c {
248254
case trx1CID.String():
249255
test_helpers.ExpectEqual(t, data, tx1)
250-
var txType uint8
251-
err = sqlxdb.Get(&txType, txTypePgStr, c)
256+
txRes := new(txResult)
257+
err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
252258
if err != nil {
253259
t.Fatal(err)
254260
}
255-
if txType != 0 {
256-
t.Fatalf("expected LegacyTxType (0), got %d", txType)
261+
if txRes.TxType != 0 {
262+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
263+
}
264+
if txRes.Value != txs[0].Value().String() {
265+
t.Fatalf("expected tx value %s got %s", txs[0].Value().String(), txRes.Value)
257266
}
258267
case trx2CID.String():
259268
test_helpers.ExpectEqual(t, data, tx2)
260-
var txType uint8
261-
err = sqlxdb.Get(&txType, txTypePgStr, c)
269+
txRes := new(txResult)
270+
err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
262271
if err != nil {
263272
t.Fatal(err)
264273
}
265-
if txType != 0 {
266-
t.Fatalf("expected LegacyTxType (0), got %d", txType)
274+
if txRes.TxType != 0 {
275+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
276+
}
277+
if txRes.Value != txs[1].Value().String() {
278+
t.Fatalf("expected tx value %s got %s", txs[1].Value().String(), txRes.Value)
267279
}
268280
case trx3CID.String():
269281
test_helpers.ExpectEqual(t, data, tx3)
270-
var txType uint8
271-
err = sqlxdb.Get(&txType, txTypePgStr, c)
282+
txRes := new(txResult)
283+
err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
272284
if err != nil {
273285
t.Fatal(err)
274286
}
275-
if txType != 0 {
276-
t.Fatalf("expected LegacyTxType (0), got %d", txType)
287+
if txRes.TxType != 0 {
288+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
289+
}
290+
if txRes.Value != txs[2].Value().String() {
291+
t.Fatalf("expected tx value %s got %s", txs[2].Value().String(), txRes.Value)
277292
}
278293
case trx4CID.String():
279294
test_helpers.ExpectEqual(t, data, tx4)
280-
var txType uint8
281-
err = sqlxdb.Get(&txType, txTypePgStr, c)
295+
txRes := new(txResult)
296+
err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
282297
if err != nil {
283298
t.Fatal(err)
284299
}
285-
if txType != types.AccessListTxType {
286-
t.Fatalf("expected AccessListTxType (1), got %d", txType)
300+
if txRes.TxType != types.AccessListTxType {
301+
t.Fatalf("expected AccessListTxType (1), got %d", txRes.TxType)
302+
}
303+
if txRes.Value != txs[3].Value().String() {
304+
t.Fatalf("expected tx value %s got %s", txs[3].Value().String(), txRes.Value)
287305
}
288306
accessListElementModels := make([]models.AccessListElementModel, 0)
289307
pgStr = `SELECT access_list_elements.* FROM eth.access_list_elements INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC`
@@ -307,13 +325,16 @@ func TestFileIndexer(t *testing.T) {
307325
test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model)
308326
case trx5CID.String():
309327
test_helpers.ExpectEqual(t, data, tx5)
310-
var txType *uint8
311-
err = sqlxdb.Get(&txType, txTypePgStr, c)
328+
txRes := new(txResult)
329+
err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
312330
if err != nil {
313331
t.Fatal(err)
314332
}
315-
if *txType != types.DynamicFeeTxType {
316-
t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType)
333+
if txRes.TxType != types.DynamicFeeTxType {
334+
t.Fatalf("expected DynamicFeeTxType (2), got %d", txRes.TxType)
335+
}
336+
if txRes.Value != txs[4].Value().String() {
337+
t.Fatalf("expected tx value %s got %s", txs[4].Value().String(), txRes.Value)
317338
}
318339
}
319340
}

statediff/indexer/database/sql/pgx_indexer_test.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ func TestPGXIndexer(t *testing.T) {
207207
expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String()))
208208
expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String()))
209209
// and published
210+
transactions := mocks.MockBlock.Transactions()
211+
type txResult struct {
212+
TxType uint8 `db:"tx_type"`
213+
Value string
214+
}
210215
for _, c := range trxs {
211216
dc, err := cid.Decode(c)
212217
if err != nil {
@@ -219,47 +224,59 @@ func TestPGXIndexer(t *testing.T) {
219224
if err != nil {
220225
t.Fatal(err)
221226
}
222-
txTypePgStr := `SELECT tx_type FROM eth.transaction_cids WHERE cid = $1`
227+
txTypeAndValueStr := `SELECT tx_type, CAST(value as TEXT) FROM eth.transaction_cids WHERE cid = $1`
223228
switch c {
224229
case trx1CID.String():
225230
test_helpers.ExpectEqual(t, data, tx1)
226-
var txType uint8
227-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
231+
txRes := new(txResult)
232+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
228233
if err != nil {
229234
t.Fatal(err)
230235
}
231-
if txType != 0 {
232-
t.Fatalf("expected tx_type 0, got %d", txType)
236+
if txRes.TxType != 0 {
237+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
238+
}
239+
if txRes.Value != transactions[0].Value().String() {
240+
t.Fatalf("expected tx value %s got %s", transactions[0].Value().String(), txRes.Value)
233241
}
234242
case trx2CID.String():
235243
test_helpers.ExpectEqual(t, data, tx2)
236-
var txType uint8
237-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
244+
txRes := new(txResult)
245+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
238246
if err != nil {
239247
t.Fatal(err)
240248
}
241-
if txType != 0 {
242-
t.Fatalf("expected tx_type 0, got %d", txType)
249+
if txRes.TxType != 0 {
250+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
251+
}
252+
if txRes.Value != transactions[1].Value().String() {
253+
t.Fatalf("expected tx value %s got %s", transactions[1].Value().String(), txRes.Value)
243254
}
244255
case trx3CID.String():
245256
test_helpers.ExpectEqual(t, data, tx3)
246-
var txType uint8
247-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
257+
txRes := new(txResult)
258+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
248259
if err != nil {
249260
t.Fatal(err)
250261
}
251-
if txType != 0 {
252-
t.Fatalf("expected tx_type 0, got %d", txType)
262+
if txRes.TxType != 0 {
263+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
264+
}
265+
if txRes.Value != transactions[2].Value().String() {
266+
t.Fatalf("expected tx value %s got %s", transactions[2].Value().String(), txRes.Value)
253267
}
254268
case trx4CID.String():
255269
test_helpers.ExpectEqual(t, data, tx4)
256-
var txType uint8
257-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
270+
txRes := new(txResult)
271+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
258272
if err != nil {
259273
t.Fatal(err)
260274
}
261-
if txType != types.AccessListTxType {
262-
t.Fatalf("expected AccessListTxType (1), got %d", txType)
275+
if txRes.TxType != types.AccessListTxType {
276+
t.Fatalf("expected AccessListTxType (1), got %d", txRes.TxType)
277+
}
278+
if txRes.Value != transactions[3].Value().String() {
279+
t.Fatalf("expected tx value %s got %s", transactions[3].Value().String(), txRes.Value)
263280
}
264281
accessListElementModels := make([]models.AccessListElementModel, 0)
265282
pgStr = `SELECT access_list_elements.* FROM eth.access_list_elements INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC`
@@ -283,13 +300,16 @@ func TestPGXIndexer(t *testing.T) {
283300
test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model)
284301
case trx5CID.String():
285302
test_helpers.ExpectEqual(t, data, tx5)
286-
var txType *uint8
287-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
303+
txRes := new(txResult)
304+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
288305
if err != nil {
289306
t.Fatal(err)
290307
}
291-
if *txType != types.DynamicFeeTxType {
292-
t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType)
308+
if txRes.TxType != types.DynamicFeeTxType {
309+
t.Fatalf("expected DynamicFeeTxType (2), got %d", txRes.TxType)
310+
}
311+
if txRes.Value != transactions[4].Value().String() {
312+
t.Fatalf("expected tx value %s got %s", transactions[4].Value().String(), txRes.Value)
293313
}
294314
}
295315
}

statediff/indexer/database/sql/sqlx_indexer_test.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ func TestSQLXIndexer(t *testing.T) {
229229
expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String()))
230230
expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String()))
231231
// and published
232+
transactions := mocks.MockBlock.Transactions()
233+
type txResult struct {
234+
TxType uint8 `db:"tx_type"`
235+
Value string
236+
}
232237
for _, c := range trxs {
233238
dc, err := cid.Decode(c)
234239
if err != nil {
@@ -241,47 +246,59 @@ func TestSQLXIndexer(t *testing.T) {
241246
if err != nil {
242247
t.Fatal(err)
243248
}
244-
txTypePgStr := `SELECT tx_type FROM eth.transaction_cids WHERE cid = $1`
249+
txTypeAndValueStr := `SELECT tx_type, value FROM eth.transaction_cids WHERE cid = $1`
245250
switch c {
246251
case trx1CID.String():
247252
test_helpers.ExpectEqual(t, data, tx1)
248-
var txType uint8
249-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
253+
txRes := new(txResult)
254+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
250255
if err != nil {
251256
t.Fatal(err)
252257
}
253-
if txType != 0 {
254-
t.Fatalf("expected LegacyTxType (0), got %d", txType)
258+
if txRes.TxType != 0 {
259+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
260+
}
261+
if txRes.Value != transactions[0].Value().String() {
262+
t.Fatalf("expected tx value %s got %s", transactions[0].Value().String(), txRes.Value)
255263
}
256264
case trx2CID.String():
257265
test_helpers.ExpectEqual(t, data, tx2)
258-
var txType uint8
259-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
266+
txRes := new(txResult)
267+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
260268
if err != nil {
261269
t.Fatal(err)
262270
}
263-
if txType != 0 {
264-
t.Fatalf("expected LegacyTxType (0), got %d", txType)
271+
if txRes.TxType != 0 {
272+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
273+
}
274+
if txRes.Value != transactions[1].Value().String() {
275+
t.Fatalf("expected tx value %s got %s", transactions[1].Value().String(), txRes.Value)
265276
}
266277
case trx3CID.String():
267278
test_helpers.ExpectEqual(t, data, tx3)
268-
var txType uint8
269-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
279+
txRes := new(txResult)
280+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
270281
if err != nil {
271282
t.Fatal(err)
272283
}
273-
if txType != 0 {
274-
t.Fatalf("expected LegacyTxType (0), got %d", txType)
284+
if txRes.TxType != 0 {
285+
t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType)
286+
}
287+
if txRes.Value != transactions[2].Value().String() {
288+
t.Fatalf("expected tx value %s got %s", transactions[2].Value().String(), txRes.Value)
275289
}
276290
case trx4CID.String():
277291
test_helpers.ExpectEqual(t, data, tx4)
278-
var txType uint8
279-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
292+
txRes := new(txResult)
293+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
280294
if err != nil {
281295
t.Fatal(err)
282296
}
283-
if txType != types.AccessListTxType {
284-
t.Fatalf("expected AccessListTxType (1), got %d", txType)
297+
if txRes.TxType != types.AccessListTxType {
298+
t.Fatalf("expected AccessListTxType (1), got %d", txRes.TxType)
299+
}
300+
if txRes.Value != transactions[3].Value().String() {
301+
t.Fatalf("expected tx value %s got %s", transactions[3].Value().String(), txRes.Value)
285302
}
286303
accessListElementModels := make([]models.AccessListElementModel, 0)
287304
pgStr = `SELECT access_list_elements.* FROM eth.access_list_elements INNER JOIN eth.transaction_cids ON (tx_id = transaction_cids.tx_hash) WHERE cid = $1 ORDER BY access_list_elements.index ASC`
@@ -305,13 +322,16 @@ func TestSQLXIndexer(t *testing.T) {
305322
test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model)
306323
case trx5CID.String():
307324
test_helpers.ExpectEqual(t, data, tx5)
308-
var txType *uint8
309-
err = db.Get(context.Background(), &txType, txTypePgStr, c)
325+
txRes := new(txResult)
326+
err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
310327
if err != nil {
311328
t.Fatal(err)
312329
}
313-
if *txType != types.DynamicFeeTxType {
314-
t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType)
330+
if txRes.TxType != types.DynamicFeeTxType {
331+
t.Fatalf("expected DynamicFeeTxType (2), got %d", txRes.TxType)
332+
}
333+
if txRes.Value != transactions[4].Value().String() {
334+
t.Fatalf("expected tx value %s got %s", transactions[4].Value().String(), txRes.Value)
315335
}
316336
}
317337
}

statediff/indexer/mocks/test_data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func createTransactionsAndReceipts(config *params.ChainConfig, blockNumber *big.
308308
GasPrice: big.NewInt(100),
309309
Gas: 50,
310310
To: &AnotherAddress,
311-
Value: big.NewInt(1000),
311+
Value: big.NewInt(999),
312312
Data: []byte{},
313313
AccessList: types.AccessList{
314314
AccessListEntry1,

0 commit comments

Comments
 (0)