Skip to content

Commit

Permalink
chore: extract helper in go libs
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent e5f0257 commit e4e0caf
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 66 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/alitto/pond v1.9.2
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10
github.com/bluele/gcache v0.0.2
github.com/formancehq/go-libs v1.7.2-0.20240930151849-9e7ee54046e9
github.com/formancehq/go-libs v1.7.2-0.20241001151743-2bd31525c7d1
github.com/formancehq/stack/ledger/client v0.0.0-00010101000000-000000000000
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/cors v1.2.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ github.com/formancehq/go-libs v1.7.2-0.20240927143916-90a2f07df259 h1:UNR167W1JQ
github.com/formancehq/go-libs v1.7.2-0.20240927143916-90a2f07df259/go.mod h1:oLMVltNsXmvqLA0Ox5NSwVSeH8gbnG2ZUo6Lchp23XU=
github.com/formancehq/go-libs v1.7.2-0.20240930151849-9e7ee54046e9 h1:l9ZGJmrgHPww1eyPhpo4jnv4Bjwz2o/Wvt0Fjh//gYg=
github.com/formancehq/go-libs v1.7.2-0.20240930151849-9e7ee54046e9/go.mod h1:oLMVltNsXmvqLA0Ox5NSwVSeH8gbnG2ZUo6Lchp23XU=
github.com/formancehq/go-libs v1.7.2-0.20241001142646-eb9c0c1cd58a h1:TkUCCiZjo7uQ1vY9E2RLaA2zxtEjnodSL9QILDyBoR0=
github.com/formancehq/go-libs v1.7.2-0.20241001142646-eb9c0c1cd58a/go.mod h1:oLMVltNsXmvqLA0Ox5NSwVSeH8gbnG2ZUo6Lchp23XU=
github.com/formancehq/go-libs v1.7.2-0.20241001151743-2bd31525c7d1 h1:/Zo3e6njdSqIwi4nI038P2fTvzcfRQ6WaPmFzSI7s8o=
github.com/formancehq/go-libs v1.7.2-0.20241001151743-2bd31525c7d1/go.mod h1:oLMVltNsXmvqLA0Ox5NSwVSeH8gbnG2ZUo6Lchp23XU=
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
4 changes: 1 addition & 3 deletions internal/storage/bucket/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ func TestBuckets(t *testing.T) {
ctx := logging.TestingContext()
name := uuid.NewString()[:8]

<-srv.Done()

pgDatabase := srv.GetValue().NewDatabase(t)
pgDatabase := srv.NewDatabase(t)
db, err := bunconnect.OpenSQLDB(ctx, pgDatabase.ConnectionOptions())
require.NoError(t, err)

Expand Down
6 changes: 2 additions & 4 deletions internal/storage/bucket/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import (
)

var (
srv = NewDeferred[*pgtesting.PostgresServer]()
srv *pgtesting.PostgresServer
)

func TestMain(m *testing.M) {
WithTestMain(func(t *TestingTForMain) int {
srv.LoadAsync(func() *pgtesting.PostgresServer {
return pgtesting.CreatePostgresServer(t, docker.NewPool(t, logging.Testing()))
})
srv = pgtesting.CreatePostgresServer(t, docker.NewPool(t, logging.Testing()))

return m.Run()
})
Expand Down
47 changes: 47 additions & 0 deletions internal/storage/bucket/migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build it

package bucket_test

import (
"context"
"github.com/formancehq/go-libs/testing/migrations"
"github.com/formancehq/ledger/internal/storage/driver"
"testing"

"github.com/formancehq/go-libs/bun/bunconnect"
"github.com/formancehq/go-libs/bun/bundebug"
"github.com/formancehq/go-libs/logging"
"github.com/stretchr/testify/require"
"github.com/uptrace/bun"
)

func TestMigrations(t *testing.T) {
t.Parallel()
ctx := logging.TestingContext()

pgServer := srv.NewDatabase(t)

hooks := make([]bun.QueryHook, 0)
if testing.Verbose() {
hooks = append(hooks, bundebug.NewQueryHook())
}

db, err := bunconnect.OpenSQLDB(ctx, pgServer.ConnectionOptions(), hooks...)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, db.Close())
})

test := migrations.NewMigrationTest(t, driver.GetMigrator(), db)
test.Append(8, removeSequenceOnTransactionTable)
test.Run()
}

var removeSequenceOnTransactionTable = migrations.Hook{
Before: func(ctx context.Context, t *testing.T, db bun.IDB) {

},
After: func(ctx context.Context, t *testing.T, db bun.IDB) {

},
}
61 changes: 3 additions & 58 deletions internal/storage/driver/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,19 @@ package driver_test
import (
"context"
"fmt"
"github.com/formancehq/go-libs/testing/migrations"
"github.com/formancehq/ledger/internal/storage/driver"
"testing"

"github.com/formancehq/go-libs/bun/bunconnect"
"github.com/formancehq/go-libs/bun/bundebug"
"github.com/formancehq/go-libs/logging"
"github.com/formancehq/go-libs/migrations"
"github.com/formancehq/go-libs/time"
ledger "github.com/formancehq/ledger/internal"
"github.com/stretchr/testify/require"
"github.com/uptrace/bun"
)

type HookFn func(ctx context.Context, t *testing.T, db bun.IDB)

type Hook struct {
Before HookFn
After HookFn
}

// todo(libs): export in go libs
type MigrationTest struct {
migrator *migrations.Migrator
hooks map[int][]Hook
db bun.IDB
t *testing.T
}

func (mt *MigrationTest) Run() {
ctx := logging.TestingContext()
i := 0
for {
for _, hook := range mt.hooks[i] {
if hook.Before != nil {
hook.Before(ctx, mt.t, mt.db)
}
}

more, err := mt.migrator.UpByOne(ctx, mt.db)
require.NoError(mt.t, err)

for _, hook := range mt.hooks[i] {
if hook.After != nil {
hook.After(ctx, mt.t, mt.db)
}
}

i++

if !more {
break
}
}
}

func (mt *MigrationTest) Append(i int, hook Hook) {
mt.hooks[i] = append(mt.hooks[i], hook)
}

func NewMigrationTest(t *testing.T, migrator *migrations.Migrator, db bun.IDB) *MigrationTest {
return &MigrationTest{
migrator: migrator,
hooks: map[int][]Hook{},
t: t,
db: db,
}
}

func TestMigrations(t *testing.T) {
t.Parallel()
ctx := logging.TestingContext()
Expand All @@ -90,12 +35,12 @@ func TestMigrations(t *testing.T) {
require.NoError(t, db.Close())
})

test := NewMigrationTest(t, driver.GetMigrator(), db)
test := migrations.NewMigrationTest(t, driver.GetMigrator(), db)
test.Append(8, addIdOnLedgerTable)
test.Run()
}

var addIdOnLedgerTable = Hook{
var addIdOnLedgerTable = migrations.Hook{
Before: func(ctx context.Context, t *testing.T, db bun.IDB) {
for i := 0; i < 3; i++ {
_, err := db.NewInsert().
Expand Down

0 comments on commit e4e0caf

Please sign in to comment.