channeldb/invoice_test: refactor TestQueryInvoices and add reverse test cases

This commit is contained in:
Wilmer Paulino 2018-09-10 18:20:38 -07:00
parent 0fe35e0014
commit 7e6eb44cf8
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F

@ -377,7 +377,7 @@ func TestDuplicateSettleInvoice(t *testing.T) {
}
// 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) {
t.Parallel()
@ -387,11 +387,11 @@ func TestQueryInvoices(t *testing.T) {
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
// as the amount of the invoice itself.
const numInvoices = 100
for i := lnwire.MilliSatoshi(0); i < numInvoices; i++ {
const numInvoices = 50
for i := lnwire.MilliSatoshi(1); i <= numInvoices; i++ {
invoice, err := randInvoice(i)
if err != nil {
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
// start with a simple query to retrieve all invoices.
query := InvoiceQuery{
NumMaxInvoices: numInvoices,
}
res, err := db.QueryInvoices(query)
// We'll then retrieve the set of all invoices and pending invoices.
// This will serve useful when comparing the expected responses of the
// query with the actual ones.
invoices, err := db.FetchAllInvoices(false)
if err != nil {
t.Fatalf("unable to query invoices: %v", err)
t.Fatalf("unable to retrieve invoices: %v", err)
}
if len(res.Invoices) != numInvoices {
t.Fatalf("expected %d invoices, got %d", numInvoices,
len(res.Invoices))
pendingInvoices, err := db.FetchAllInvoices(true)
if err != nil {
t.Fatalf("unable to retrieve pending invoices: %v", err)
}
// Now, we'll limit the query to only return the latest 30 invoices.
query.IndexOffset = 70
res, err = db.QueryInvoices(query)
if err != nil {
t.Fatalf("unable to query 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,
},
expected: invoices,
},
// Fetch the first 25 invoices.
{
query: InvoiceQuery{
NumMaxInvoices: numInvoices / 2,
},
expected: invoices[:numInvoices/2],
},
// Fetch the first 10 invoices, but this time iterating
// backwards.
{
query: InvoiceQuery{
IndexOffset: 11,
Reversed: true,
NumMaxInvoices: numInvoices,
},
expected: invoices[:10],
},
// Fetch the last 40 invoices.
{
query: InvoiceQuery{
IndexOffset: 10,
NumMaxInvoices: numInvoices,
},
expected: invoices[10:],
},
// Fetch all pending invoices with a single query.
{
query: InvoiceQuery{
PendingOnly: true,
NumMaxInvoices: numInvoices,
},
expected: pendingInvoices,
},
// Fetch the first 12 pending invoices.
{
query: InvoiceQuery{
PendingOnly: true,
NumMaxInvoices: numInvoices / 4,
},
expected: pendingInvoices[:len(pendingInvoices)/2],
},
// Fetch the first 5 pending invoices, but this time iterating
// backwards.
{
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:],
},
}
if uint32(len(res.Invoices)) != numInvoices-query.IndexOffset {
t.Fatalf("expected %d invoices, got %d",
numInvoices-query.IndexOffset, len(res.Invoices))
}
for _, invoice := range res.Invoices {
if uint32(invoice.Terms.Value) < query.IndexOffset {
t.Fatalf("found invoice with index %v before offset %v",
invoice.Terms.Value, query.IndexOffset)
for i, testCase := range testCases {
response, err := db.QueryInvoices(testCase.query)
if err != nil {
t.Fatalf("unable to query invoice database: %v", err)
}
}
// Limit the query from above to return 25 invoices max.
query.NumMaxInvoices = 25
res, err = db.QueryInvoices(query)
if err != nil {
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,
NumMaxInvoices: numInvoices,
}
res, err = db.QueryInvoices(query)
if err != nil {
t.Fatalf("unable to query invoices: %v", err)
}
// Since only invoices with even amounts were settled, we should see
// that there are 50 invoices within the response.
if len(res.Invoices) != numInvoices/2 {
t.Fatalf("expected %d pending invoices, got %d", numInvoices/2,
len(res.Invoices))
}
for _, invoice := range res.Invoices {
if invoice.Terms.Value%2 == 0 {
t.Fatal("retrieved unexpected settled invoice")
}
}
// Finally, we'll skip the first 10 invoices from the set of unsettled
// invoices.
query.IndexOffset = 10
res, err = db.QueryInvoices(query)
if err != nil {
t.Fatalf("unable to query invoices: %v", err)
}
if uint32(len(res.Invoices)) != (numInvoices/2)-query.IndexOffset {
t.Fatalf("expected %d invoices, got %d",
(numInvoices/2)-query.IndexOffset, len(res.Invoices))
}
// To ensure the correct invoices were returned, we'll make sure each
// invoice has an odd value (meaning unsettled). Since the 10 invoices
// 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)
if !reflect.DeepEqual(response.Invoices, testCase.expected) {
t.Fatalf("test #%d: query returned incorrect set of "+
"invoices: expcted %v, got %v", i,
spew.Sdump(response.Invoices),
spew.Sdump(testCase.expected))
}
}
}