Skip to content

Commit

Permalink
Refactor indexer tests to avoid duplicate code (#270)
Browse files Browse the repository at this point in the history
* Refactor indexer tests to avoid duplicate code

* Refactor file mode indexer tests

* Fix expected db stats for sqlx after tx closure

* Refactor indexer tests for legacy block

* Refactor mainnet indexer tests

* Refactor tests for watched addressess methods

* Fix query in legacy indexer test
  • Loading branch information
prathamesh0 authored Aug 16, 2022
1 parent 88a24ad commit be1757b
Show file tree
Hide file tree
Showing 22 changed files with 2,609 additions and 4,136 deletions.
77 changes: 30 additions & 47 deletions statediff/indexer/database/file/csv_indexer_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,66 +25,49 @@ import (
"strings"
"testing"

"github.com/jmoiron/sqlx"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/statediff/indexer/database/file"
"github.com/ethereum/go-ethereum/statediff/indexer/database/file/types"
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
"github.com/ethereum/go-ethereum/statediff/indexer/interfaces"
"github.com/ethereum/go-ethereum/statediff/indexer/ipld"
"github.com/ethereum/go-ethereum/statediff/indexer/test"
"github.com/ethereum/go-ethereum/statediff/indexer/test_helpers"
)

const dbDirectory = "/file_indexer"
const pgCopyStatement = `COPY %s FROM '%s' CSV`

func setupCSVLegacy(t *testing.T) {
mockLegacyBlock = legacyData.MockBlock
legacyHeaderCID, _ = ipld.RawdataToCid(ipld.MEthHeader, legacyData.MockHeaderRlp, multihash.KECCAK_256)
file.CSVTestConfig.OutputDir = "./statediffing_legacy_test"

func setupLegacyCSVIndexer(t *testing.T) {
if _, err := os.Stat(file.CSVTestConfig.OutputDir); !errors.Is(err, os.ErrNotExist) {
err := os.RemoveAll(file.CSVTestConfig.OutputDir)
require.NoError(t, err)
}

ind, err := file.NewStateDiffIndexer(context.Background(), legacyData.Config, file.CSVTestConfig)
require.NoError(t, err)
var tx interfaces.Batch
tx, err = ind.PushBlock(
mockLegacyBlock,
legacyData.MockReceipts,
legacyData.MockBlock.Difficulty())
ind, err = file.NewStateDiffIndexer(context.Background(), test.LegacyConfig, file.CSVTestConfig)
require.NoError(t, err)

defer func() {
if err := tx.Submit(err); err != nil {
t.Fatal(err)
}
if err := ind.Close(); err != nil {
t.Fatal(err)
}
}()

for _, node := range legacyData.StateDiffs {
err = ind.PushStateNode(tx, node, legacyData.MockBlock.Hash().String())
require.NoError(t, err)
}

require.Equal(t, legacyData.BlockNumber.String(), tx.(*file.BatchTx).BlockNumber)

connStr := postgres.DefaultConfig.DbConnectionString()
sqlxdb, err = sqlx.Connect("postgres", connStr)
db, err = postgres.SetupSQLXDB()
if err != nil {
t.Fatalf("failed to connect to db with connection string: %s err: %v", connStr, err)
t.Fatal(err)
}
}

func setupLegacyCSV(t *testing.T) {
setupLegacyCSVIndexer(t)
test.SetupLegacyTestData(t, ind)
}

func dumpCSVFileData(t *testing.T) {
outputDir := filepath.Join(dbDirectory, file.CSVTestConfig.OutputDir)
workingDir, err := os.Getwd()
require.NoError(t, err)

localOutputDir := filepath.Join(workingDir, file.CSVTestConfig.OutputDir)

for _, tbl := range file.Tables {
err := test_helpers.DedupFile(file.TableFilePath(localOutputDir, tbl.Name))
require.NoError(t, err)

var stmt string
varcharColumns := tbl.VarcharColumns()
if len(varcharColumns) > 0 {
Expand All @@ -98,38 +81,38 @@ func dumpCSVFileData(t *testing.T) {
stmt = fmt.Sprintf(pgCopyStatement, tbl.Name, file.TableFilePath(outputDir, tbl.Name))
}

_, err = sqlxdb.Exec(stmt)
_, err = db.Exec(context.Background(), stmt)
require.NoError(t, err)
}
}

func dumpWatchedAddressesCSVFileData(t *testing.T) {
func resetAndDumpWatchedAddressesCSVFileData(t *testing.T) {
test_helpers.TearDownDB(t, db)

outputFilePath := filepath.Join(dbDirectory, file.CSVTestConfig.WatchedAddressesFilePath)
stmt := fmt.Sprintf(pgCopyStatement, types.TableWatchedAddresses.Name, outputFilePath)

_, err = sqlxdb.Exec(stmt)
_, err = db.Exec(context.Background(), stmt)
require.NoError(t, err)
}

func tearDownCSV(t *testing.T) {
file.TearDownDB(t, sqlxdb)
test_helpers.TearDownDB(t, db)
require.NoError(t, db.Close())

err := os.RemoveAll(file.CSVTestConfig.OutputDir)
require.NoError(t, err)
require.NoError(t, os.RemoveAll(file.CSVTestConfig.OutputDir))

if err := os.Remove(file.CSVTestConfig.WatchedAddressesFilePath); !errors.Is(err, os.ErrNotExist) {
require.NoError(t, err)
}

err = sqlxdb.Close()
require.NoError(t, err)
}

func TestCSVFileIndexerLegacy(t *testing.T) {
func TestLegacyCSVFileIndexer(t *testing.T) {
t.Run("Publish and index header IPLDs", func(t *testing.T) {
setupCSVLegacy(t)
setupLegacyCSV(t)
dumpCSVFileData(t)
defer tearDownCSV(t)
testLegacyPublishAndIndexHeaderIPLDs(t)

test.TestLegacyIndexer(t, db)
})
}
Loading

0 comments on commit be1757b

Please sign in to comment.