From a3e9d544a64cf6bb67e4d95d495a0d4c7dedd1ca Mon Sep 17 00:00:00 2001 From: i-norden Date: Fri, 26 Nov 2021 09:12:10 -0600 Subject: [PATCH] finish unit tests --- .../indexer/database/file/indexer_test.go | 65 ++++++++++++------- .../indexer/database/sql/pgx_indexer_test.go | 62 ++++++++++++------ .../indexer/database/sql/sqlx_indexer_test.go | 62 ++++++++++++------ statediff/indexer/mocks/test_data.go | 2 +- 4 files changed, 126 insertions(+), 65 deletions(-) diff --git a/statediff/indexer/database/file/indexer_test.go b/statediff/indexer/database/file/indexer_test.go index b0a8835ee8c6..c2be9f993993 100644 --- a/statediff/indexer/database/file/indexer_test.go +++ b/statediff/indexer/database/file/indexer_test.go @@ -52,6 +52,8 @@ var ( ipfsPgGet = `SELECT data FROM public.blocks WHERE key = $1` tx1, tx2, tx3, tx4, tx5, rct1, rct2, rct3, rct4, rct5 []byte + txs types.Transactions + rcts types.Receipts mockBlock *types.Block headerCID, trx1CID, trx2CID, trx3CID, trx4CID, trx5CID cid.Cid rct1CID, rct2CID, rct3CID, rct4CID, rct5CID cid.Cid @@ -65,7 +67,7 @@ func init() { } mockBlock = mocks.MockBlock - txs, rcts := mocks.MockBlock.Transactions(), mocks.MockReceipts + txs, rcts = mocks.MockBlock.Transactions(), mocks.MockReceipts buf := new(bytes.Buffer) txs.EncodeIndex(0, buf) @@ -231,6 +233,10 @@ func TestFileIndexer(t *testing.T) { expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String())) // and published + type txResult struct { + TxType uint8 `db:"tx_type"` + Value string + } for _, c := range trxs { dc, err := cid.Decode(c) if err != nil { @@ -243,47 +249,59 @@ func TestFileIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - txTypePgStr := `SELECT tx_type FROM eth.transaction_cids WHERE cid = $1` + txTypeAndValueStr := `SELECT tx_type, value FROM eth.transaction_cids WHERE cid = $1` switch c { case trx1CID.String(): test_helpers.ExpectEqual(t, data, tx1) - var txType uint8 - err = sqlxdb.Get(&txType, txTypePgStr, c) + txRes := new(txResult) + err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected LegacyTxType (0), got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != txs[0].Value().String() { + t.Fatalf("expected tx value %s got %s", txs[0].Value().String(), txRes.Value) } case trx2CID.String(): test_helpers.ExpectEqual(t, data, tx2) - var txType uint8 - err = sqlxdb.Get(&txType, txTypePgStr, c) + txRes := new(txResult) + err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected LegacyTxType (0), got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != txs[1].Value().String() { + t.Fatalf("expected tx value %s got %s", txs[1].Value().String(), txRes.Value) } case trx3CID.String(): test_helpers.ExpectEqual(t, data, tx3) - var txType uint8 - err = sqlxdb.Get(&txType, txTypePgStr, c) + txRes := new(txResult) + err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected LegacyTxType (0), got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != txs[2].Value().String() { + t.Fatalf("expected tx value %s got %s", txs[2].Value().String(), txRes.Value) } case trx4CID.String(): test_helpers.ExpectEqual(t, data, tx4) - var txType uint8 - err = sqlxdb.Get(&txType, txTypePgStr, c) + txRes := new(txResult) + err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { t.Fatal(err) } - if txType != types.AccessListTxType { - t.Fatalf("expected AccessListTxType (1), got %d", txType) + if txRes.TxType != types.AccessListTxType { + t.Fatalf("expected AccessListTxType (1), got %d", txRes.TxType) + } + if txRes.Value != txs[3].Value().String() { + t.Fatalf("expected tx value %s got %s", txs[3].Value().String(), txRes.Value) } accessListElementModels := make([]models.AccessListElementModel, 0) 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) { test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model) case trx5CID.String(): test_helpers.ExpectEqual(t, data, tx5) - var txType *uint8 - err = sqlxdb.Get(&txType, txTypePgStr, c) + txRes := new(txResult) + err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes) if err != nil { t.Fatal(err) } - if *txType != types.DynamicFeeTxType { - t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType) + if txRes.TxType != types.DynamicFeeTxType { + t.Fatalf("expected DynamicFeeTxType (2), got %d", txRes.TxType) + } + if txRes.Value != txs[4].Value().String() { + t.Fatalf("expected tx value %s got %s", txs[4].Value().String(), txRes.Value) } } } diff --git a/statediff/indexer/database/sql/pgx_indexer_test.go b/statediff/indexer/database/sql/pgx_indexer_test.go index f78882986ca8..bdb52403252b 100644 --- a/statediff/indexer/database/sql/pgx_indexer_test.go +++ b/statediff/indexer/database/sql/pgx_indexer_test.go @@ -207,6 +207,11 @@ func TestPGXIndexer(t *testing.T) { expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String())) // and published + transactions := mocks.MockBlock.Transactions() + type txResult struct { + TxType uint8 `db:"tx_type"` + Value string + } for _, c := range trxs { dc, err := cid.Decode(c) if err != nil { @@ -219,47 +224,59 @@ func TestPGXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - txTypePgStr := `SELECT tx_type FROM eth.transaction_cids WHERE cid = $1` + txTypeAndValueStr := `SELECT tx_type, CAST(value as TEXT) FROM eth.transaction_cids WHERE cid = $1` switch c { case trx1CID.String(): test_helpers.ExpectEqual(t, data, tx1) - var txType uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected tx_type 0, got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != transactions[0].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[0].Value().String(), txRes.Value) } case trx2CID.String(): test_helpers.ExpectEqual(t, data, tx2) - var txType uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected tx_type 0, got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != transactions[1].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[1].Value().String(), txRes.Value) } case trx3CID.String(): test_helpers.ExpectEqual(t, data, tx3) - var txType uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected tx_type 0, got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != transactions[2].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[2].Value().String(), txRes.Value) } case trx4CID.String(): test_helpers.ExpectEqual(t, data, tx4) - var txType uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { t.Fatal(err) } - if txType != types.AccessListTxType { - t.Fatalf("expected AccessListTxType (1), got %d", txType) + if txRes.TxType != types.AccessListTxType { + t.Fatalf("expected AccessListTxType (1), got %d", txRes.TxType) + } + if txRes.Value != transactions[3].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[3].Value().String(), txRes.Value) } accessListElementModels := make([]models.AccessListElementModel, 0) 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) { test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model) case trx5CID.String(): test_helpers.ExpectEqual(t, data, tx5) - var txType *uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value) if err != nil { t.Fatal(err) } - if *txType != types.DynamicFeeTxType { - t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType) + if txRes.TxType != types.DynamicFeeTxType { + t.Fatalf("expected DynamicFeeTxType (2), got %d", txRes.TxType) + } + if txRes.Value != transactions[4].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[4].Value().String(), txRes.Value) } } } diff --git a/statediff/indexer/database/sql/sqlx_indexer_test.go b/statediff/indexer/database/sql/sqlx_indexer_test.go index d4854e989be6..9ab1163ecf01 100644 --- a/statediff/indexer/database/sql/sqlx_indexer_test.go +++ b/statediff/indexer/database/sql/sqlx_indexer_test.go @@ -229,6 +229,11 @@ func TestSQLXIndexer(t *testing.T) { expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String())) // and published + transactions := mocks.MockBlock.Transactions() + type txResult struct { + TxType uint8 `db:"tx_type"` + Value string + } for _, c := range trxs { dc, err := cid.Decode(c) if err != nil { @@ -241,47 +246,59 @@ func TestSQLXIndexer(t *testing.T) { if err != nil { t.Fatal(err) } - txTypePgStr := `SELECT tx_type FROM eth.transaction_cids WHERE cid = $1` + txTypeAndValueStr := `SELECT tx_type, value FROM eth.transaction_cids WHERE cid = $1` switch c { case trx1CID.String(): test_helpers.ExpectEqual(t, data, tx1) - var txType uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected LegacyTxType (0), got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != transactions[0].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[0].Value().String(), txRes.Value) } case trx2CID.String(): test_helpers.ExpectEqual(t, data, tx2) - var txType uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected LegacyTxType (0), got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != transactions[1].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[1].Value().String(), txRes.Value) } case trx3CID.String(): test_helpers.ExpectEqual(t, data, tx3) - var txType uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { t.Fatal(err) } - if txType != 0 { - t.Fatalf("expected LegacyTxType (0), got %d", txType) + if txRes.TxType != 0 { + t.Fatalf("expected LegacyTxType (0), got %d", txRes.TxType) + } + if txRes.Value != transactions[2].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[2].Value().String(), txRes.Value) } case trx4CID.String(): test_helpers.ExpectEqual(t, data, tx4) - var txType uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { t.Fatal(err) } - if txType != types.AccessListTxType { - t.Fatalf("expected AccessListTxType (1), got %d", txType) + if txRes.TxType != types.AccessListTxType { + t.Fatalf("expected AccessListTxType (1), got %d", txRes.TxType) + } + if txRes.Value != transactions[3].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[3].Value().String(), txRes.Value) } accessListElementModels := make([]models.AccessListElementModel, 0) 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) { test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model) case trx5CID.String(): test_helpers.ExpectEqual(t, data, tx5) - var txType *uint8 - err = db.Get(context.Background(), &txType, txTypePgStr, c) + txRes := new(txResult) + err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes) if err != nil { t.Fatal(err) } - if *txType != types.DynamicFeeTxType { - t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType) + if txRes.TxType != types.DynamicFeeTxType { + t.Fatalf("expected DynamicFeeTxType (2), got %d", txRes.TxType) + } + if txRes.Value != transactions[4].Value().String() { + t.Fatalf("expected tx value %s got %s", transactions[4].Value().String(), txRes.Value) } } } diff --git a/statediff/indexer/mocks/test_data.go b/statediff/indexer/mocks/test_data.go index 35d70d2f945c..dccf72e607b0 100644 --- a/statediff/indexer/mocks/test_data.go +++ b/statediff/indexer/mocks/test_data.go @@ -308,7 +308,7 @@ func createTransactionsAndReceipts(config *params.ChainConfig, blockNumber *big. GasPrice: big.NewInt(100), Gas: 50, To: &AnotherAddress, - Value: big.NewInt(1000), + Value: big.NewInt(999), Data: []byte{}, AccessList: types.AccessList{ AccessListEntry1,