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