Skip to content

Commit

Permalink
feat: optimize storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 24, 2024
1 parent cbfaa80 commit bc7ec17
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ github.com/formancehq/go-libs/v2 v2.0.1-0.20241017153232-1a62cecf1a61 h1:GSIhsdo
github.com/formancehq/go-libs/v2 v2.0.1-0.20241017153232-1a62cecf1a61/go.mod h1:LgxayMN6wgAQbkB3ioBDTHOVMKp1rC6Q55M1CvG44xY=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241021110602-fbd3b37b93f8 h1:q9mP1jT2q2/QxGdEg9mWOcsf/P7NNvC1vMmyDvj5EEY=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241021110602-fbd3b37b93f8/go.mod h1:KO+eOrTVQ5tR3TZUAHapoQ+d7y2+Ie5Tg0QwfZHAK4k=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241021183239-813f4dc647a1 h1:PnDpgyTBicMbSC/c7PTdYaNZCCHlnKyVXURo4VTJyCc=
github.com/formancehq/go-libs/v2 v2.0.1-0.20241021183239-813f4dc647a1/go.mod h1:LgxayMN6wgAQbkB3ioBDTHOVMKp1rC6Q55M1CvG44xY=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
Expand Down
24 changes: 15 additions & 9 deletions internal/storage/ledger/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ package ledger_test

import (
"database/sql"
"github.com/formancehq/go-libs/v2/bun/bunconnect"
. "github.com/formancehq/go-libs/v2/testing/utils"
systemstore "github.com/formancehq/ledger/internal/storage/driver"
"github.com/formancehq/ledger/internal/storage/driver"
ledgerstore "github.com/formancehq/ledger/internal/storage/ledger"
"go.opentelemetry.io/otel/trace/noop"
"math/big"
"os"
"sync/atomic"
"testing"

"github.com/formancehq/go-libs/v2/bun/bunconnect"
"github.com/formancehq/go-libs/v2/bun/bundebug"
"github.com/formancehq/go-libs/v2/testing/docker"
ledger "github.com/formancehq/ledger/internal"
Expand All @@ -30,8 +31,9 @@ import (
)

var (
srv = NewDeferred[*pgtesting.PostgresServer]()
bunDB = NewDeferred[*bun.DB]()
srv = NewDeferred[*pgtesting.PostgresServer]()
bunDB = NewDeferred[*bun.DB]()
ledgerCount = atomic.Int64{}
)

func TestMain(m *testing.M) {
Expand All @@ -43,10 +45,13 @@ func TestMain(m *testing.M) {
db, err := sql.Open("pgx", ret.GetDSN())
require.NoError(t, err)

bunDB := bun.NewDB(db, pgdialect.New())
bunDB := bun.NewDB(db, pgdialect.New(), bun.WithDiscardUnknownColumns())
if os.Getenv("DEBUG") == "true" {
bunDB.AddQueryHook(bundebug.NewQueryHook())
}
bunDB.SetMaxOpenConns(100)

require.NoError(t, driver.Migrate(logging.TestingContext(), bunDB))

return bunDB
})
Expand Down Expand Up @@ -81,16 +86,17 @@ func newLedgerStore(t T) *ledgerstore.Store {
db, err := bunconnect.OpenSQLDB(ctx, pgDatabase.ConnectionOptions(), hooks...)
require.NoError(t, err)

require.NoError(t, systemstore.Migrate(ctx, db))
require.NoError(t, driver.Migrate(ctx, db))

l := ledger.MustNewWithDefault(ledgerName)
l.Bucket = ledgerName
l.ID = int(ledgerCount.Add(1))

b := bucket.New(db, ledgerName)
b := bucket.New(bunDB.GetValue(), ledgerName)
require.NoError(t, b.Migrate(ctx, noop.Tracer{}))
require.NoError(t, b.AddLedger(ctx, l, db))
require.NoError(t, b.AddLedger(ctx, l, bunDB.GetValue()))

return ledgerstore.New(db, b, l)
return ledgerstore.New(bunDB.GetValue(), b, l)
}

func bigIntComparer(v1 *big.Int, v2 *big.Int) bool {
Expand Down

0 comments on commit bc7ec17

Please sign in to comment.