@@ -2,6 +2,7 @@ package accounts
2
2
3
3
import (
4
4
"context"
5
+ "github.com/lightningnetwork/lnd/lnwire"
5
6
"testing"
6
7
"time"
7
8
@@ -71,6 +72,18 @@ func TestAccountStore(t *testing.T) {
71
72
)
72
73
require .NoError (t , err )
73
74
75
+ // Adjust the account balance by first adding 10000, and then deducting
76
+ // 5000.
77
+ err = store .AdjustAccountBalance (
78
+ ctx , acct1 .ID , lnwire .MilliSatoshi (10000 ), true ,
79
+ )
80
+ require .NoError (t , err )
81
+
82
+ err = store .AdjustAccountBalance (
83
+ ctx , acct1 .ID , lnwire .MilliSatoshi (5000 ), false ,
84
+ )
85
+ require .NoError (t , err )
86
+
74
87
// Update the in-memory account so that we can compare it with the
75
88
// account we get from the store.
76
89
acct1 .CurrentBalance = - 500
@@ -85,11 +98,32 @@ func TestAccountStore(t *testing.T) {
85
98
}
86
99
acct1 .Invoices [lntypes.Hash {12 , 34 , 56 , 78 }] = struct {}{}
87
100
acct1 .Invoices [lntypes.Hash {34 , 56 , 78 , 90 }] = struct {}{}
101
+ acct1 .CurrentBalance += 10000
102
+ acct1 .CurrentBalance -= 5000
103
+
104
+ dbAccount , err = store .Account (ctx , acct1 .ID )
105
+ require .NoError (t , err )
106
+ assertEqualAccounts (t , acct1 , dbAccount )
107
+
108
+ // Test that adjusting the balance to exactly 0 should work, while
109
+ // adjusting the balance to below 0 should fail.
110
+ err = store .AdjustAccountBalance (
111
+ ctx , acct1 .ID , lnwire .MilliSatoshi (acct1 .CurrentBalance ), false ,
112
+ )
113
+ require .NoError (t , err )
114
+
115
+ acct1 .CurrentBalance = 0
88
116
89
117
dbAccount , err = store .Account (ctx , acct1 .ID )
90
118
require .NoError (t , err )
91
119
assertEqualAccounts (t , acct1 , dbAccount )
92
120
121
+ // Adjusting the value to below 0 should fail.
122
+ err = store .AdjustAccountBalance (
123
+ ctx , acct1 .ID , lnwire .MilliSatoshi (1 ), false ,
124
+ )
125
+ require .ErrorContains (t , err , "balance would be below 0" )
126
+
93
127
// Sleep just a tiny bit to make sure we are never too quick to measure
94
128
// the expiry, even though the time is nanosecond scale and writing to
95
129
// the store and reading again should take at least a couple of
0 commit comments