Skip to content

Commit

Permalink
refactor configuration handling in tests to use ConfigStore instead o…
Browse files Browse the repository at this point in the history
…f MockConfig
  • Loading branch information
jerry-enebeli committed Jan 17, 2025
1 parent 90db126 commit 95d4967
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 26 deletions.
37 changes: 35 additions & 2 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func TestCreateAccount(t *testing.T) {
}

func TestCreateAccountWithExternalGenerator(t *testing.T) {

// Initialize the mock HTTP responder
httpmock.Activate()
defer httpmock.DeactivateAndReset()
Expand All @@ -92,8 +93,6 @@ func TestCreateAccountWithExternalGenerator(t *testing.T) {
httpmock.RegisterResponder("GET", "http://example.com/generateAccount",
httpmock.NewStringResponder(200, `{"account_number": "123456789", "bank_name": "Blnk Bank"}`))

config.MockConfig(&config.Configuration{Server: config.ServerConfig{SecretKey: "some-secret"}, AccountNumberGeneration: config.AccountNumberGenerationConfig{HttpService: config.AccountGenerationHttpService{Url: "http://example.com/generateAccount"}}})

account := model.Account{
Name: "Test Account",
BankName: "Blnk Bank",
Expand Down Expand Up @@ -129,6 +128,23 @@ func TestCreateAccountWithExternalGenerator(t *testing.T) {
}

func TestGetAccountByID(t *testing.T) {
cnf := &config.Configuration{
Redis: config.RedisConfig{
Dns: "localhost:6379",
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
Server: config.ServerConfig{SecretKey: "some-secret"},
AccountNumberGeneration: config.AccountNumberGenerationConfig{
HttpService: config.AccountGenerationHttpService{
Url: "http://example.com/generateAccount",
},
},
}

config.ConfigStore.Store(cnf)
datasource, mock, err := newTestDataSource()
assert.NoError(t, err)

Expand Down Expand Up @@ -172,6 +188,23 @@ func TestGetAccountByID(t *testing.T) {
}

func TestGetAllAccounts(t *testing.T) {
cnf := &config.Configuration{
Redis: config.RedisConfig{
Dns: "localhost:6379",
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
Server: config.ServerConfig{SecretKey: "some-secret"},
AccountNumberGeneration: config.AccountNumberGenerationConfig{
HttpService: config.AccountGenerationHttpService{
Url: "http://example.com/generateAccount",
},
},
}

config.ConfigStore.Store(cnf)
datasource, mock, err := newTestDataSource()
assert.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func TestGetBalanceByID(t *testing.T) {
}

func TestGetAllBalances(t *testing.T) {

datasource, mock, err := newTestDataSource()
if err != nil {
t.Fatalf("Error creating test data source: %s", err)
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func setupBlnk(cfg *config.Configuration) (*blnk.Blnk, error) {
// Create a new Blnk instance using the initialized data source.
newBlnk, err := blnk.NewBlnk(db)
if err != nil {
logrus.Error(err) // Log the error using Logrus
return &blnk.Blnk{}, fmt.Errorf("error creating blnk: %v", err)
}
return newBlnk, nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func serverCommands(b *blnkInstance) *cobra.Command {
// Load configuration
cfg, err := config.Fetch()
if err != nil {
log.Fatal(err)
log.Println(err)
}

// Initialize observability (tracing and PostHog)
Expand Down
2 changes: 1 addition & 1 deletion database/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestGetDBConnection_Singleton(t *testing.T) {
},
}

config.MockConfig(mockConfig)
config.ConfigStore.Store(mockConfig)

// First call to GetDBConnection should initialize the instance
ds1, err := GetDBConnection(mockConfig)
Expand Down
18 changes: 17 additions & 1 deletion internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@ import (
)

func TestSet(t *testing.T) {
config.MockConfig(&config.Configuration{})
cnf := &config.Configuration{
Redis: config.RedisConfig{
Dns: "localhost:6379",
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
Server: config.ServerConfig{SecretKey: "some-secret"},
AccountNumberGeneration: config.AccountNumberGenerationConfig{
HttpService: config.AccountGenerationHttpService{
Url: "http://example.com/generateAccount",
},
},
}

config.ConfigStore.Store(cnf)
ctx := context.Background()
mockCache, err := NewCache()
assert.NoError(t, err)
Expand Down
18 changes: 17 additions & 1 deletion ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,23 @@ import (
)

func newTestDataSource() (database.IDataSource, sqlmock.Sqlmock, error) {
config.MockConfig(&config.Configuration{})
cnf := &config.Configuration{
Redis: config.RedisConfig{
Dns: "localhost:6379",
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
Server: config.ServerConfig{SecretKey: "some-secret"},
AccountNumberGeneration: config.AccountNumberGenerationConfig{
HttpService: config.AccountGenerationHttpService{
Url: "http://example.com/generateAccount",
},
},
}

config.ConfigStore.Store(cnf)
db, mock, err := sqlmock.New()
if err != nil {
log.Printf("an error '%s' was not expected when opening a stub database Connection", err)
Expand Down
29 changes: 20 additions & 9 deletions queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestEnqueueImmediateTransactionSuccess(t *testing.T) {
NumberOfQueues: 1,
},
}
config.MockConfig(cnf)
config.ConfigStore.Store(cnf)

redisOption, err := redis_db.ParseRedisURL("localhost:6379")
if err != nil {
Expand Down Expand Up @@ -68,10 +68,16 @@ func TestEnqueueImmediateTransactionSuccess(t *testing.T) {
}

func TestEnqueueScheduledTransaction(t *testing.T) {
conf, err := config.Fetch()
if err != nil {
assert.NoError(t, err)
conf := &config.Configuration{
Redis: config.RedisConfig{
Dns: "localhost:6379",
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
}
config.ConfigStore.Store(conf)

client := asynq.NewClient(asynq.RedisClientOpt{Addr: "localhost:6379"})
inspector := asynq.NewInspector(asynq.RedisClientOpt{Addr: "localhost:6379"})
Expand All @@ -86,7 +92,7 @@ func TestEnqueueScheduledTransaction(t *testing.T) {

transaction := getTransactionMock(100, false)

_, err = json.Marshal(transaction)
_, err := json.Marshal(transaction)
assert.NoError(t, err)

err = q.Enqueue(context.Background(), &transaction)
Expand All @@ -100,9 +106,14 @@ func TestEnqueueScheduledTransaction(t *testing.T) {
}

func TestEnqueueWithAsynqClientEnqueueError(t *testing.T) {
conf, err := config.Fetch()
if err != nil {
assert.NoError(t, err)
conf := &config.Configuration{
Redis: config.RedisConfig{
Dns: "localhost:6379",
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
}

client := asynq.NewClient(asynq.RedisClientOpt{Addr: "localhost:6379"})
Expand All @@ -118,7 +129,7 @@ func TestEnqueueWithAsynqClientEnqueueError(t *testing.T) {

transaction := getTransactionMock(100, false)

_, err = json.Marshal(transaction)
_, err := json.Marshal(transaction)
assert.NoError(t, err)

err = q.Enqueue(context.Background(), &transaction)
Expand Down
24 changes: 14 additions & 10 deletions reconciliation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestOneToOneReconciliation(t *testing.T) {
MaxWorkers: 1,
},
}
config.MockConfig(cnf)
config.ConfigStore.Store(cnf)
mockDS := new(mocks.MockDataSource)
blnk := &Blnk{datasource: mockDS}

Expand Down Expand Up @@ -82,10 +82,11 @@ func TestOneToOneReconciliation(t *testing.T) {
func TestOneToManyReconciliation(t *testing.T) {
cnf := &config.Configuration{
Transaction: config.TransactionConfig{
BatchSize: 100000,
BatchSize: 100000,
MaxWorkers: 1,
},
}
config.MockConfig(cnf)
config.ConfigStore.Store(cnf)
mockDS := new(mocks.MockDataSource)
blnk := &Blnk{datasource: mockDS}

Expand Down Expand Up @@ -140,14 +141,15 @@ func TestOneToManyReconciliationNoMatches(t *testing.T) {
Dns: "localhost:6379",
},
Transaction: config.TransactionConfig{
BatchSize: 100000,
BatchSize: 100000,
MaxWorkers: 1,
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
}
config.MockConfig(cnf)
config.ConfigStore.Store(cnf)
mockDS := new(mocks.MockDataSource)
blnk := &Blnk{datasource: mockDS}

Expand Down Expand Up @@ -375,15 +377,18 @@ func TestReconciliationEdgeCases(t *testing.T) {
Dns: "localhost:6379",
},
Transaction: config.TransactionConfig{
BatchSize: 100000,
BatchSize: 100000,
MaxWorkers: 1,
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
Reconciliation: config.ReconciliationConfig{
ProgressInterval: 100,
},
}
config.MockConfig(cnf)

config.ConfigStore.Store(cnf)
mockDS := new(mocks.MockDataSource)

blnk := &Blnk{datasource: mockDS}
Expand Down Expand Up @@ -411,9 +416,8 @@ func TestReconciliationEdgeCases(t *testing.T) {
externalTxns := []*model.Transaction{
{TransactionID: "ext1", Amount: 100, CreatedAt: time.Now()},
}
internalTxns := []*model.Transaction{}

mockDS.On("GetTransactionsPaginated", mock.Anything, "", 100000, int64(0)).Return(internalTxns, nil)
mockDS.On("GetTransactionsPaginated", mock.Anything, "", 100000, int64(0)).Return([]*model.Transaction{}, nil).Once()

matchingRules := []model.MatchingRule{
{
Expand Down
35 changes: 35 additions & 0 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"
"time"

"github.com/jerry-enebeli/blnk/config"
"github.com/jerry-enebeli/blnk/model"

"github.com/brianvoe/gofakeit/v6"
Expand All @@ -34,6 +35,23 @@ import (
)

func TestRecordTransaction(t *testing.T) {
cnf := &config.Configuration{
Redis: config.RedisConfig{
Dns: "localhost:6379",
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
Server: config.ServerConfig{SecretKey: "some-secret"},
AccountNumberGeneration: config.AccountNumberGenerationConfig{
HttpService: config.AccountGenerationHttpService{
Url: "http://example.com/generateAccount",
},
},
}

config.ConfigStore.Store(cnf)
datasource, mock, err := newTestDataSource()
assert.NoError(t, err)

Expand Down Expand Up @@ -143,6 +161,23 @@ func TestRecordTransaction(t *testing.T) {
}

func TestRecordTransactionWithRate(t *testing.T) {
cnf := &config.Configuration{
Redis: config.RedisConfig{
Dns: "localhost:6379",
},
Queue: config.QueueConfig{
WebhookQueue: "webhook_queue",
NumberOfQueues: 1,
},
Server: config.ServerConfig{SecretKey: "some-secret"},
AccountNumberGeneration: config.AccountNumberGenerationConfig{
HttpService: config.AccountGenerationHttpService{
Url: "http://example.com/generateAccount",
},
},
}

config.ConfigStore.Store(cnf)
datasource, mock, err := newTestDataSource()
assert.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestSendWebhook(t *testing.T) {
},
},
}
config.MockConfig(cnf)
config.ConfigStore.Store(cnf)

testData := NewWebhook{
Event: "transaction.queued",
Expand Down

0 comments on commit 95d4967

Please sign in to comment.