From 7f6fbd4533fb65b1e0380b4584647503f654a7e2 Mon Sep 17 00:00:00 2001 From: Vadym Popov Date: Sun, 12 Aug 2018 16:13:59 +0300 Subject: [PATCH] channeldb: test of payment statuses transitions --- channeldb/payments_test.go | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/channeldb/payments_test.go b/channeldb/payments_test.go index 450b4acf..d13e039d 100644 --- a/channeldb/payments_test.go +++ b/channeldb/payments_test.go @@ -40,6 +40,14 @@ func makeFakePayment() *OutgoingPayment { return fakePayment } +func makeFakePaymentHash() [32]byte { + var paymentHash [32]byte + rBytes, _ := randomBytes(0, 32) + copy(paymentHash[:], rBytes) + + return paymentHash +} + // randomBytes creates random []byte with length in range [minLen, maxLen) func randomBytes(minLen, maxLen int) ([]byte, error) { randBuf := make([]byte, minLen+rand.Intn(maxLen-minLen)) @@ -195,3 +203,51 @@ func TestOutgoingPaymentWorkflow(t *testing.T) { len(paymentsAfterDeletion), 0) } } + +func TestPaymentStatusWorkflow(t *testing.T) { + t.Parallel() + + db, cleanUp, err := makeTestDB() + defer cleanUp() + if err != nil { + t.Fatalf("unable to make test db: %v", err) + } + + testCases := []struct { + paymentHash [32]byte + status PaymentStatus + }{ + { + paymentHash: makeFakePaymentHash(), + status: StatusGrounded, + }, + { + paymentHash: makeFakePaymentHash(), + status: StatusInFlight, + }, + { + paymentHash: makeFakePaymentHash(), + status: StatusCompleted, + }, + } + + for _, testCase := range testCases { + err := db.UpdatePaymentStatus(testCase.paymentHash, testCase.status) + if err != nil { + t.Fatalf("unable to put payment in DB: %v", err) + } + + status, err := db.FetchPaymentStatus(testCase.paymentHash) + if err != nil { + t.Fatalf("unable to fetch payments from DB: %v", err) + } + + if status != testCase.status { + t.Fatalf("Wrong payments status after reading from DB."+ + "Got %v, want %v", + spew.Sdump(status), + spew.Sdump(testCase.status), + ) + } + } +}