Skip to content

Commit

Permalink
fix: add invoicesfor in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Dec 12, 2023
1 parent 838b226 commit 205fe1a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion integration_tests/hodl_invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (suite *HodlInvoiceSuite) TestHodlInvoice() {
}
assert.Equal(suite.T(), int64(userFundingSats), userBalance)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/internal_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func (suite *PaymentTestSuite) TestInternalPaymentFail() {
_ = suite.createPayInvoiceReqError(bobInvoice.PayReq, suite.aliceToken)

userId := getUserIdFromToken(suite.aliceToken)
invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/keysend_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (suite *KeySendFailureTestSuite) TestKeysendPayment() {
}
assert.Equal(suite.T(), int64(aliceFundingSats), aliceBalance)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/payment_failure_async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (suite *PaymentTestAsyncErrorsSuite) TestExternalAsyncFailingInvoice() {
}
assert.Equal(suite.T(), int64(userFundingSats), userBalance)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/payment_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (suite *PaymentTestErrorsSuite) TestExternalFailingInvoice() {

userId := getUserIdFromToken(suite.userToken)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
invoices, err := invoicesFor(suite.service, userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
Expand Down
18 changes: 18 additions & 0 deletions integration_tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"os"
"time"

"github.com/getAlby/lndhub.go/common"
"github.com/getAlby/lndhub.go/db"
"github.com/getAlby/lndhub.go/db/migrations"
"github.com/getAlby/lndhub.go/db/models"
"github.com/getAlby/lndhub.go/lib"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
Expand Down Expand Up @@ -126,6 +128,22 @@ func getUserIdFromToken(token string) int64 {
return int64(claims["id"].(float64))
}

// since svc.invoicesFor excludes erroneous invoices, this is used for testing
func invoicesFor(svc *service.LndhubService, userId int64, invoiceType string) ([]models.Invoice, error) {
var invoices []models.Invoice

query := svc.DB.NewSelect().Model(&invoices).Where("user_id = ?", userId)
if invoiceType != "" {
query.Where("type = ? AND state <> ?", invoiceType, common.InvoiceStateInitialized)
}
query.OrderExpr("id DESC").Limit(100)
err := query.Scan(context.Background())
if err != nil {
return nil, err
}
return invoices, nil
}

func createUsers(svc *service.LndhubService, usersToCreate int) (logins []ExpectedCreateUserResponseBody, tokens []string, err error) {
logins = []ExpectedCreateUserResponseBody{}
tokens = []string{}
Expand Down

0 comments on commit 205fe1a

Please sign in to comment.