invoices+test: replace DeepEqual + spew dump with testify

This commit replaces reflect.DeepEqual tests and spew.Dump prints with
testify's require.Equal to make diffs smaller and test outputs more
readable.
This commit is contained in:
Andras Banki-Horvath 2020-06-05 15:02:06 +02:00
parent be36776120
commit 574bbe5eba
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8

View File

@ -2,16 +2,16 @@ 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/assert"
"github.com/stretchr/testify/require"
) )
var ( var (
@ -185,10 +185,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 +281,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]) { assert.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])) )
}
} }
} }
@ -501,9 +501,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),
)
} }
} }
@ -566,9 +570,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 +662,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 +676,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 +734,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 +748,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 +1016,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 +1116,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 +1132,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())
} }