Skip to content
34 changes: 34 additions & 0 deletions accounts/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package accounts

import (
"context"
"github.com/lightningnetwork/lnd/lnwire"
"testing"
"time"

Expand Down Expand Up @@ -71,6 +72,18 @@ func TestAccountStore(t *testing.T) {
)
require.NoError(t, err)

// Adjust the account balance by first adding 10000, and then deducting
// 5000.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for review it's nicer if unit tests are in the same commit as the new addition :)

err = store.AdjustAccountBalance(
ctx, acct1.ID, lnwire.MilliSatoshi(10000), true,
)
require.NoError(t, err)

err = store.AdjustAccountBalance(
ctx, acct1.ID, lnwire.MilliSatoshi(5000), false,
)
require.NoError(t, err)

// Update the in-memory account so that we can compare it with the
// account we get from the store.
acct1.CurrentBalance = -500
Expand All @@ -85,11 +98,32 @@ func TestAccountStore(t *testing.T) {
}
acct1.Invoices[lntypes.Hash{12, 34, 56, 78}] = struct{}{}
acct1.Invoices[lntypes.Hash{34, 56, 78, 90}] = struct{}{}
acct1.CurrentBalance += 10000
acct1.CurrentBalance -= 5000

dbAccount, err = store.Account(ctx, acct1.ID)
require.NoError(t, err)
assertEqualAccounts(t, acct1, dbAccount)

// Test that adjusting the balance to exactly 0 should work, while
// adjusting the balance to below 0 should fail.
err = store.AdjustAccountBalance(
ctx, acct1.ID, lnwire.MilliSatoshi(acct1.CurrentBalance), false,
)
require.NoError(t, err)

acct1.CurrentBalance = 0

dbAccount, err = store.Account(ctx, acct1.ID)
require.NoError(t, err)
assertEqualAccounts(t, acct1, dbAccount)

// Adjusting the value to below 0 should fail.
err = store.AdjustAccountBalance(
ctx, acct1.ID, lnwire.MilliSatoshi(1), false,
)
require.ErrorContains(t, err, "balance would be below 0")

// Sleep just a tiny bit to make sure we are never too quick to measure
// the expiry, even though the time is nanosecond scale and writing to
// the store and reading again should take at least a couple of
Expand Down