finish unit tests

This commit is contained in:
i-norden 2021-11-26 09:12:10 -06:00
parent 6c285d6d1d
commit a3e9d544a6
4 changed files with 126 additions and 65 deletions

View File

@ -52,6 +52,8 @@ var (
ipfsPgGet = `SELECT data FROM public.blocks ipfsPgGet = `SELECT data FROM public.blocks
WHERE key = $1` WHERE key = $1`
tx1, tx2, tx3, tx4, tx5, rct1, rct2, rct3, rct4, rct5 []byte tx1, tx2, tx3, tx4, tx5, rct1, rct2, rct3, rct4, rct5 []byte
txs types.Transactions
rcts types.Receipts
mockBlock *types.Block mockBlock *types.Block
headerCID, trx1CID, trx2CID, trx3CID, trx4CID, trx5CID cid.Cid headerCID, trx1CID, trx2CID, trx3CID, trx4CID, trx5CID cid.Cid
rct1CID, rct2CID, rct3CID, rct4CID, rct5CID cid.Cid rct1CID, rct2CID, rct3CID, rct4CID, rct5CID cid.Cid
@ -65,7 +67,7 @@ func init() {
} }
mockBlock = mocks.MockBlock mockBlock = mocks.MockBlock
txs, rcts := mocks.MockBlock.Transactions(), mocks.MockReceipts txs, rcts = mocks.MockBlock.Transactions(), mocks.MockReceipts
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
txs.EncodeIndex(0, buf) 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, trx4CID.String()))
expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String()))
// and published // and published
type txResult struct {
TxType uint8 `db:"tx_type"`
Value string
}
for _, c := range trxs { for _, c := range trxs {
dc, err := cid.Decode(c) dc, err := cid.Decode(c)
if err != nil { if err != nil {
@ -243,47 +249,59 @@ func TestFileIndexer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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 { switch c {
case trx1CID.String(): case trx1CID.String():
test_helpers.ExpectEqual(t, data, tx1) test_helpers.ExpectEqual(t, data, tx1)
var txType uint8 txRes := new(txResult)
err = sqlxdb.Get(&txType, txTypePgStr, c) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected LegacyTxType (0), got %d", txType) 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(): case trx2CID.String():
test_helpers.ExpectEqual(t, data, tx2) test_helpers.ExpectEqual(t, data, tx2)
var txType uint8 txRes := new(txResult)
err = sqlxdb.Get(&txType, txTypePgStr, c) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected LegacyTxType (0), got %d", txType) 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(): case trx3CID.String():
test_helpers.ExpectEqual(t, data, tx3) test_helpers.ExpectEqual(t, data, tx3)
var txType uint8 txRes := new(txResult)
err = sqlxdb.Get(&txType, txTypePgStr, c) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected LegacyTxType (0), got %d", txType) 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(): case trx4CID.String():
test_helpers.ExpectEqual(t, data, tx4) test_helpers.ExpectEqual(t, data, tx4)
var txType uint8 txRes := new(txResult)
err = sqlxdb.Get(&txType, txTypePgStr, c) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != types.AccessListTxType { if txRes.TxType != types.AccessListTxType {
t.Fatalf("expected AccessListTxType (1), got %d", txType) 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) 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` 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) test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model)
case trx5CID.String(): case trx5CID.String():
test_helpers.ExpectEqual(t, data, tx5) test_helpers.ExpectEqual(t, data, tx5)
var txType *uint8 txRes := new(txResult)
err = sqlxdb.Get(&txType, txTypePgStr, c) err = sqlxdb.QueryRowx(txTypeAndValueStr, c).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if *txType != types.DynamicFeeTxType { if txRes.TxType != types.DynamicFeeTxType {
t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType) 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)
} }
} }
} }

View File

@ -207,6 +207,11 @@ func TestPGXIndexer(t *testing.T) {
expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String()))
expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String()))
// and published // and published
transactions := mocks.MockBlock.Transactions()
type txResult struct {
TxType uint8 `db:"tx_type"`
Value string
}
for _, c := range trxs { for _, c := range trxs {
dc, err := cid.Decode(c) dc, err := cid.Decode(c)
if err != nil { if err != nil {
@ -219,47 +224,59 @@ func TestPGXIndexer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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 { switch c {
case trx1CID.String(): case trx1CID.String():
test_helpers.ExpectEqual(t, data, tx1) test_helpers.ExpectEqual(t, data, tx1)
var txType uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected tx_type 0, got %d", txType) 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(): case trx2CID.String():
test_helpers.ExpectEqual(t, data, tx2) test_helpers.ExpectEqual(t, data, tx2)
var txType uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected tx_type 0, got %d", txType) 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(): case trx3CID.String():
test_helpers.ExpectEqual(t, data, tx3) test_helpers.ExpectEqual(t, data, tx3)
var txType uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected tx_type 0, got %d", txType) 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(): case trx4CID.String():
test_helpers.ExpectEqual(t, data, tx4) test_helpers.ExpectEqual(t, data, tx4)
var txType uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != types.AccessListTxType { if txRes.TxType != types.AccessListTxType {
t.Fatalf("expected AccessListTxType (1), got %d", txType) 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) 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` 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) test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model)
case trx5CID.String(): case trx5CID.String():
test_helpers.ExpectEqual(t, data, tx5) test_helpers.ExpectEqual(t, data, tx5)
var txType *uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).Scan(&txRes.TxType, &txRes.Value)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if *txType != types.DynamicFeeTxType { if txRes.TxType != types.DynamicFeeTxType {
t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType) 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)
} }
} }
} }

