Skip to content

Commit

Permalink
SDP-761 - refactor, remove SqlxDB() from interface
Browse files Browse the repository at this point in the history
  • Loading branch information
marwen-abid committed Nov 3, 2023
1 parent d6dfcc8 commit 22e1be8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 58 deletions.
1 change: 0 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type DBConnectionPool interface {
Close() error
Ping() error
SqlDB() *sql.DB
SqlxDB() *sqlx.DB
DSN(ctx context.Context) (string, error)
}

Expand Down
5 changes: 0 additions & 5 deletions db/db_connection_pool_with_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"fmt"

"github.com/jmoiron/sqlx"
"github.com/stellar/stellar-disbursement-platform-backend/internal/monitor"
)

Expand Down Expand Up @@ -51,10 +50,6 @@ func (dbc *DBConnectionPoolWithMetrics) SqlDB() *sql.DB {
return dbc.dbConnectionPool.SqlDB()
}

func (dbc *DBConnectionPoolWithMetrics) SqlxDB() *sqlx.DB {
return dbc.dbConnectionPool.SqlxDB()
}

func (dbc *DBConnectionPoolWithMetrics) DSN(ctx context.Context) (string, error) {
return dbc.dbConnectionPool.DSN(ctx)
}
18 changes: 0 additions & 18 deletions db/db_connection_pool_with_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,12 @@ import (
"database/sql"
"testing"

"github.com/jmoiron/sqlx"
"github.com/stellar/stellar-disbursement-platform-backend/db/dbtest"
"github.com/stellar/stellar-disbursement-platform-backend/internal/monitor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDBConnectionPoolWithMetrics_SqlxDB(t *testing.T) {
dbt := dbtest.Open(t)
defer dbt.Close()
dbConnectionPool, err := OpenDBConnectionPool(dbt.DSN)
require.NoError(t, err)
defer dbConnectionPool.Close()

mMonitorService := &monitor.MockMonitorService{}

dbConnectionPoolWithMetrics, err := NewDBConnectionPoolWithMetrics(dbConnectionPool, mMonitorService)
require.NoError(t, err)

sqlxDB := dbConnectionPoolWithMetrics.SqlxDB()

assert.IsType(t, &sqlx.DB{}, sqlxDB)
}

func TestDBConnectionPoolWithMetrics_SqlDB(t *testing.T) {
dbt := dbtest.Open(t)
defer dbt.Close()
Expand Down
28 changes: 14 additions & 14 deletions internal/data/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_AssetModelGet(t *testing.T) {
})

