Skip to content

Commit

Permalink
Test transaction insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Sep 6, 2024
1 parent 398f85b commit bcdfcb2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/sqlite/migrations/202409061249_bootstrapDb/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (m *SqliteMigration) Up(grm *gorm.DB) error {
)`,
`CREATE TABLE IF NOT EXISTS transactions (
block_number INTEGER NOT NULL REFERENCES blocks(number) ON DELETE CASCADE,
transaction_hash TEXT NOT NULL,
transaction_hash TEXT NOT NULL PRIMARY KEY,
transaction_index INTEGER NOT NULL,
from_address TEXT NOT NULL,
to_address TEXT DEFAULT NULL,
Expand Down
69 changes: 66 additions & 3 deletions internal/storage/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -44,10 +45,13 @@ func teardown(db *gorm.DB, l *zap.Logger) {
}

func Test_SqliteBlockstore(t *testing.T) {
t.Run("Blocks", func(t *testing.T) {
db, l, cfg := setup()
db, l, cfg := setup()
sqliteStore := NewSqliteBlockStore(db, l, cfg)

insertedBlocks := make([]*storage.Block, 0)
insertedTransactions := make([]*storage.Transaction, 0)

sqliteStore := NewSqliteBlockStore(db, l, cfg)
t.Run("Blocks", func(t *testing.T) {

t.Run("InsertBlockAtHeight", func(t *testing.T) {
block := &storage.Block{
Expand All @@ -63,6 +67,8 @@ func Test_SqliteBlockstore(t *testing.T) {
assert.NotNil(t, insertedBlock)
assert.Equal(t, block.Number, insertedBlock.Number)
assert.Equal(t, block.Hash, insertedBlock.Hash)

insertedBlocks = append(insertedBlocks, insertedBlock)
})
t.Run("Fail to insert a duplicate block", func(t *testing.T) {
block := &storage.Block{
Expand All @@ -76,8 +82,65 @@ func Test_SqliteBlockstore(t *testing.T) {
assert.Contains(t, err.Error(), "UNIQUE constraint failed")
fmt.Printf("Error: %v\n", err)
})
})
t.Run("Transactions", func(t *testing.T) {
block := insertedBlocks[0]

t.Run("InsertBlockTransaction", func(t *testing.T) {
tx := storage.Transaction{
BlockNumber: block.Number,
TransactionHash: "txHash",
TransactionIndex: 0,
FromAddress: "from",
ToAddress: "to",
ContractAddress: "contractAddress",
BytecodeHash: "bytecodeHash",
}
insertedTx, err := sqliteStore.InsertBlockTransaction(
tx.BlockNumber,
tx.TransactionHash,
tx.TransactionIndex,
tx.FromAddress,
tx.ToAddress,
tx.ContractAddress,
tx.BytecodeHash,
)
assert.Nil(t, err)
assert.NotNil(t, insertedTx)
assert.Equal(t, tx.BlockNumber, insertedTx.BlockNumber)
assert.Equal(t, tx.TransactionHash, insertedTx.TransactionHash)
assert.Equal(t, tx.TransactionIndex, insertedTx.TransactionIndex)
assert.Equal(t, tx.FromAddress, insertedTx.FromAddress)
assert.Equal(t, tx.ToAddress, insertedTx.ToAddress)
assert.Equal(t, strings.ToLower(tx.ContractAddress), insertedTx.ContractAddress)
assert.Equal(t, tx.BytecodeHash, insertedTx.BytecodeHash)

insertedTransactions = append(insertedTransactions, insertedTx)
})
t.Run("Fail to insert a duplicate transaction", func(t *testing.T) {
tx := storage.Transaction{
BlockNumber: block.Number,
TransactionHash: "txHash",
TransactionIndex: 0,
FromAddress: "from",
ToAddress: "to",
ContractAddress: "contractAddress",
BytecodeHash: "bytecodeHash",
}
_, err := sqliteStore.InsertBlockTransaction(
tx.BlockNumber,
tx.TransactionHash,
tx.TransactionIndex,
tx.FromAddress,
tx.ToAddress,
tx.ContractAddress,
tx.BytecodeHash,
)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "UNIQUE constraint failed")
})
})
t.Run("TransactionLogs", func(t *testing.T) {

})
}

0 comments on commit bcdfcb2

Please sign in to comment.