@@ -5,8 +5,8 @@ use primitives::{sentry::Pagination, spender::Spendable, Address, ChannelId};
5
5
use super :: { DbPool , PoolError } ;
6
6
7
7
/// ```text
8
- /// INSERT INTO spendable (spender, channel_id, total, still_on_create2)
9
- /// values ('0xce07CbB7e054514D590a0262C93070D838bFBA2e', '0x061d5e2a67d0a9a10f1c732bca12a676d83f79663a396f7d87b3e30b9b411088', 10.00000000, 2.00000000);
8
+ /// INSERT INTO spendable (spender, channel_id, total, still_on_create2, created )
9
+ /// values ('0xce07CbB7e054514D590a0262C93070D838bFBA2e', '0x061d5e2a67d0a9a10f1c732bca12a676d83f79663a396f7d87b3e30b9b411088', 10.00000000, 2.00000000, NOW() );
10
10
/// ```
11
11
pub async fn insert_spendable ( pool : DbPool , spendable : & Spendable ) -> Result < bool , PoolError > {
12
12
let client = pool. get ( ) . await ?;
@@ -114,8 +114,11 @@ async fn list_spendable_total_count<'a>(
114
114
115
115
#[ cfg( test) ]
116
116
mod test {
117
+ use std:: collections:: HashMap ;
118
+
117
119
use primitives:: {
118
120
spender:: Spendable ,
121
+ test_util:: { ADVERTISER , CREATOR , FOLLOWER , GUARDIAN , GUARDIAN_2 , PUBLISHER } ,
119
122
util:: tests:: prep_db:: { ADDRESSES , DUMMY_CAMPAIGN } ,
120
123
Deposit , UnifiedNum ,
121
124
} ;
@@ -165,12 +168,21 @@ mod test {
165
168
. expect ( "Should fetch successfully" ) ;
166
169
167
170
assert_eq ! ( Some ( spendable) , fetched_spendable) ;
171
+ }
168
172
169
- // TODO: Update spendable
173
+ fn new_spendable_with ( spender : & Address ) -> Spendable {
174
+ Spendable {
175
+ spender : * spender,
176
+ channel : DUMMY_CAMPAIGN . channel ,
177
+ deposit : Deposit {
178
+ total : UnifiedNum :: from ( 100_000_000 ) ,
179
+ still_on_create2 : UnifiedNum :: from ( 500_000 ) ,
180
+ } ,
181
+ }
170
182
}
171
183
172
184
#[ tokio:: test]
173
- async fn gets_all_spendables_for_channel ( ) {
185
+ async fn insert_and_get_single_spendable_for_channel ( ) {
174
186
let database = DATABASE_POOL . get ( ) . await . expect ( "Should get a DB pool" ) ;
175
187
176
188
setup_test_migrations ( database. pool . clone ( ) )
@@ -179,7 +191,7 @@ mod test {
179
191
180
192
let channel = DUMMY_CAMPAIGN . channel ;
181
193
182
- insert_channel ( & database, DUMMY_CAMPAIGN . channel )
194
+ insert_channel ( & database, channel)
183
195
. await
184
196
. expect ( "Should insert" ) ;
185
197
@@ -192,14 +204,7 @@ mod test {
192
204
assert_eq ! ( pagination. total_pages, 1 ) ;
193
205
194
206
// Test for 1 pages
195
- let spendable_user = Spendable {
196
- spender : ADDRESSES [ "user" ] ,
197
- channel : DUMMY_CAMPAIGN . channel ,
198
- deposit : Deposit {
199
- total : UnifiedNum :: from ( 100_000_000 ) ,
200
- still_on_create2 : UnifiedNum :: from ( 500_000 ) ,
201
- } ,
202
- } ;
207
+ let spendable_user = new_spendable_with ( & FOLLOWER ) ;
203
208
204
209
insert_spendable ( database. pool . clone ( ) , & spendable_user)
205
210
. await
@@ -215,96 +220,98 @@ mod test {
215
220
page : 0 ,
216
221
total_pages : 1 ,
217
222
} ;
218
- assert_eq ! ( spendables, expected_spendables) ;
219
- assert_eq ! ( pagination, expected_pagination) ;
223
+ pretty_assertions:: assert_eq!( spendables, expected_spendables) ;
224
+ pretty_assertions:: assert_eq!( pagination, expected_pagination) ;
225
+ }
220
226
221
- // Test for multiple pages
222
- let spendable_publisher = Spendable {
223
- spender : ADDRESSES [ "publisher" ] ,
224
- channel : DUMMY_CAMPAIGN . channel ,
225
- deposit : Deposit {
226
- total : UnifiedNum :: from ( 100_000_000 ) ,
227
- still_on_create2 : UnifiedNum :: from ( 500_000 ) ,
228
- } ,
229
- } ;
230
- insert_spendable ( database. pool . clone ( ) , & spendable_publisher)
231
- . await
232
- . expect ( "should insert spendable" ) ;
233
- sleep ( Duration :: from_millis ( 100 ) ) . await ;
227
+ #[ tokio:: test]
228
+ async fn gets_multiple_pages_of_spendables_for_channel ( ) {
229
+ let database = DATABASE_POOL . get ( ) . await . expect ( "Should get a DB pool" ) ;
234
230
235
- let spendable_publisher2 = Spendable {
236
- spender : ADDRESSES [ "publisher2" ] ,
237
- channel : DUMMY_CAMPAIGN . channel ,
238
- deposit : Deposit {
239
- total : UnifiedNum :: from ( 100_000_000 ) ,
240
- still_on_create2 : UnifiedNum :: from ( 500_000 ) ,
241
- } ,
242
- } ;
243
- insert_spendable ( database. pool . clone ( ) , & spendable_publisher2)
231
+ setup_test_migrations ( database. pool . clone ( ) )
244
232
. await
245
- . expect ( "should insert spendable" ) ;
246
- sleep ( Duration :: from_millis ( 100 ) ) . await ;
233
+ . expect ( "Migrations should succeed" ) ;
247
234
248
- let spendable_creator = Spendable {
249
- spender : ADDRESSES [ "creator" ] ,
250
- channel : DUMMY_CAMPAIGN . channel ,
251
- deposit : Deposit {
252
- total : UnifiedNum :: from ( 100_000_000 ) ,
253
- still_on_create2 : UnifiedNum :: from ( 500_000 ) ,
254
- } ,
255
- } ;
256
- insert_spendable ( database. pool . clone ( ) , & spendable_creator)
257
- . await
258
- . expect ( "should insert spendable" ) ;
259
- sleep ( Duration :: from_millis ( 100 ) ) . await ;
235
+ let channel = DUMMY_CAMPAIGN . channel ;
260
236
261
- let spendable_tester = Spendable {
262
- spender : ADDRESSES [ "tester" ] ,
263
- channel : DUMMY_CAMPAIGN . channel ,
264
- deposit : Deposit {
265
- total : UnifiedNum :: from ( 100_000_000 ) ,
266
- still_on_create2 : UnifiedNum :: from ( 500_000 ) ,
267
- } ,
268
- } ;
269
- insert_spendable ( database. pool . clone ( ) , & spendable_tester)
237
+ insert_channel ( & database, channel)
270
238
. await
271
- . expect ( "should insert spendable" ) ;
272
- sleep ( Duration :: from_millis ( 100 ) ) . await ;
273
-
274
- let ( spendables, pagination) =
275
- get_all_spendables_for_channel ( database. clone ( ) , & channel. id ( ) , 0 , 2 )
276
- . await
277
- . expect ( "should get result" ) ;
278
- let expected_spendables = vec ! [ spendable_user, spendable_publisher] ;
279
- let expected_pagination = Pagination {
280
- page : 0 ,
281
- total_pages : 3 ,
282
- } ;
283
- assert_eq ! ( spendables, expected_spendables) ;
284
- assert_eq ! ( pagination, expected_pagination) ;
285
-
286
- let ( spendables, pagination) =
287
- get_all_spendables_for_channel ( database. clone ( ) , & channel. id ( ) , 2 , 2 )
288
- . await
289
- . expect ( "should get result" ) ;
290
- let expected_spendables = vec ! [ spendable_publisher2, spendable_creator] ;
291
- let expected_pagination = Pagination {
292
- page : 1 ,
293
- total_pages : 3 ,
294
- } ;
295
- assert_eq ! ( spendables, expected_spendables) ;
296
- assert_eq ! ( pagination, expected_pagination) ;
239
+ . expect ( "Should insert" ) ;
297
240
298
- let ( spendables, pagination) =
299
- get_all_spendables_for_channel ( database. clone ( ) , & channel. id ( ) , 4 , 2 )
241
+ let create_spendables: Vec < ( Address , Spendable ) > = vec ! [
242
+ ( * PUBLISHER , new_spendable_with( & PUBLISHER ) ) ,
243
+ ( * ADVERTISER , new_spendable_with( & ADVERTISER ) ) ,
244
+ ( * CREATOR , new_spendable_with( & CREATOR ) ) ,
245
+ ( * GUARDIAN , new_spendable_with( & GUARDIAN ) ) ,
246
+ ( * GUARDIAN_2 , new_spendable_with( & GUARDIAN_2 ) ) ,
247
+ ] ;
248
+
249
+ // insert all spendables
250
+ for ( address, spendable) in create_spendables. iter ( ) {
251
+ insert_spendable ( database. pool . clone ( ) , spendable)
300
252
. await
301
- . expect ( "should get result" ) ;
302
- let expected_spendables = vec ! [ spendable_tester] ;
303
- let expected_pagination = Pagination {
304
- page : 2 ,
305
- total_pages : 3 ,
306
- } ;
307
- assert_eq ! ( spendables, expected_spendables) ;
308
- assert_eq ! ( pagination, expected_pagination) ;
253
+ . expect ( & format ! (
254
+ "Failed to insert spendable for {:?} with Spendable: {:?}" ,
255
+ address, spendable
256
+ ) ) ;
257
+ // use sleep to make all spendables with different time
258
+ // they will follow the order in which they were defined in the variable
259
+ sleep ( Duration :: from_millis ( 100 ) ) . await ;
260
+ }
261
+
262
+ let spendables = create_spendables. into_iter ( ) . collect :: < HashMap < _ , _ > > ( ) ;
263
+
264
+ let expected_pages = vec ! [
265
+ (
266
+ vec![ & spendables[ & PUBLISHER ] , & spendables[ & ADVERTISER ] ] ,
267
+ Pagination {
268
+ page: 0 ,
269
+ total_pages: 3 ,
270
+ } ,
271
+ ) ,
272
+ (
273
+ vec![ & spendables[ & CREATOR ] , & spendables[ & GUARDIAN ] ] ,
274
+ Pagination {
275
+ page: 1 ,
276
+ total_pages: 3 ,
277
+ } ,
278
+ ) ,
279
+ (
280
+ vec![ & spendables[ & GUARDIAN_2 ] ] ,
281
+ Pagination {
282
+ page: 2 ,
283
+ total_pages: 3 ,
284
+ } ,
285
+ ) ,
286
+ ] ;
287
+
288
+ for ( expected_spendables, expected_pagination) in expected_pages. iter ( ) {
289
+ // for page = 0; skip = 0
290
+ // for page = 1; skip = 2
291
+ // etc.
292
+ let limit = 2 ;
293
+ let skip = expected_pagination. page * limit;
294
+
295
+ let debug_msg = format ! (
296
+ "{:?} page = {} with skip = {} & limit = {}" ,
297
+ channel. id( ) ,
298
+ expected_pagination. page,
299
+ skip,
300
+ limit
301
+ ) ;
302
+
303
+ let ( spendables, pagination) =
304
+ get_all_spendables_for_channel ( database. clone ( ) , & channel. id ( ) , skip, limit)
305
+ . await
306
+ . expect ( & format ! ( "could not fetch spendables {}" , debug_msg) ) ;
307
+
308
+ pretty_assertions:: assert_eq!(
309
+ & pagination,
310
+ expected_pagination,
311
+ "Unexpected pagination for {}" ,
312
+ debug_msg
313
+ ) ;
314
+ pretty_assertions:: assert_eq!( & spendables, expected_spendables) ;
315
+ }
309
316
}
310
317
}
0 commit comments