arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/dbUtils.go
test/dbUtils.goRaw
 1package test
 2
 3import (
 4	"testing"
 5
 6	dbUtils "github.com/hermeznetwork/hermez-node/db"
 7	"github.com/jmoiron/sqlx"
 8	"github.com/stretchr/testify/assert"
 9)
10
11// AssertUSD asserts pointers to float64, and checks that they are equal
12// with a tolerance of 0.01%. After that, the actual value is setted to the expected value
13// in order to be able to perform further assertions using the standar assert functions.
14func AssertUSD(t *testing.T, expected, actual *float64) {
15	if actual == nil {
16		assert.Equal(t, expected, actual)
17		return
18	}
19	if *expected < *actual {
20		assert.InEpsilon(t, *actual, *expected, 0.0001)
21	} else if *expected > *actual {
22		assert.InEpsilon(t, *expected, *actual, 0.0001)
23	}
24	*expected = *actual
25}
26
27// WipeDB redo all the migrations of the SQL DB (HistoryDB and L2DB),
28// efectively recreating the original state
29func WipeDB(db *sqlx.DB) {
30	if err := dbUtils.MigrationsDown(db.DB); err != nil {
31		panic(err)
32	}
33	if err := dbUtils.MigrationsUp(db.DB); err != nil {
34		panic(err)
35	}
36}