Skip to content

Commit

Permalink
chore: update tests for testing with Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmitr committed Dec 23, 2024
1 parent 3d96ce1 commit 564cc20
Show file tree
Hide file tree
Showing 35 changed files with 334 additions and 209 deletions.
11 changes: 6 additions & 5 deletions alby/alby_oauth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package alby
import (
"testing"

"github.com/getAlby/hub/config"
"github.com/getAlby/hub/events"
"github.com/getAlby/hub/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tyler-smith/go-bip32"
"github.com/tyler-smith/go-bip39"

"github.com/getAlby/hub/config"
"github.com/getAlby/hub/events"
"github.com/getAlby/hub/tests"
)

func TestExistingEncryptedBackup(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

mnemonic := "limit reward expect search tissue call visa fit thank cream brave jump"
unlockPassword := "123"
Expand All @@ -40,11 +41,11 @@ func TestExistingEncryptedBackup(t *testing.T) {
}

func TestEncryptedBackup(t *testing.T) {
defer tests.RemoveTestService()
mnemonic := "limit reward expect search tissue call visa fit thank cream brave jump"
unlockPassword := "123"
svc, err := tests.CreateTestServiceWithMnemonic(mnemonic, unlockPassword)
require.NoError(t, err)
defer svc.Remove()

albyOAuthSvc := NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
encryptedBackup, err := albyOAuthSvc.createEncryptedChannelBackup(&events.StaticChannelsBackupEvent{
Expand Down
82 changes: 59 additions & 23 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,68 @@ import (
"github.com/getAlby/hub/logger"
)

type Config struct {
URI string
LogQueries bool
DriverName string
}

func NewDB(uri string, logDBQueries bool) (*gorm.DB, error) {
config := &gorm.Config{
return NewDBWithConfig(&Config{
URI: uri,
LogQueries: logDBQueries,
DriverName: "",
})
}

func NewDBWithConfig(cfg *Config) (*gorm.DB, error) {
gormConfig := &gorm.Config{
TranslateError: true,
}
if logDBQueries {
config.Logger = gorm_logger.Default.LogMode(gorm_logger.Info)
if cfg.LogQueries {
gormConfig.Logger = gorm_logger.Default.LogMode(gorm_logger.Info)
}

if strings.HasPrefix(uri, "postgresql://") {
return newPostgresDB(uri, config)
var ret *gorm.DB

if IsPostgresURI(cfg.URI) {
pgConfig := postgres.Config{
DriverName: cfg.DriverName,
DSN: cfg.URI,
}
var err error
ret, err = newPostgresDB(pgConfig, gormConfig)
if err != nil {
return nil, err
}
} else {
sqliteURI := cfg.URI
// avoid SQLITE_BUSY errors with _txlock=IMMEDIATE
if !strings.Contains(sqliteURI, "_txlock=") {
sqliteURI = sqliteURI + "?_txlock=IMMEDIATE"
}
sqliteConfig := sqlite.Config{
DriverName: cfg.DriverName,
DSN: sqliteURI,
}
var err error
ret, err = newSqliteDB(sqliteConfig, gormConfig)
if err != nil {
return nil, err
}
}

return newSqliteDB(uri, config)
err := migrations.Migrate(ret)
if err != nil {
logger.Logger.WithError(err).Error("Failed to migrate")
return nil, err
}

return ret, nil
}

func newSqliteDB(uri string, config *gorm.Config) (*gorm.DB, error) {
// avoid SQLITE_BUSY errors with _txlock=IMMEDIATE
gormDB, err := gorm.Open(sqlite.Open(uri+"?_txlock=IMMEDIATE"), config)
func newSqliteDB(sqliteConfig sqlite.Config, gormConfig *gorm.Config) (*gorm.DB, error) {
gormDB, err := gorm.Open(sqlite.New(sqliteConfig), gormConfig)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,27 +119,15 @@ func newSqliteDB(uri string, config *gorm.Config) (*gorm.DB, error) {
return nil, err
}

err = migrations.Migrate(gormDB)
if err != nil {
logger.Logger.WithError(err).Error("Failed to migrate")
return nil, err
}

return gormDB, nil
}

func newPostgresDB(uri string, config *gorm.Config) (*gorm.DB, error) {
gormDB, err := gorm.Open(postgres.Open(uri), config)
func newPostgresDB(pgConfig postgres.Config, gormConfig *gorm.Config) (*gorm.DB, error) {
gormDB, err := gorm.Open(postgres.New(pgConfig), gormConfig)
if err != nil {
return nil, err
}

err = migrations.Migrate(gormDB)
if err != nil {
logger.Logger.WithError(err).Error("Failed to migrate")
return nil, err
}

return gormDB, nil
}

Expand All @@ -120,3 +152,7 @@ func Stop(db *gorm.DB) error {
}
return nil
}

func IsPostgresURI(uri string) bool {
return strings.HasPrefix(uri, "postgresql://")
}
8 changes: 6 additions & 2 deletions db/migrations/202406061259_delete_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migrations

import (
_ "embed"
"testing"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
Expand All @@ -19,8 +20,11 @@ var _202406061259_delete_content = &gormigrate.Migration{
return err
}

if err := tx.Exec("VACUUM").Error; err != nil {
return err
// Cannot run when testing with txdb: VACUUM must be run outside of transaction.
if !testing.Testing() {
if err := tx.Exec("VACUUM").Error; err != nil {
return err
}
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions db/migrations/202406071726_vacuum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migrations

import (
_ "embed"
"testing"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
Expand All @@ -15,8 +16,11 @@ import (
var _202406071726_vacuum = &gormigrate.Migration{
ID: "202406071726_vacuum",
Migrate: func(tx *gorm.DB) error {
if err := tx.Exec("VACUUM").Error; err != nil {
return err
// Cannot run when testing with txdb: VACUUM must be run outside of transaction.
if !testing.Testing() {
if err := tx.Exec("VACUUM").Error; err != nil {
return err
}
}

return nil
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
dario.cat/mergo v1.0.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/DATA-DOG/go-txdb v0.2.0 // indirect
github.com/DataDog/datadog-go/v5 v5.3.0 // indirect
github.com/DataDog/gostackparse v0.7.0 // indirect
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DATA-DOG/go-txdb v0.2.0 h1:p1VAEZGN0U58Z5efRbI9mI6fDhcMn2+hV1sPBeOp/A8=
github.com/DATA-DOG/go-txdb v0.2.0/go.mod h1:Dqk6PhlGpMk1JZ3n8sjybgBLcW69nuijArOMubFCXM0=
github.com/DataDog/appsec-internal-go v1.8.0 h1:1Tfn3LEogntRqZtf88twSApOCAAO3V+NILYhuQIo4J4=
github.com/DataDog/appsec-internal-go v1.8.0/go.mod h1:wW0cRfWBo4C044jHGwYiyh5moQV2x0AhnwqMuiX7O/g=
github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0 h1:bUMSNsw1iofWiju9yc1f+kBd33E3hMJtq9GuU602Iy8=
Expand Down
7 changes: 4 additions & 3 deletions lnclient/ldk/ldk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package ldk
import (
"testing"

"github.com/getAlby/hub/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/getAlby/hub/tests"
)

func TestGetVssNodeIdentifier(t *testing.T) {
mnemonic := "thought turkey ask pottery head say catalog desk pledge elbow naive mimic"
expectedVssNodeIdentifier := "751636"

defer tests.RemoveTestService()
svc, err := tests.CreateTestServiceWithMnemonic(mnemonic, "123")
require.NoError(t, err)
defer svc.Remove()

vssNodeIdentifier, err := GetVssNodeIdentifier(svc.Keys)
require.NoError(t, err)
Expand All @@ -25,9 +26,9 @@ func TestGetVssNodeIdentifier2(t *testing.T) {
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
expectedVssNodeIdentifier := "770256"

defer tests.RemoveTestService()
svc, err := tests.CreateTestServiceWithMnemonic(mnemonic, "123")
require.NoError(t, err)
defer svc.Remove()

vssNodeIdentifier, err := GetVssNodeIdentifier(svc.Keys)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions nip47/controllers/get_balance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const nip47GetBalanceJson = `

func TestHandleGetBalanceEvent(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBalanceJson), nip47Request)
Expand Down Expand Up @@ -57,9 +57,9 @@ func TestHandleGetBalanceEvent(t *testing.T) {

func TestHandleGetBalanceEvent_IsolatedApp_NoTransactions(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBalanceJson), nip47Request)
Expand Down Expand Up @@ -90,9 +90,9 @@ func TestHandleGetBalanceEvent_IsolatedApp_NoTransactions(t *testing.T) {
}
func TestHandleGetBalanceEvent_IsolatedApp_Transactions(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBalanceJson), nip47Request)
Expand Down
10 changes: 5 additions & 5 deletions nip47/controllers/get_budget_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const nip47GetBudgetJson = `

func TestHandleGetBudgetEvent_NoRenewal(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down Expand Up @@ -71,9 +71,9 @@ func TestHandleGetBudgetEvent_NoRenewal(t *testing.T) {

func TestHandleGetBudgetEvent_NoneUsed(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down Expand Up @@ -118,9 +118,9 @@ func TestHandleGetBudgetEvent_NoneUsed(t *testing.T) {

func TestHandleGetBudgetEvent_HalfUsed(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down Expand Up @@ -172,9 +172,9 @@ func TestHandleGetBudgetEvent_HalfUsed(t *testing.T) {

func TestHandleGetBudgetEvent_NoBudget(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down Expand Up @@ -219,9 +219,9 @@ func TestHandleGetBudgetEvent_NoBudget(t *testing.T) {

func TestHandleGetBudgetEvent_NoPayInvoicePermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

nip47Request := &models.Request{}
err = json.Unmarshal([]byte(nip47GetBudgetJson), nip47Request)
Expand Down
6 changes: 3 additions & 3 deletions nip47/controllers/get_info_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const nip47GetInfoJson = `

func TestHandleGetInfoEvent_NoPermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -74,9 +74,9 @@ func TestHandleGetInfoEvent_NoPermission(t *testing.T) {

func TestHandleGetInfoEvent_WithPermission(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down Expand Up @@ -122,9 +122,9 @@ func TestHandleGetInfoEvent_WithPermission(t *testing.T) {

func TestHandleGetInfoEvent_WithNotifications(t *testing.T) {
ctx := context.TODO()
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
require.NoError(t, err)
defer svc.Remove()

app, _, err := tests.CreateApp(svc)
assert.NoError(t, err)
Expand Down
Loading

0 comments on commit 564cc20

Please sign in to comment.