View File

@ -229,6 +229,11 @@ func TestSQLXIndexer(t *testing.T) {
expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx4CID.String()))
expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String())) expectTrue(t, test_helpers.ListContainsString(trxs, trx5CID.String()))
// and published // and published
transactions := mocks.MockBlock.Transactions()
type txResult struct {
TxType uint8 `db:"tx_type"`
Value string
}
for _, c := range trxs { for _, c := range trxs {
dc, err := cid.Decode(c) dc, err := cid.Decode(c)
if err != nil { if err != nil {
@ -241,47 +246,59 @@ func TestSQLXIndexer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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 { switch c {
case trx1CID.String(): case trx1CID.String():
test_helpers.ExpectEqual(t, data, tx1) test_helpers.ExpectEqual(t, data, tx1)
var txType uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected LegacyTxType (0), got %d", txType) 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(): case trx2CID.String():
test_helpers.ExpectEqual(t, data, tx2) test_helpers.ExpectEqual(t, data, tx2)
var txType uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected LegacyTxType (0), got %d", txType) 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(): case trx3CID.String():
test_helpers.ExpectEqual(t, data, tx3) test_helpers.ExpectEqual(t, data, tx3)
var txType uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != 0 { if txRes.TxType != 0 {
t.Fatalf("expected LegacyTxType (0), got %d", txType) 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(): case trx4CID.String():
test_helpers.ExpectEqual(t, data, tx4) test_helpers.ExpectEqual(t, data, tx4)
var txType uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if txType != types.AccessListTxType { if txRes.TxType != types.AccessListTxType {
t.Fatalf("expected AccessListTxType (1), got %d", txType) 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) 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` 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) test_helpers.ExpectEqual(t, model2, mocks.AccessListEntry2Model)
case trx5CID.String(): case trx5CID.String():
test_helpers.ExpectEqual(t, data, tx5) test_helpers.ExpectEqual(t, data, tx5)
var txType *uint8 txRes := new(txResult)
err = db.Get(context.Background(), &txType, txTypePgStr, c) err = db.QueryRow(context.Background(), txTypeAndValueStr, c).(*sqlx.Row).StructScan(txRes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if *txType != types.DynamicFeeTxType { if txRes.TxType != types.DynamicFeeTxType {
t.Fatalf("expected DynamicFeeTxType (2), got %d", *txType) 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)
} }
} }
} }

View File

@ -308,7 +308,7 @@ func createTransactionsAndReceipts(config *params.ChainConfig, blockNumber *big.
GasPrice: big.NewInt(100), GasPrice: big.NewInt(100),
Gas: 50, Gas: 50,
To: &AnotherAddress, To: &AnotherAddress,
Value: big.NewInt(1000), Value: big.NewInt(999),
Data: []byte{}, Data: []byte{},
AccessList: types.AccessList{ AccessList: types.AccessList{
AccessListEntry1, AccessListEntry1,