Merge pull request #4357 from bhandras/invoices_testify

invoices+test: replace DeepEqual + spew dump with testify
This commit is contained in:
Olaoluwa Osuntokun 2020-06-08 20:37:38 -07:00 committed by GitHub
commit 38b8e54ba7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,16 +2,15 @@ package channeldb
import ( import (
"crypto/rand" "crypto/rand"
"fmt"
"math" "math"
"reflect"
"testing" "testing"
"time" "time"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record" "github.com/lightningnetwork/lnd/record"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
var ( var (
@ -185,10 +184,11 @@ func testInvoiceWorkflow(t *testing.T, test invWorkflowTest) {
} }
return return
} }
if !reflect.DeepEqual(*fakeInvoice, dbInvoice) {
t.Fatalf("invoice fetched from db doesn't match original %v vs %v", require.Equal(t,
spew.Sdump(fakeInvoice), spew.Sdump(dbInvoice)) *fakeInvoice, dbInvoice,
} "invoice fetched from db doesn't match original",
)
// The add index of the invoice retrieved from the database should now // The add index of the invoice retrieved from the database should now
// be fully populated. As this is the first index written to the DB, // be fully populated. As this is the first index written to the DB,
@ -280,11 +280,10 @@ func testInvoiceWorkflow(t *testing.T, test invWorkflowTest) {
// order (and the primary key should be incremented with each // order (and the primary key should be incremented with each
// insertion). // insertion).
for i := 0; i < len(invoices); i++ { for i := 0; i < len(invoices); i++ {
if !reflect.DeepEqual(*invoices[i], response.Invoices[i]) { require.Equal(t,
t.Fatalf("retrieved invoices don't match %v vs %v", *invoices[i], response.Invoices[i],
spew.Sdump(invoices[i]), "retrieved invoice doesn't match",
spew.Sdump(response.Invoices[i])) )
}
} }
} }
@ -293,25 +292,25 @@ func testInvoiceWorkflow(t *testing.T, test invWorkflowTest) {
func TestAddDuplicatePayAddr(t *testing.T) { func TestAddDuplicatePayAddr(t *testing.T) {
db, cleanUp, err := makeTestDB() db, cleanUp, err := makeTestDB()
defer cleanUp() defer cleanUp()
assert.Nil(t, err) require.NoError(t, err)
// Create two invoices with the same payment addr. // Create two invoices with the same payment addr.
invoice1, err := randInvoice(1000) invoice1, err := randInvoice(1000)
assert.Nil(t, err) require.NoError(t, err)
invoice2, err := randInvoice(20000) invoice2, err := randInvoice(20000)
assert.Nil(t, err) require.NoError(t, err)
invoice2.Terms.PaymentAddr = invoice1.Terms.PaymentAddr invoice2.Terms.PaymentAddr = invoice1.Terms.PaymentAddr
// First insert should succeed. // First insert should succeed.
inv1Hash := invoice1.Terms.PaymentPreimage.Hash() inv1Hash := invoice1.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(invoice1, inv1Hash) _, err = db.AddInvoice(invoice1, inv1Hash)
assert.Nil(t, err) require.NoError(t, err)
// Second insert should fail with duplicate payment addr. // Second insert should fail with duplicate payment addr.
inv2Hash := invoice2.Terms.PaymentPreimage.Hash() inv2Hash := invoice2.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(invoice2, inv2Hash) _, err = db.AddInvoice(invoice2, inv2Hash)
assert.Equal(t, ErrDuplicatePayAddr, err) require.Error(t, err, ErrDuplicatePayAddr)
} }
// TestInvRefEquivocation asserts that retrieving or updating an invoice using // TestInvRefEquivocation asserts that retrieving or updating an invoice using
@ -319,29 +318,29 @@ func TestAddDuplicatePayAddr(t *testing.T) {
func TestInvRefEquivocation(t *testing.T) { func TestInvRefEquivocation(t *testing.T) {
db, cleanUp, err := makeTestDB() db, cleanUp, err := makeTestDB()
defer cleanUp() defer cleanUp()
assert.Nil(t, err) require.NoError(t, err)
// Add two random invoices. // Add two random invoices.
invoice1, err := randInvoice(1000) invoice1, err := randInvoice(1000)
assert.Nil(t, err) require.NoError(t, err)
inv1Hash := invoice1.Terms.PaymentPreimage.Hash() inv1Hash := invoice1.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(invoice1, inv1Hash) _, err = db.AddInvoice(invoice1, inv1Hash)
assert.Nil(t, err) require.NoError(t, err)
invoice2, err := randInvoice(2000) invoice2, err := randInvoice(2000)
assert.Nil(t, err) require.NoError(t, err)
inv2Hash := invoice2.Terms.PaymentPreimage.Hash() inv2Hash := invoice2.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(invoice2, inv2Hash) _, err = db.AddInvoice(invoice2, inv2Hash)
assert.Nil(t, err) require.NoError(t, err)
// Now, query using invoice 1's payment address, but invoice 2's payment // Now, query using invoice 1's payment address, but invoice 2's payment
// hash. We expect an error since the invref points to multiple // hash. We expect an error since the invref points to multiple
// invoices. // invoices.
ref := InvoiceRefByHashAndAddr(inv2Hash, invoice1.Terms.PaymentAddr) ref := InvoiceRefByHashAndAddr(inv2Hash, invoice1.Terms.PaymentAddr)
_, err = db.LookupInvoice(ref) _, err = db.LookupInvoice(ref)
assert.Equal(t, ErrInvRefEquivocation, err) require.Error(t, err, ErrInvRefEquivocation)
// The same error should be returned when updating an equivocating // The same error should be returned when updating an equivocating
// reference. // reference.
@ -349,7 +348,7 @@ func TestInvRefEquivocation(t *testing.T) {
return nil, nil return nil, nil
} }
_, err = db.UpdateInvoice(ref, nop) _, err = db.UpdateInvoice(ref, nop)
assert.Equal(t, ErrInvRefEquivocation, err) require.Error(t, err, ErrInvRefEquivocation)
} }
// TestInvoiceCancelSingleHtlc tests that a single htlc can be canceled on the // TestInvoiceCancelSingleHtlc tests that a single htlc can be canceled on the
@ -438,7 +437,7 @@ func TestInvoiceAddTimeSeries(t *testing.T) {
} }
_, err = db.InvoicesAddedSince(0) _, err = db.InvoicesAddedSince(0)
assert.Nil(t, err) require.NoError(t, err)
// We'll start off by creating 20 random invoices, and inserting them // We'll start off by creating 20 random invoices, and inserting them
// into the database. // into the database.
@ -501,14 +500,18 @@ func TestInvoiceAddTimeSeries(t *testing.T) {
t.Fatalf("unable to query: %v", err) t.Fatalf("unable to query: %v", err)
} }
if !reflect.DeepEqual(query.resp, resp) { require.Equal(t, len(query.resp), len(resp))
t.Fatalf("test #%v: expected %v, got %v", i,
spew.Sdump(query.resp), spew.Sdump(resp)) for j := 0; j < len(query.resp); j++ {
require.Equal(t,
query.resp[j], resp[j],
fmt.Sprintf("test: #%v, item: #%v", i, j),
)
} }
} }
_, err = db.InvoicesSettledSince(0) _, err = db.InvoicesSettledSince(0)
assert.Nil(t, err) require.NoError(t, err)
var settledInvoices []Invoice var settledInvoices []Invoice
var settleIndex uint64 = 1 var settleIndex uint64 = 1
@ -566,9 +569,13 @@ func TestInvoiceAddTimeSeries(t *testing.T) {
t.Fatalf("unable to query: %v", err) t.Fatalf("unable to query: %v", err)
} }
if !reflect.DeepEqual(query.resp, resp) { require.Equal(t, len(query.resp), len(resp))
t.Fatalf("test #%v: expected %v, got %v", i,
spew.Sdump(query.resp), spew.Sdump(resp)) for j := 0; j < len(query.resp); j++ {
require.Equal(t,
query.resp[j], resp[j],
fmt.Sprintf("test: #%v, item: #%v", i, j),
)
} }
} }
} }
@ -654,14 +661,11 @@ func TestFetchAllInvoicesWithPaymentHash(t *testing.T) {
pendingInvoices[i].PaymentHash) pendingInvoices[i].PaymentHash)
} }
// Zero out add index to not confuse DeepEqual. // Zero out add index to not confuse require.Equal.
pendingInvoices[i].Invoice.AddIndex = 0 pendingInvoices[i].Invoice.AddIndex = 0
expected.AddIndex = 0 expected.AddIndex = 0
if !reflect.DeepEqual(*expected, pendingInvoices[i].Invoice) { require.Equal(t, *expected, pendingInvoices[i].Invoice)
t.Fatalf("expected: %v, got: %v",
spew.Sdump(expected), spew.Sdump(pendingInvoices[i].Invoice))
}
} }
for i := range allInvoices { for i := range allInvoices {
@ -671,14 +675,11 @@ func TestFetchAllInvoicesWithPaymentHash(t *testing.T) {
allInvoices[i].PaymentHash) allInvoices[i].PaymentHash)
} }
// Zero out add index to not confuse DeepEqual. // Zero out add index to not confuse require.Equal.
allInvoices[i].Invoice.AddIndex = 0 allInvoices[i].Invoice.AddIndex = 0
expected.AddIndex = 0 expected.AddIndex = 0
if !reflect.DeepEqual(*expected, allInvoices[i].Invoice) { require.Equal(t, *expected, allInvoices[i].Invoice)
t.Fatalf("expected: %v, got: %v",
spew.Sdump(expected), spew.Sdump(allInvoices[i].Invoice))
}
} }
} }
@ -732,10 +733,7 @@ func TestDuplicateSettleInvoice(t *testing.T) {
} }
// We should get back the exact same invoice that we just inserted. // We should get back the exact same invoice that we just inserted.
if !reflect.DeepEqual(dbInvoice, invoice) { require.Equal(t, invoice, dbInvoice, "wrong invoice after settle")
t.Fatalf("wrong invoice after settle, expected %v got %v",
spew.Sdump(invoice), spew.Sdump(dbInvoice))
}
// If we try to settle the invoice again, then we should get the very // If we try to settle the invoice again, then we should get the very
// same invoice back, but with an error this time. // same invoice back, but with an error this time.
@ -749,10 +747,7 @@ func TestDuplicateSettleInvoice(t *testing.T) {
} }
invoice.SettleDate = dbInvoice.SettleDate invoice.SettleDate = dbInvoice.SettleDate
if !reflect.DeepEqual(dbInvoice, invoice) { require.Equal(t, invoice, dbInvoice, "wrong invoice after second settle")
t.Fatalf("wrong invoice after second settle, expected %v got %v",
spew.Sdump(invoice), spew.Sdump(dbInvoice))
}
} }
// TestQueryInvoices ensures that we can properly query the invoice database for // TestQueryInvoices ensures that we can properly query the invoice database for
@ -1020,11 +1015,13 @@ func TestQueryInvoices(t *testing.T) {
t.Fatalf("unable to query invoice database: %v", err) t.Fatalf("unable to query invoice database: %v", err)
} }
if !reflect.DeepEqual(response.Invoices, testCase.expected) { require.Equal(t, len(testCase.expected), len(response.Invoices))
t.Fatalf("test #%d: query returned incorrect set of "+
"invoices: expcted %v, got %v", i, for j, expected := range testCase.expected {
spew.Sdump(response.Invoices), require.Equal(t,
spew.Sdump(testCase.expected)) expected, response.Invoices[j],
fmt.Sprintf("test: #%v, item: #%v", i, j),
)
} }
} }
} }
@ -1118,9 +1115,11 @@ func TestCustomRecords(t *testing.T) {
if len(dbInvoice.Htlcs) != 1 { if len(dbInvoice.Htlcs) != 1 {
t.Fatalf("expected the htlc to be added") t.Fatalf("expected the htlc to be added")
} }
if !reflect.DeepEqual(records, dbInvoice.Htlcs[key].CustomRecords) {
t.Fatalf("invalid custom records") require.Equal(t,
} records, dbInvoice.Htlcs[key].CustomRecords,
"invalid custom records",
)
} }
// TestInvoiceRef asserts that the proper identifiers are returned from an // TestInvoiceRef asserts that the proper identifiers are returned from an
@ -1132,12 +1131,12 @@ func TestInvoiceRef(t *testing.T) {
// An InvoiceRef by hash should return the provided hash and a nil // An InvoiceRef by hash should return the provided hash and a nil
// payment addr. // payment addr.
refByHash := InvoiceRefByHash(payHash) refByHash := InvoiceRefByHash(payHash)
assert.Equal(t, payHash, refByHash.PayHash()) require.Equal(t, payHash, refByHash.PayHash())
assert.Equal(t, (*[32]byte)(nil), refByHash.PayAddr()) require.Equal(t, (*[32]byte)(nil), refByHash.PayAddr())
// An InvoiceRef by hash and addr should return the payment hash and // An InvoiceRef by hash and addr should return the payment hash and
// payment addr passed to the constructor. // payment addr passed to the constructor.
refByHashAndAddr := InvoiceRefByHashAndAddr(payHash, payAddr) refByHashAndAddr := InvoiceRefByHashAndAddr(payHash, payAddr)
assert.Equal(t, payHash, refByHashAndAddr.PayHash()) require.Equal(t, payHash, refByHashAndAddr.PayHash())
assert.Equal(t, &payAddr, refByHashAndAddr.PayAddr()) require.Equal(t, &payAddr, refByHashAndAddr.PayAddr())
} }