Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
chore(BUX-000): clean tests logs - use Nop logger
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Dec 13, 2023
1 parent 6acfb97 commit f0eb1c0
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 30 deletions.
7 changes: 7 additions & 0 deletions bux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/mrz1836/go-cache"
"github.com/mrz1836/go-datastore"
"github.com/rafaeljusto/redigomock"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -83,9 +84,12 @@ func DefaultClientOpts(debug, shared bool) []ClientOps {
func CreateTestSQLiteClient(t *testing.T, debug, shared bool, clientOpts ...ClientOps) (context.Context, ClientInterface, func()) {
ctx := tester.GetNewRelicCtx(t, "app-test", "test-transaction")

logger := zerolog.Nop()

// Set the default options, add migrate models
opts := DefaultClientOpts(debug, shared)
opts = append(opts, WithAutoMigrate(BaseModels...))
opts = append(opts, WithLogger(&logger))
opts = append(opts, clientOpts...)

// Create the client
Expand All @@ -106,9 +110,12 @@ func CreateTestSQLiteClient(t *testing.T, debug, shared bool, clientOpts ...Clie
func CreateBenchmarkSQLiteClient(b *testing.B, debug, shared bool, clientOpts ...ClientOps) (context.Context, ClientInterface, func()) {
ctx := context.Background()

logger := zerolog.Nop()

// Set the default options, add migrate models
opts := DefaultClientOpts(debug, shared)
opts = append(opts, WithAutoMigrate(BaseModels...))
opts = append(opts, WithLogger(&logger))
opts = append(opts, clientOpts...)

// Create the client
Expand Down
4 changes: 3 additions & 1 deletion chainstate/chainstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"testing"
"time"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// NewTestClient returns a test client
func NewTestClient(ctx context.Context, t *testing.T, opts ...ClientOps) ClientInterface {
logger := zerolog.Nop()
c, err := NewClient(
ctx, append(opts, WithDebugging())...,
ctx, append(opts, WithDebugging(), WithLogger(&logger))...,
)
require.NoError(t, err)
require.NotNil(t, c)
Expand Down
8 changes: 4 additions & 4 deletions chainstate/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"
"time"

"github.com/BuxOrg/bux/logging"
broadcast_client_mock "github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client-mock"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -305,10 +305,10 @@ func TestWithLogger(t *testing.T) {
options := &clientOptions{
config: &syncConfig{},
}
customLogger := logging.GetDefaultLogger()
opt := WithLogger(customLogger)
customLogger := zerolog.Nop()
opt := WithLogger(&customLogger)
opt(options)
assert.Equal(t, customLogger, options.logger)
assert.Equal(t, &customLogger, options.logger)
})
}

Expand Down
42 changes: 31 additions & 11 deletions client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/mrz1836/go-cachestore"
"github.com/mrz1836/go-datastore"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -70,8 +71,9 @@ func Test_newRelicOptions_getOrStartTxn(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, app)

logger := zerolog.Nop()
opts := DefaultClientOpts(false, true)
opts = append(opts, WithNewRelic(app))
opts = append(opts, WithNewRelic(app), WithLogger(&logger))

var tc ClientInterface
tc, err = NewClient(
Expand All @@ -90,8 +92,9 @@ func Test_newRelicOptions_getOrStartTxn(t *testing.T) {
})

t.Run("invalid ctx and txn", func(t *testing.T) {
logger := zerolog.Nop()
opts := DefaultClientOpts(false, true)
opts = append(opts, WithNewRelic(nil))
opts = append(opts, WithNewRelic(nil), WithLogger(&logger))

tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...)
require.NoError(t, err)
Expand Down Expand Up @@ -148,8 +151,9 @@ func TestWithUserAgent(t *testing.T) {
})

t.Run("empty user agent", func(t *testing.T) {
logger := zerolog.Nop()
opts := DefaultClientOpts(false, true)
opts = append(opts, WithUserAgent(""))
opts = append(opts, WithUserAgent(""), WithLogger(&logger))

tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...)
require.NoError(t, err)
Expand All @@ -163,8 +167,9 @@ func TestWithUserAgent(t *testing.T) {
t.Run("custom user agent", func(t *testing.T) {
customAgent := "custom-user-agent"

logger := zerolog.Nop()
opts := DefaultClientOpts(false, true)
opts = append(opts, WithUserAgent(customAgent))
opts = append(opts, WithUserAgent(customAgent), WithLogger(&logger))

tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...)
require.NoError(t, err)
Expand Down Expand Up @@ -382,12 +387,16 @@ func TestWithFreeCacheConnection(t *testing.T) {
})

t.Run("using a nil client", func(t *testing.T) {
logger := zerolog.Nop()

tc, err := NewClient(
tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx),
WithFreeCacheConnection(nil),
WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory),
WithSQLite(&datastore.SQLiteConfig{Shared: true}),
WithMinercraft(&chainstate.MinerCraftBase{}))
WithMinercraft(&chainstate.MinerCraftBase{}),
WithLogger(&logger),
)
require.NoError(t, err)
require.NotNil(t, tc)

Expand All @@ -400,13 +409,15 @@ func TestWithFreeCacheConnection(t *testing.T) {

t.Run("using an existing connection", func(t *testing.T) {
fc := freecache.NewCache(cachestore.DefaultCacheSize)

logger := zerolog.Nop()
tc, err := NewClient(
tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx),
WithFreeCacheConnection(fc),
WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory),
WithSQLite(&datastore.SQLiteConfig{Shared: true}),
WithMinercraft(&chainstate.MinerCraftBase{}))
WithMinercraft(&chainstate.MinerCraftBase{}),
WithLogger(&logger),
)
require.NoError(t, err)
require.NotNil(t, tc)
defer CloseClient(context.Background(), t, tc)
Expand Down Expand Up @@ -459,9 +470,14 @@ func TestWithTaskQ(t *testing.T) {
// todo: test cases where config is nil, or cannot load TaskQ

t.Run("using taskq using memory", func(t *testing.T) {

logger := zerolog.Nop()
tcOpts := DefaultClientOpts(true, true)
tcOpts = append(tcOpts, WithLogger(&logger))

tc, err := NewClient(
tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx),
DefaultClientOpts(false, true)...,
tcOpts...,
)
require.NoError(t, err)
require.NotNil(t, tc)
Expand All @@ -478,6 +494,8 @@ func TestWithTaskQ(t *testing.T) {
t.Skip("skipping live local redis tests")
}

logger := zerolog.Nop()

tc, err := NewClient(
tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx),
WithTaskQUsingRedis(
Expand All @@ -491,6 +509,7 @@ func TestWithTaskQ(t *testing.T) {
}),
WithSQLite(tester.SQLiteTestConfig(false, true)),
WithMinercraft(&chainstate.MinerCraftBase{}),
WithLogger(&logger),
)
require.NoError(t, err)
require.NotNil(t, tc)
Expand Down Expand Up @@ -522,19 +541,20 @@ func TestWithLogger(t *testing.T) {
defer CloseClient(context.Background(), t, tc)

assert.NotNil(t, tc.Logger())
assert.Equal(t, logging.GetDefaultLogger(), tc.Logger())
})

t.Run("test applying option", func(t *testing.T) {
customLogger := logging.GetDefaultLogger()
customLogger := zerolog.Nop()
opts := DefaultClientOpts(false, true)
opts = append(opts, WithLogger(customLogger))
opts = append(opts, WithLogger(&customLogger))

tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...)
require.NoError(t, err)
require.NotNil(t, tc)
defer CloseClient(context.Background(), t, tc)

assert.Equal(t, customLogger, tc.Logger())
assert.Equal(t, &customLogger, tc.Logger())
})
}

Expand Down
5 changes: 4 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bitcoin-sv/go-paymail"
"github.com/mrz1836/go-cachestore"
"github.com/mrz1836/go-datastore"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -254,13 +255,15 @@ func TestPaymailOptions_ServerConfig(t *testing.T) {
})

t.Run("valid server config", func(t *testing.T) {
logger := zerolog.Nop()
opts := DefaultClientOpts(false, true)
opts = append(opts, WithPaymailSupport(
[]string{testDomain},
defaultSenderPaymail,
defaultAddressResolutionPurpose,
false, false,
))
),
WithLogger(&logger))

tc, err := NewClient(context.Background(), opts...)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// GetDefaultLogger generates and returns a default logger instance.
func GetDefaultLogger() *zerolog.Logger {
logger := ecszerolog.New(os.Stdout, ecszerolog.Level(zerolog.DebugLevel)).
logger := ecszerolog.New(os.Stdout, ecszerolog.Level(zerolog.InfoLevel)).
With().
Timestamp().
Caller().
Expand Down
25 changes: 21 additions & 4 deletions model_transaction_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/utils"
magic "github.com/bitcoinschema/go-map"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -193,9 +194,13 @@ func TestTransactionConfig_processOutput(t *testing.T) {
t.Run("basic paymail address resolution - valid response", func(t *testing.T) {
client := newTestPaymailClient(t, []string{testDomain})

logger := zerolog.Nop()
tcOpts := DefaultClientOpts(true, true)
tcOpts = append(tcOpts, WithLogger(&logger))

tc, err := NewClient(
context.Background(),
DefaultClientOpts(true, true)...,
tcOpts...,
)
require.NoError(t, err)
require.NotNil(t, tc)
Expand Down Expand Up @@ -232,9 +237,13 @@ func TestTransactionConfig_processOutput(t *testing.T) {

client := newTestPaymailClient(t, []string{testDomain, handleDomain})

logger := zerolog.Nop()
tcOpts := DefaultClientOpts(true, true)
tcOpts = append(tcOpts, WithLogger(&logger))

tc, err := NewClient(
context.Background(),
DefaultClientOpts(true, true)...,
tcOpts...,
)
require.NoError(t, err)
require.NotNil(t, tc)
Expand Down Expand Up @@ -271,9 +280,13 @@ func TestTransactionConfig_processOutput(t *testing.T) {

client := newTestPaymailClient(t, []string{testDomain, handleDomain})

logger := zerolog.Nop()
tcOpts := DefaultClientOpts(true, true)
tcOpts = append(tcOpts, WithLogger(&logger))

tc, err := NewClient(
context.Background(),
DefaultClientOpts(true, true)...,
tcOpts...,
)
require.NoError(t, err)
require.NotNil(t, tc)
Expand Down Expand Up @@ -307,9 +320,13 @@ func TestTransactionConfig_processOutput(t *testing.T) {
t.Run("p2p paymail address resolution - valid response", func(t *testing.T) {
client := newTestPaymailClient(t, []string{testDomain})

logger := zerolog.Nop()
tcOpts := DefaultClientOpts(true, true)
tcOpts = append(tcOpts, WithLogger(&logger))

tc, err := NewClient(
context.Background(),
DefaultClientOpts(true, true)...,
tcOpts...,
)
require.NoError(t, err)
require.NotNil(t, tc)
Expand Down
Loading

0 comments on commit f0eb1c0

Please sign in to comment.