Skip to content

Commit

Permalink
feat: use pgtesting from go libs (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed May 12, 2023
1 parent 8b8c8c6 commit 9a0a03d
Show file tree
Hide file tree
Showing 24 changed files with 188 additions and 314 deletions.
3 changes: 2 additions & 1 deletion cmd/internal/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ func TestAnalyticsModule(t *testing.T) {
module := NewAnalyticsModule(v, "1.0.0")
app := fx.New(
module,
fx.NopLogger,
fx.Provide(func(lc fx.Lifecycle) (storage.Driver[ledger.Store], error) {
driver, stopFn, err := ledgertesting.StorageDriver()
driver, stopFn, err := ledgertesting.StorageDriver(t)
if err != nil {
return nil, err
}
Expand Down
21 changes: 21 additions & 0 deletions cmd/internal/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package internal

import (
"os"
"testing"

"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/pgtesting"
)

func TestMain(t *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
logging.Error(err)
os.Exit(1)
}
code := t.Run()
if err := pgtesting.DestroyPostgresServer(); err != nil {
logging.Error(err)
}
os.Exit(code)
}
134 changes: 0 additions & 134 deletions cmd/root_test.go

This file was deleted.

2 changes: 2 additions & 0 deletions cmd/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func Test_StorageCommands(t *testing.T) {
db := pgtesting.NewPostgresDatabase(t)

viper.Set(storagePostgresConnectionStringFlag, db.ConnString())
viper.Set(cacheMaxNumKeys, 10)
viper.Set(cacheCapacityBytes, 100)

require.NoError(t, NewStorageList().Execute())

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/lib/pq v1.10.7
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mitchellh/mapstructure v1.5.0
github.com/ory/dockertest/v3 v3.9.1
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -98,6 +97,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/ory/dockertest/v3 v3.9.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
84 changes: 0 additions & 84 deletions internal/pgtesting/testing.go

This file was deleted.

21 changes: 21 additions & 0 deletions pkg/analytics/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package analytics

import (
"os"
"testing"

"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/pgtesting"
)

func TestMain(t *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
logging.Error(err)
os.Exit(1)
}
code := t.Run()
if err := pgtesting.DestroyPostgresServer(); err != nil {
logging.Error(err)
}
os.Exit(code)
}
14 changes: 8 additions & 6 deletions pkg/analytics/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/formancehq/ledger/pkg/ledgertesting"
"github.com/formancehq/ledger/pkg/storage"
"github.com/formancehq/ledger/pkg/storage/sqlstorage"
"github.com/formancehq/stack/libs/go-libs/pgtesting"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
"gopkg.in/segmentio/analytics-go.v3"
Expand Down Expand Up @@ -77,16 +78,17 @@ const (
writeKey = "key"
)

var (
module = fx.Options(
func module(t pgtesting.TestingT) fx.Option {
return fx.Options(
NewHeartbeatModule(version, writeKey, interval),
fx.NopLogger,
fx.Provide(func() AppIdProvider {
return AppIdProviderFn(func(ctx context.Context) (string, error) {
return "foo", nil
})
}),
fx.Provide(func(lc fx.Lifecycle) (storage.Driver[ledger.Store], error) {
driver, stopFn, err := ledgertesting.StorageDriver()
driver, stopFn, err := ledgertesting.StorageDriver(t)
if err != nil {
return nil, err
}
Expand All @@ -100,7 +102,7 @@ var (
return sqlstorage.NewLedgerStorageDriverFromRawDriver(driver), nil
}),
)
)
}

func EventuallyQueueNotEmpty[ITEM any](t *testing.T, queue *Queue[ITEM]) {
require.Eventually(t, func() bool {
Expand Down Expand Up @@ -132,7 +134,7 @@ func TestSegment(t *testing.T) {

t.Run("Nominal case", func(t *testing.T) {
queue := NewQueue[*http.Request]()
app := newApp(module, func(request *http.Request) (*http.Response, error) {
app := newApp(module(t), func(request *http.Request) (*http.Response, error) {
queue.Put(request)
return emptyHttpResponse, nil
})
Expand Down Expand Up @@ -162,7 +164,7 @@ func TestSegment(t *testing.T) {
firstCallChan := make(chan struct{})

queue := NewQueue[*http.Request]()
app := newApp(module, func(request *http.Request) (*http.Response, error) {
app := newApp(module(t), func(request *http.Request) (*http.Response, error) {
select {
case <-firstCallChan: // Enter this case only if the chan is closed
queue.Put(request)
Expand Down
21 changes: 21 additions & 0 deletions pkg/api/controllers/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package controllers_test

import (
"os"
"testing"

"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/pgtesting"
)

func TestMain(t *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
logging.Error(err)
os.Exit(1)
}
code := t.Run()
if err := pgtesting.DestroyPostgresServer(); err != nil {
logging.Error(err)
}
os.Exit(code)
}
Loading

0 comments on commit 9a0a03d

Please sign in to comment.