channeldb/invoice_test: refactor TestQueryInvoices and add reverse test cases
This commit is contained in:
parent
0fe35e0014
commit
7e6eb44cf8
@ -377,7 +377,7 @@ func TestDuplicateSettleInvoice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestQueryInvoices ensures that we can properly query the invoice database for
|
// TestQueryInvoices ensures that we can properly query the invoice database for
|
||||||
// invoices between specific time intervals.
|
// invoices using different types of queries.
|
||||||
func TestQueryInvoices(t *testing.T) {
|
func TestQueryInvoices(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@ -387,11 +387,11 @@ func TestQueryInvoices(t *testing.T) {
|
|||||||
t.Fatalf("unable to make test db: %v", err)
|
t.Fatalf("unable to make test db: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// To begin the test, we'll add 100 invoices to the database. We'll
|
// To begin the test, we'll add 50 invoices to the database. We'll
|
||||||
// assume that the index of the invoice within the database is the same
|
// assume that the index of the invoice within the database is the same
|
||||||
// as the amount of the invoice itself.
|
// as the amount of the invoice itself.
|
||||||
const numInvoices = 100
|
const numInvoices = 50
|
||||||
for i := lnwire.MilliSatoshi(0); i < numInvoices; i++ {
|
for i := lnwire.MilliSatoshi(1); i <= numInvoices; i++ {
|
||||||
invoice, err := randInvoice(i)
|
invoice, err := randInvoice(i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create invoice: %v", err)
|
t.Fatalf("unable to create invoice: %v", err)
|
||||||
@ -410,93 +410,112 @@ func TestQueryInvoices(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// With the invoices created, we can begin querying the database. We'll
|
// We'll then retrieve the set of all invoices and pending invoices.
|
||||||
// start with a simple query to retrieve all invoices.
|
// This will serve useful when comparing the expected responses of the
|
||||||
query := InvoiceQuery{
|
// query with the actual ones.
|
||||||
|
invoices, err := db.FetchAllInvoices(false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to retrieve invoices: %v", err)
|
||||||
|
}
|
||||||
|
pendingInvoices, err := db.FetchAllInvoices(true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to retrieve pending invoices: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The test will consist of several queries along with their respective
|
||||||
|
// expected response. Each query response should match its expected one.
|
||||||
|
testCases := []struct {
|
||||||
|
query InvoiceQuery
|
||||||
|
expected []Invoice
|
||||||
|
}{
|
||||||
|
// Fetch all invoices with a single query.
|
||||||
|
{
|
||||||
|
query: InvoiceQuery{
|
||||||
NumMaxInvoices: numInvoices,
|
NumMaxInvoices: numInvoices,
|
||||||
}
|
},
|
||||||
res, err := db.QueryInvoices(query)
|
expected: invoices,
|
||||||
if err != nil {
|
},
|
||||||
t.Fatalf("unable to query invoices: %v", err)
|
// Fetch the first 25 invoices.
|
||||||
}
|
{
|
||||||
if len(res.Invoices) != numInvoices {
|
query: InvoiceQuery{
|
||||||
t.Fatalf("expected %d invoices, got %d", numInvoices,
|
NumMaxInvoices: numInvoices / 2,
|
||||||
len(res.Invoices))
|
},
|
||||||
}
|
expected: invoices[:numInvoices/2],
|
||||||
|
},
|
||||||
// Now, we'll limit the query to only return the latest 30 invoices.
|
// Fetch the first 10 invoices, but this time iterating
|
||||||
query.IndexOffset = 70
|
// backwards.
|
||||||
res, err = db.QueryInvoices(query)
|
{
|
||||||
if err != nil {
|
query: InvoiceQuery{
|
||||||
t.Fatalf("unable to query invoices: %v", err)
|
IndexOffset: 11,
|
||||||
}
|
Reversed: true,
|
||||||
if uint32(len(res.Invoices)) != numInvoices-query.IndexOffset {
|
NumMaxInvoices: numInvoices,
|
||||||
t.Fatalf("expected %d invoices, got %d",
|
},
|
||||||
numInvoices-query.IndexOffset, len(res.Invoices))
|
expected: invoices[:10],
|
||||||
}
|
},
|
||||||
for _, invoice := range res.Invoices {
|
// Fetch the last 40 invoices.
|
||||||
if uint32(invoice.Terms.Value) < query.IndexOffset {
|
{
|
||||||
t.Fatalf("found invoice with index %v before offset %v",
|
query: InvoiceQuery{
|
||||||
invoice.Terms.Value, query.IndexOffset)
|
IndexOffset: 10,
|
||||||
}
|
NumMaxInvoices: numInvoices,
|
||||||
}
|
},
|
||||||
|
expected: invoices[10:],
|
||||||
// Limit the query from above to return 25 invoices max.
|
},
|
||||||
query.NumMaxInvoices = 25
|
// Fetch all pending invoices with a single query.
|
||||||
res, err = db.QueryInvoices(query)
|
{
|
||||||
if err != nil {
|
query: InvoiceQuery{
|
||||||
t.Fatalf("unable to query invoices: %v", err)
|
|
||||||
}
|
|
||||||
if uint32(len(res.Invoices)) != query.NumMaxInvoices {
|
|
||||||
t.Fatalf("expected %d invoices, got %d", query.NumMaxInvoices,
|
|
||||||
len(res.Invoices))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset the query to fetch all unsettled invoices within the time
|
|
||||||
// slice.
|
|
||||||
query = InvoiceQuery{
|
|
||||||
PendingOnly: true,
|
PendingOnly: true,
|
||||||
NumMaxInvoices: numInvoices,
|
NumMaxInvoices: numInvoices,
|
||||||
}
|
},
|
||||||
res, err = db.QueryInvoices(query)
|
expected: pendingInvoices,
|
||||||
if err != nil {
|
},
|
||||||
t.Fatalf("unable to query invoices: %v", err)
|
// Fetch the first 12 pending invoices.
|
||||||
}
|
{
|
||||||
// Since only invoices with even amounts were settled, we should see
|
query: InvoiceQuery{
|
||||||
// that there are 50 invoices within the response.
|
PendingOnly: true,
|
||||||
if len(res.Invoices) != numInvoices/2 {
|
NumMaxInvoices: numInvoices / 4,
|
||||||
t.Fatalf("expected %d pending invoices, got %d", numInvoices/2,
|
},
|
||||||
len(res.Invoices))
|
expected: pendingInvoices[:len(pendingInvoices)/2],
|
||||||
}
|
},
|
||||||
for _, invoice := range res.Invoices {
|
// Fetch the first 5 pending invoices, but this time iterating
|
||||||
if invoice.Terms.Value%2 == 0 {
|
// backwards.
|
||||||
t.Fatal("retrieved unexpected settled invoice")
|
{
|
||||||
}
|
query: InvoiceQuery{
|
||||||
|
IndexOffset: 10,
|
||||||
|
PendingOnly: true,
|
||||||
|
Reversed: true,
|
||||||
|
NumMaxInvoices: numInvoices,
|
||||||
|
},
|
||||||
|
// Since we seek to the invoice with index 10 and
|
||||||
|
// iterate backwards, there should only be 5 pending
|
||||||
|
// invoices before it as every other invoice within the
|
||||||
|
// index is settled.
|
||||||
|
expected: pendingInvoices[:5],
|
||||||
|
},
|
||||||
|
// Fetch the last 15 invoices.
|
||||||
|
{
|
||||||
|
query: InvoiceQuery{
|
||||||
|
IndexOffset: 20,
|
||||||
|
PendingOnly: true,
|
||||||
|
NumMaxInvoices: numInvoices,
|
||||||
|
},
|
||||||
|
// Since we seek to the invoice with index 20, there are
|
||||||
|
// 30 invoices left. From these 30, only 15 of them are
|
||||||
|
// still pending.
|
||||||
|
expected: pendingInvoices[len(pendingInvoices)-15:],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, we'll skip the first 10 invoices from the set of unsettled
|
for i, testCase := range testCases {
|
||||||
// invoices.
|
response, err := db.QueryInvoices(testCase.query)
|
||||||
query.IndexOffset = 10
|
|
||||||
res, err = db.QueryInvoices(query)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to query invoices: %v", err)
|
t.Fatalf("unable to query invoice database: %v", err)
|
||||||
}
|
}
|
||||||
if uint32(len(res.Invoices)) != (numInvoices/2)-query.IndexOffset {
|
|
||||||
t.Fatalf("expected %d invoices, got %d",
|
if !reflect.DeepEqual(response.Invoices, testCase.expected) {
|
||||||
(numInvoices/2)-query.IndexOffset, len(res.Invoices))
|
t.Fatalf("test #%d: query returned incorrect set of "+
|
||||||
}
|
"invoices: expcted %v, got %v", i,
|
||||||
// To ensure the correct invoices were returned, we'll make sure each
|
spew.Sdump(response.Invoices),
|
||||||
// invoice has an odd value (meaning unsettled). Since the 10 invoices
|
spew.Sdump(testCase.expected))
|
||||||
// skipped should be unsettled, the value of the invoice must be at
|
|
||||||
// least the index of the 11th unsettled invoice.
|
|
||||||
for _, invoice := range res.Invoices {
|
|
||||||
if uint32(invoice.Terms.Value) < query.IndexOffset*2 {
|
|
||||||
t.Fatalf("found invoice with index %v before offset %v",
|
|
||||||
invoice.Terms.Value, query.IndexOffset*2)
|
|
||||||
}
|
|
||||||
if invoice.Terms.Value%2 == 0 {
|
|
||||||
t.Fatalf("found unexpected settled invoice with index %v",
|
|
||||||
invoice.Terms.Value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user