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.
|
||||||
NumMaxInvoices: numInvoices,
|
invoices, err := db.FetchAllInvoices(false)
|
||||||
}
|
|
||||||
res, err := db.QueryInvoices(query)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to query invoices: %v", err)
|
t.Fatalf("unable to retrieve invoices: %v", err)
|
||||||
}
|
}
|
||||||
if len(res.Invoices) != numInvoices {
|
pendingInvoices, err := db.FetchAllInvoices(true)
|
||||||
t.Fatalf("expected %d invoices, got %d", numInvoices,
|
if err != nil {
|
||||||
len(res.Invoices))
|
t.Fatalf("unable to retrieve pending invoices: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now, we'll limit the query to only return the latest 30 invoices.
|
// The test will consist of several queries along with their respective
|
||||||
query.IndexOffset = 70
|
// expected response. Each query response should match its expected one.
|
||||||
res, err = db.QueryInvoices(query)
|
testCases := []struct {
|
||||||
if err != nil {
|
query InvoiceQuery
|
||||||
t.Fatalf("unable to query invoices: %v", err)
|
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",
|
for i, testCase := range testCases {
|
||||||
numInvoices-query.IndexOffset, len(res.Invoices))
|
response, err := db.QueryInvoices(testCase.query)
|
||||||
}
|
if err != nil {
|
||||||
for _, invoice := range res.Invoices {
|
t.Fatalf("unable to query invoice database: %v", err)
|
||||||
if uint32(invoice.Terms.Value) < query.IndexOffset {
|
|
||||||
t.Fatalf("found invoice with index %v before offset %v",
|
|
||||||
invoice.Terms.Value, query.IndexOffset)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Limit the query from above to return 25 invoices max.
|
if !reflect.DeepEqual(response.Invoices, testCase.expected) {
|
||||||
query.NumMaxInvoices = 25
|
t.Fatalf("test #%d: query returned incorrect set of "+
|
||||||
res, err = db.QueryInvoices(query)
|
"invoices: expcted %v, got %v", i,
|
||||||
if err != nil {
|
spew.Sdump(response.Invoices),
|
||||||
t.Fatalf("unable to query invoices: %v", err)
|
spew.Sdump(testCase.expected))
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user