Add test utilities for dependents #30

Merged
roysc merged 5 commits from utils-for-server-tests into main 2024-07-13 11:09:40 +00:00
2 changed files with 11 additions and 42 deletions
Showing only changes of commit c31b1f30ed - Show all commits

View File

@ -19,6 +19,7 @@ package test_helpers
import ( import (
"bufio" "bufio"
"context" "context"
"fmt"
"os" "os"
"testing" "testing"
@ -39,9 +40,6 @@ func DedupFile(filePath string) error {
s := sc.Text() s := sc.Text()
stmts[s] = struct{}{} stmts[s] = struct{}{}
} }
if err != nil {
return err
}
f.Close() f.Close()
@ -60,10 +58,17 @@ func DedupFile(filePath string) error {
// TearDownDB is used to tear down the watcher dbs after tests // TearDownDB is used to tear down the watcher dbs after tests
func TearDownDB(t *testing.T, db sql.Database) { func TearDownDB(t *testing.T, db sql.Database) {
err := ClearDB(db)
if err != nil {
t.Fatal(err)
}
}
func ClearDB(db sql.Database) error {
ctx := context.Background() ctx := context.Background()
tx, err := db.Begin(ctx) tx, err := db.Begin(ctx)
if err != nil { if err != nil {
t.Fatal(err) return err
} }
statements := []string{ statements := []string{
@ -81,10 +86,8 @@ func TearDownDB(t *testing.T, db sql.Database) {
} }
for _, stm := range statements { for _, stm := range statements {
if _, err = tx.Exec(ctx, stm); err != nil { if _, err = tx.Exec(ctx, stm); err != nil {
t.Fatal(err) return fmt.Errorf("error executing `%s`: %w", stm, err)
} }
} }
if err = tx.Commit(ctx); err != nil { return tx.Commit(ctx)
t.Fatal(err)
}
} }

View File

@ -1,34 +0,0 @@
package test_helpers
import (
"fmt"
"github.com/jmoiron/sqlx"
)
// ClearDB is used to empty the IPLD-ETH tables after tests
func ClearDB(db *sqlx.DB) error {
tx, err := db.Beginx()
if err != nil {
return err
}
statements := []string{
`TRUNCATE nodes`,
`TRUNCATE ipld.blocks`,
`TRUNCATE eth.header_cids`,
`TRUNCATE eth.uncle_cids`,
`TRUNCATE eth.transaction_cids`,
`TRUNCATE eth.receipt_cids`,
`TRUNCATE eth.state_cids`,
`TRUNCATE eth.storage_cids`,
`TRUNCATE eth.log_cids`,
`TRUNCATE eth.withdrawal_cids`,
`TRUNCATE eth_meta.watched_addresses`,
}
for _, stm := range statements {
if _, err = tx.Exec(stm); err != nil {
return fmt.Errorf("error executing `%s`: %w", stm, err)
}
}
return tx.Commit()
}