t.Run("returns asset successfully", func(t *testing.T) {
expected := CreateAssetFixture(t, ctx, dbConnectionPool.SqlxDB(), "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV")
expected := CreateAssetFixture(t, ctx, dbConnectionPool, "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV")
actual, err := assetModel.Get(ctx, expected.ID)
require.NoError(t, err)
assert.Equal(t, expected, actual)
Expand All @@ -57,7 +57,7 @@ func Test_AssetModelGetByCodeAndIssuer(t *testing.T) {
})

t.Run("returns asset successfully", func(t *testing.T) {
expected := CreateAssetFixture(t, ctx, dbConnectionPool.SqlxDB(), "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV")
expected := CreateAssetFixture(t, ctx, dbConnectionPool, "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV")
actual, err := assetModel.GetByCodeAndIssuer(ctx, expected.Code, expected.Issuer)
require.NoError(t, err)
assert.Equal(t, expected, actual)
Expand All @@ -77,15 +77,15 @@ func Test_AssetModelGetAll(t *testing.T) {
assetModel := &AssetModel{dbConnectionPool: dbConnectionPool}

t.Run("returns all assets successfully", func(t *testing.T) {
expected := ClearAndCreateAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
expected := ClearAndCreateAssetFixtures(t, ctx, dbConnectionPool)
actual, err := assetModel.GetAll(ctx)
require.NoError(t, err)

assert.Equal(t, expected, actual)
})

t.Run("returns empty array when no assets", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)
actual, err := assetModel.GetAll(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -145,7 +145,7 @@ func Test_AssetModelInsert(t *testing.T) {
assetModel := &AssetModel{dbConnectionPool: dbConnectionPool}

t.Run("inserts asset successfully", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)
code := "USDT"
issuer := "GBVHJTRLQRMIHRYTXZQOPVYCVVH7IRJN3DOFT7VC6U75CBWWBVDTWURG"

Expand All @@ -159,7 +159,7 @@ func Test_AssetModelInsert(t *testing.T) {
})

t.Run("re-create a deleted asset", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)
code := "USDT"
issuer := "GBVHJTRLQRMIHRYTXZQOPVYCVVH7IRJN3DOFT7VC6U75CBWWBVDTWURG"

Expand Down Expand Up @@ -204,7 +204,7 @@ func Test_AssetModelInsert(t *testing.T) {
})

t.Run("does not insert the same asset again", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)
code := "USDT"
issuer := "GBVHJTRLQRMIHRYTXZQOPVYCVVH7IRJN3DOFT7VC6U75CBWWBVDTWURG"

Expand All @@ -218,7 +218,7 @@ func Test_AssetModelInsert(t *testing.T) {
})

t.Run("creates the stellar native asset successfully", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)

asset, err := assetModel.Insert(ctx, dbConnectionPool, "XLM", "")
require.NoError(t, err)
Expand All @@ -229,15 +229,15 @@ func Test_AssetModelInsert(t *testing.T) {
})

t.Run("does not create an asset with empty issuer", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)

asset, err := assetModel.Insert(ctx, dbConnectionPool, "USDC", "")
assert.EqualError(t, err, `error inserting asset: pq: new row for relation "assets" violates check constraint "asset_issuer_length_check"`)
assert.Nil(t, asset)
})

t.Run("does not create an asset with a invalid issuer", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)

asset, err := assetModel.Insert(ctx, dbConnectionPool, "USDC", "INVALID")
assert.EqualError(t, err, `error inserting asset: pq: new row for relation "assets" violates check constraint "asset_issuer_length_check"`)
Expand Down Expand Up @@ -275,7 +275,7 @@ func Test_AssetModelGetOrCreate(t *testing.T) {
})

