@@ -151,3 +151,87 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
151151 . expect ( "Failed to get next collection ID" )
152152 }
153153}
154+
155+ #[ cfg( test) ]
156+ mod tests {
157+ use crate :: { mock:: * , tests:: * , Currency , Error , ReservableCurrency } ;
158+
159+ #[ test]
160+ fn increment_account_balance_works ( ) {
161+ new_test_ext ( ) . execute_with ( || {
162+ let collection_id = 0 ;
163+ let deposit_account = account ( 1 ) ;
164+ let deposit_amount = balance_deposit ( ) ;
165+ let owner = account ( 2 ) ;
166+ assert_noop ! (
167+ Nfts :: increment_account_balance(
168+ collection_id,
169+ & owner,
170+ ( & deposit_account, deposit_amount)
171+ ) ,
172+ BalancesError :: <Test >:: InsufficientBalance
173+ ) ;
174+ Balances :: make_free_balance_be ( & deposit_account, 100 ) ;
175+ // Initialize `AccountBalance` and increase the collection item count for the new
176+ // account.
177+ assert_ok ! ( Nfts :: increment_account_balance(
178+ collection_id,
179+ & owner,
180+ ( & deposit_account, deposit_amount)
181+ ) ) ;
182+ assert_eq ! ( Balances :: reserved_balance( & deposit_account) , deposit_amount) ;
183+ assert_eq ! (
184+ AccountBalance :: get( collection_id, & owner) ,
185+ Some ( ( 1 , ( deposit_account. clone( ) , deposit_amount) ) )
186+ ) ;
187+ // Increment the balance of a non-zero balance account. No additional reserves.
188+ assert_ok ! ( Nfts :: increment_account_balance(
189+ collection_id,
190+ & owner,
191+ ( & deposit_account, deposit_amount)
192+ ) ) ;
193+ assert_eq ! ( Balances :: reserved_balance( & deposit_account) , deposit_amount) ;
194+ assert_eq ! (
195+ AccountBalance :: get( collection_id, & owner) ,
196+ Some ( ( 2 , ( deposit_account. clone( ) , deposit_amount) ) )
197+ ) ;
198+ } ) ;
199+ }
200+
201+ #[ test]
202+ fn decrement_account_balance_works ( ) {
203+ new_test_ext ( ) . execute_with ( || {
204+ let collection_id = 0 ;
205+ let balance = 2u32 ;
206+ let deposit_account = account ( 1 ) ;
207+ let deposit_amount = balance_deposit ( ) ;
208+ let owner = account ( 2 ) ;
209+
210+ Balances :: make_free_balance_be ( & deposit_account, 100 ) ;
211+ // Decrement non-existing `AccountBalance` record.
212+ assert_noop ! (
213+ Nfts :: decrement_account_balance( collection_id, & deposit_account) ,
214+ Error :: <Test >:: NoItemOwned
215+ ) ;
216+ // Set account balance and reserve `DepositBalance`.
217+ AccountBalance :: insert (
218+ collection_id,
219+ & owner,
220+ ( & balance, ( & deposit_account, deposit_amount) ) ,
221+ ) ;
222+ Balances :: reserve ( & deposit_account, deposit_amount) . expect ( "should work" ) ;
223+ // Successfully decrement the value of the `AccountBalance` entry.
224+ assert_ok ! ( Nfts :: decrement_account_balance( collection_id, & owner) ) ;
225+ assert_eq ! (
226+ AccountBalance :: get( collection_id, & owner) ,
227+ Some ( ( balance - 1 , ( deposit_account. clone( ) , deposit_amount) ) )
228+ ) ;
229+ assert_eq ! ( Balances :: reserved_balance( & deposit_account) , deposit_amount) ;
230+ // `AccountBalance` record is deleted, and the depositor's funds are unreserved if
231+ // the `AccountBalance` value reaches zero after decrementing.
232+ assert_ok ! ( Nfts :: decrement_account_balance( collection_id, & owner) ) ;
233+ assert_eq ! ( Balances :: reserved_balance( & deposit_account) , 0 ) ;
234+ assert ! ( !AccountBalance :: contains_key( collection_id, & owner) ) ;
235+ } ) ;
236+ }
237+ }
0 commit comments