get_total_subwallet_balance_test.go raw

   1  package queries
   2  
   3  import (
   4  	"fmt"
   5  	"testing"
   6  
   7  	"github.com/stretchr/testify/assert"
   8  	"github.com/stretchr/testify/require"
   9  	"gorm.io/datatypes"
  10  
  11  	"github.com/getAlby/hub/constants"
  12  	"github.com/getAlby/hub/db"
  13  	"github.com/getAlby/hub/tests"
  14  )
  15  
  16  func TestGetTotalSubwalletBalance(t *testing.T) {
  17  	svc, err := tests.CreateTestService(t)
  18  	require.NoError(t, err)
  19  	defer svc.Remove()
  20  
  21  	subwalletA, _, err := tests.CreateApp(svc)
  22  	require.NoError(t, err)
  23  	subwalletA.Isolated = true
  24  	subwalletA.Metadata = datatypes.JSON([]byte(fmt.Sprintf(`{"%s":"%s"}`, constants.METADATA_APPSTORE_APP_ID_KEY, constants.SUBWALLET_APPSTORE_APP_ID)))
  25  	svc.DB.Save(&subwalletA)
  26  
  27  	subwalletB, _, err := tests.CreateApp(svc)
  28  	require.NoError(t, err)
  29  	subwalletB.Isolated = true
  30  	subwalletB.Metadata = datatypes.JSON([]byte(fmt.Sprintf(`{"%s":"%s"}`, constants.METADATA_APPSTORE_APP_ID_KEY, constants.SUBWALLET_APPSTORE_APP_ID)))
  31  	svc.DB.Save(&subwalletB)
  32  
  33  	incomingSubwalletTx := db.Transaction{
  34  		AppId:      &subwalletA.ID,
  35  		Type:       constants.TRANSACTION_TYPE_INCOMING,
  36  		State:      constants.TRANSACTION_STATE_SETTLED,
  37  		AmountMsat: 5000,
  38  	}
  39  	svc.DB.Save(&incomingSubwalletTx)
  40  
  41  	outgoingSettledSubwalletTx := db.Transaction{
  42  		AppId:          &subwalletA.ID,
  43  		Type:           constants.TRANSACTION_TYPE_OUTGOING,
  44  		State:          constants.TRANSACTION_STATE_SETTLED,
  45  		AmountMsat:     1000,
  46  		FeeMsat:        100,
  47  		FeeReserveMsat: 0,
  48  	}
  49  	svc.DB.Save(&outgoingSettledSubwalletTx)
  50  
  51  	outgoingPendingSubwalletTx := db.Transaction{
  52  		AppId:          &subwalletB.ID,
  53  		Type:           constants.TRANSACTION_TYPE_OUTGOING,
  54  		State:          constants.TRANSACTION_STATE_PENDING,
  55  		AmountMsat:     2000,
  56  		FeeMsat:        0,
  57  		FeeReserveMsat: 300,
  58  	}
  59  	svc.DB.Save(&outgoingPendingSubwalletTx)
  60  
  61  	totalBalanceMsat, err := GetTotalSubwalletBalanceMsat(svc.DB)
  62  	require.NoError(t, err)
  63  	assert.Equal(t, int64(1600), totalBalanceMsat)
  64  }
  65