t.Run("returns asset successfully", func(t *testing.T) {
expected := CreateAssetFixture(t, ctx, dbConnectionPool.SqlxDB(), "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV")
expected := CreateAssetFixture(t, ctx, dbConnectionPool, "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV")
asset, err := assetModel.GetOrCreate(ctx, expected.Code, expected.Issuer)
require.NoError(t, err)
assert.Equal(t, expected.ID, asset.ID)
Expand All @@ -295,8 +295,8 @@ func Test_AssetModelSoftDelete(t *testing.T) {
assetModel := &AssetModel{dbConnectionPool: dbConnectionPool}

t.Run("delete successful", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
expected := CreateAssetFixture(t, ctx, dbConnectionPool.SqlxDB(), "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV")
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)
expected := CreateAssetFixture(t, ctx, dbConnectionPool, "USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVV")

asset, err := assetModel.SoftDelete(ctx, dbConnectionPool, expected.ID)
require.NoError(t, err)
Expand All @@ -311,7 +311,7 @@ func Test_AssetModelSoftDelete(t *testing.T) {
})

t.Run("delete unsuccessful, cannot find asset", func(t *testing.T) {
DeleteAllAssetFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllAssetFixtures(t, ctx, dbConnectionPool)

_, err := assetModel.SoftDelete(ctx, dbConnectionPool, "non-existant")
require.Error(t, err)
Expand Down
6 changes: 3 additions & 3 deletions internal/data/countries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_CountryModelGet(t *testing.T) {
})

t.Run("returns asset successfully", func(t *testing.T) {
expected := CreateCountryFixture(t, ctx, dbConnectionPool.SqlxDB(), "FRA", "France")
expected := CreateCountryFixture(t, ctx, dbConnectionPool, "FRA", "France")
actual, err := countryModel.Get(ctx, "FRA")
require.NoError(t, err)

Expand All @@ -48,15 +48,15 @@ func Test_CountryModelGetAll(t *testing.T) {
countryModel := &CountryModel{dbConnectionPool: dbConnectionPool}

t.Run("returns all countries successfully", func(t *testing.T) {
expected := ClearAndCreateCountryFixtures(t, ctx, dbConnectionPool.SqlxDB())
expected := ClearAndCreateCountryFixtures(t, ctx, dbConnectionPool)
actual, err := countryModel.GetAll(ctx)
require.NoError(t, err)

assert.Equal(t, expected, actual)
})

t.Run("returns empty array when no countries", func(t *testing.T) {
DeleteAllCountryFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllCountryFixtures(t, ctx, dbConnectionPool)
actual, err := countryModel.GetAll(ctx)
require.NoError(t, err)

Expand Down
32 changes: 16 additions & 16 deletions internal/data/wallets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_WalletModelGet(t *testing.T) {
})

t.Run("returns wallet successfully", func(t *testing.T) {
expected := CreateWalletFixture(t, ctx, dbConnectionPool.SqlxDB(),
expected := CreateWalletFixture(t, ctx, dbConnectionPool,
"NewWallet",
"https://newwallet.com",
"newwallet.com",
Expand Down Expand Up @@ -63,7 +63,7 @@ func Test_WalletModelGetByWalletName(t *testing.T) {
})

t.Run("returns wallet successfully", func(t *testing.T) {
expected := CreateWalletFixture(t, ctx, dbConnectionPool.SqlxDB(),
expected := CreateWalletFixture(t, ctx, dbConnectionPool,
"NewWallet",
"https://newwallet.com",
"newwallet.com",
Expand Down Expand Up @@ -95,7 +95,7 @@ func Test_WalletModelGetAll(t *testing.T) {
xlm := CreateAssetFixture(t, ctx, dbConnectionPool, "XLM", "")

t.Run("returns all wallets successfully", func(t *testing.T) {
wallets := ClearAndCreateWalletFixtures(t, ctx, dbConnectionPool.SqlxDB())
wallets := ClearAndCreateWalletFixtures(t, ctx, dbConnectionPool)

wallet1 := wallets[0]
wallet2 := wallets[1]
Expand Down Expand Up @@ -129,7 +129,7 @@ func Test_WalletModelGetAll(t *testing.T) {
})

t.Run("returns empty array when no wallets", func(t *testing.T) {
DeleteAllWalletFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllWalletFixtures(t, ctx, dbConnectionPool)
actual, err := walletModel.GetAll(ctx)
require.NoError(t, err)

Expand All @@ -148,10 +148,10 @@ func Test_WalletModelFindWallets(t *testing.T) {
walletModel := &WalletModel{dbConnectionPool: dbConnectionPool}

t.Run("returns only enabled wallets", func(t *testing.T) {
wallets := ClearAndCreateWalletFixtures(t, ctx, dbConnectionPool.SqlxDB())
wallets := ClearAndCreateWalletFixtures(t, ctx, dbConnectionPool)

EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool.SqlxDB(), false, wallets[0].ID)
EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool.SqlxDB(), true, wallets[1].ID)
EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool, false, wallets[0].ID)
EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool, true, wallets[1].ID)

findEnabled := true
actual, err := walletModel.FindWallets(ctx, &findEnabled)
Expand All @@ -162,10 +162,10 @@ func Test_WalletModelFindWallets(t *testing.T) {
})

t.Run("returns only disabled wallets", func(t *testing.T) {
wallets := ClearAndCreateWalletFixtures(t, ctx, dbConnectionPool.SqlxDB())
wallets := ClearAndCreateWalletFixtures(t, ctx, dbConnectionPool)

EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool.SqlxDB(), false, wallets[0].ID)
EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool.SqlxDB(), true, wallets[1].ID)
EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool, false, wallets[0].ID)
EnableOrDisableWalletFixtures(t, ctx, dbConnectionPool, true, wallets[1].ID)

findDisabled := false
actual, err := walletModel.FindWallets(ctx, &findDisabled)
Expand All @@ -176,7 +176,7 @@ func Test_WalletModelFindWallets(t *testing.T) {
})

t.Run("returns empty array when no wallets", func(t *testing.T) {
DeleteAllWalletFixtures(t, ctx, dbConnectionPool.SqlxDB())
DeleteAllWalletFixtures(t, ctx, dbConnectionPool)
actual, err := walletModel.FindWallets(ctx, nil)
require.NoError(t, err)

Expand Down Expand Up @@ -396,7 +396,7 @@ func Test_WalletModelGetOrCreate(t *testing.T) {

t.Run("returns error wallet name already been used", func(t *testing.T) {
DeleteAllWalletFixtures(t, ctx, dbConnectionPool)
CreateWalletFixture(t, ctx, dbConnectionPool.SqlxDB(),
CreateWalletFixture(t, ctx, dbConnectionPool,
"test_wallet",
"https://www.new_wallet.com",
"www.new_wallet.com",
Expand Down Expand Up @@ -429,7 +429,7 @@ func Test_WalletModelGetOrCreate(t *testing.T) {

t.Run("returns wallet successfully", func(t *testing.T) {
DeleteAllWalletFixtures(t, ctx, dbConnectionPool)
expected := CreateWalletFixture(t, ctx, dbConnectionPool.SqlxDB(),
expected := CreateWalletFixture(t, ctx, dbConnectionPool,
"test_wallet",
"https://www.test_wallet.com",
"www.test_wallet.com",
Expand Down Expand Up @@ -463,7 +463,7 @@ func Test_WalletModelGetAssets(t *testing.T) {

t.Run("return empty when wallet doesn't have assets", func(t *testing.T) {
DeleteAllWalletFixtures(t, ctx, dbConnectionPool)
wallet := CreateWalletFixture(t, ctx, dbConnectionPool.SqlxDB(),
wallet := CreateWalletFixture(t, ctx, dbConnectionPool,
"NewWallet",
"https://newwallet.com",
"newwallet.com",
Expand All @@ -476,7 +476,7 @@ func Test_WalletModelGetAssets(t *testing.T) {

t.Run("return wallet's assets", func(t *testing.T) {
DeleteAllWalletFixtures(t, ctx, dbConnectionPool)
wallet := CreateWalletFixture(t, ctx, dbConnectionPool.SqlxDB(),
wallet := CreateWalletFixture(t, ctx, dbConnectionPool,
"NewWallet",
"https://newwallet.com",
"newwallet.com",
Expand Down Expand Up @@ -572,7 +572,7 @@ func Test_WalletModelUpdate(t *testing.T) {
_, err = walletModel.Update(ctx, "unknown", true)
assert.Equal(t, ErrRecordNotFound, err)

wallet := CreateWalletFixture(t, ctx, dbConnectionPool.SqlxDB(),
wallet := CreateWalletFixture(t, ctx, dbConnectionPool,
"NewWallet",
"https://newwallet.com",
"newwallet.com",
Expand Down
2 changes: 1 addition & 1 deletion internal/serve/httphandler/payments_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *RetryPaymentsRequest) validate() *httperror.HTTPError {
func (p PaymentsHandler) GetPayment(w http.ResponseWriter, r *http.Request) {
payment_id := chi.URLParam(r, "id")

payment, err := p.Models.Payment.Get(r.Context(), payment_id, p.DBConnectionPool.SqlxDB())
payment, err := p.Models.Payment.Get(r.Context(), payment_id, p.DBConnectionPool)
if err != nil {
if errors.Is(data.ErrRecordNotFound, err) {
errorResponse := fmt.Sprintf("Cannot retrieve payment with ID: %s", payment_id)
Expand Down

0 comments on commit 22e1be8

Please sign in to comment.