1
use super::*;
2

            
3
80
async fn create_test_transaction(
4
80
    user: &User,
5
80
    account1_id: Uuid,
6
80
    account2_id: Uuid,
7
80
    commodity_id: Uuid,
8
80
    post_date: DateTime<Utc>,
9
80
    amount: i64,
10
80
) -> anyhow::Result<Uuid> {
11
80
    let tx_id = Uuid::new_v4();
12
80
    let split1 = Split {
13
80
        id: Uuid::new_v4(),
14
80
        tx_id,
15
80
        account_id: account1_id,
16
80
        commodity_id,
17
80
        value_num: -amount,
18
80
        value_denom: 1,
19
80
        reconcile_state: None,
20
80
        reconcile_date: None,
21
80
        lot_id: None,
22
80
    };
23
80
    let split2 = Split {
24
80
        id: Uuid::new_v4(),
25
80
        tx_id,
26
80
        account_id: account2_id,
27
80
        commodity_id,
28
80
        value_num: amount,
29
80
        value_denom: 1,
30
80
        reconcile_state: None,
31
80
        reconcile_date: None,
32
80
        lot_id: None,
33
80
    };
34
80
    let splits = vec![FinanceEntity::Split(split1), FinanceEntity::Split(split2)];
35

            
36
80
    CreateTransaction::new()
37
80
        .user_id(user.id)
38
80
        .splits(splits)
39
80
        .id(tx_id)
40
80
        .post_date(post_date)
41
80
        .enter_date(Utc::now())
42
80
        .run()
43
80
        .await?;
44

            
45
80
    Ok(tx_id)
46
80
}
47

            
48
#[local_db_sqlx_test]
49
async fn test_pagination_limit(pool: PgPool) -> anyhow::Result<()> {
50
    let user = USER.get().unwrap();
51
    user.commit()
52
        .await
53
        .expect("Failed to commit user to database");
54

            
55
    let commodity_result = CreateCommodity::new()
56
        .symbol("TST".to_string())
57
        .name("Test Commodity".to_string())
58
        .user_id(user.id)
59
        .run()
60
        .await?;
61

            
62
    let commodity_id = if let Some(CmdResult::String(id)) = commodity_result {
63
        uuid::Uuid::parse_str(&id)?
64
    } else {
65
        panic!("Expected commodity ID string result");
66
    };
67

            
68
    let account1 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
69
        CreateAccount::new()
70
            .name("Account 1".to_string())
71
            .user_id(user.id)
72
            .run()
73
            .await?
74
    {
75
        account
76
    } else {
77
        panic!("Expected account entity result");
78
    };
79

            
80
    let account2 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
81
        CreateAccount::new()
82
            .name("Account 2".to_string())
83
            .user_id(user.id)
84
            .run()
85
            .await?
86
    {
87
        account
88
    } else {
89
        panic!("Expected account entity result");
90
    };
91

            
92
    let base_time = Utc::now();
93
    for i in 0..25 {
94
        let post_date = base_time - Duration::days(i);
95
        create_test_transaction(
96
            user,
97
            account1.id,
98
            account2.id,
99
            commodity_id,
100
            post_date,
101
            100 + i,
102
        )
103
        .await?;
104
    }
105

            
106
    // Test limit=10 returns exactly 10 transactions
107
    if let Some(CmdResult::TaggedTransactions {
108
        entities,
109
        pagination: Some(pagination),
110
    }) = ListTransactions::new()
111
        .user_id(user.id)
112
        .limit(10)
113
        .run()
114
        .await?
115
    {
116
        assert_eq!(entities.len(), 10, "Expected exactly 10 transactions");
117
        assert_eq!(pagination.total_count, 25);
118
        assert_eq!(pagination.limit, 10);
119
        assert_eq!(pagination.offset, 0);
120
        assert!(pagination.has_more);
121
    } else {
122
        panic!("Expected TaggedTransactions result with pagination");
123
    }
124

            
125
    // Test limit=5 returns exactly 5 transactions
126
    if let Some(CmdResult::TaggedTransactions {
127
        entities,
128
        pagination: Some(pagination),
129
    }) = ListTransactions::new()
130
        .user_id(user.id)
131
        .limit(5)
132
        .run()
133
        .await?
134
    {
135
        assert_eq!(entities.len(), 5, "Expected exactly 5 transactions");
136
        assert_eq!(pagination.total_count, 25);
137
        assert!(pagination.has_more);
138
    } else {
139
        panic!("Expected TaggedTransactions result with pagination");
140
    }
141

            
142
    // Test limit=100 returns all 25 transactions (limit > total)
143
    if let Some(CmdResult::TaggedTransactions {
144
        entities,
145
        pagination: Some(pagination),
146
    }) = ListTransactions::new()
147
        .user_id(user.id)
148
        .limit(100)
149
        .run()
150
        .await?
151
    {
152
        assert_eq!(entities.len(), 25, "Expected all 25 transactions");
153
        assert_eq!(pagination.total_count, 25);
154
        assert!(!pagination.has_more);
155
    } else {
156
        panic!("Expected TaggedTransactions result with pagination");
157
    }
158
}
159

            
160
#[local_db_sqlx_test]
161
async fn test_pagination_offset(pool: PgPool) -> anyhow::Result<()> {
162
    let user = USER.get().unwrap();
163
    user.commit()
164
        .await
165
        .expect("Failed to commit user to database");
166

            
167
    let commodity_result = CreateCommodity::new()
168
        .symbol("TST".to_string())
169
        .name("Test Commodity".to_string())
170
        .user_id(user.id)
171
        .run()
172
        .await?;
173

            
174
    let commodity_id = if let Some(CmdResult::String(id)) = commodity_result {
175
        uuid::Uuid::parse_str(&id)?
176
    } else {
177
        panic!("Expected commodity ID string result");
178
    };
179

            
180
    let account1 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
181
        CreateAccount::new()
182
            .name("Account 1".to_string())
183
            .user_id(user.id)
184
            .run()
185
            .await?
186
    {
187
        account
188
    } else {
189
        panic!("Expected account entity result");
190
    };
191

            
192
    let account2 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
193
        CreateAccount::new()
194
            .name("Account 2".to_string())
195
            .user_id(user.id)
196
            .run()
197
            .await?
198
    {
199
        account
200
    } else {
201
        panic!("Expected account entity result");
202
    };
203

            
204
    let base_time = Utc::now();
205
    for i in 0..25 {
206
        let post_date = base_time - Duration::days(i);
207
        create_test_transaction(
208
            user,
209
            account1.id,
210
            account2.id,
211
            commodity_id,
212
            post_date,
213
            100 + i,
214
        )
215
        .await?;
216
    }
217

            
218
    // Test offset=10, limit=10 returns second page
219
    if let Some(CmdResult::TaggedTransactions {
220
        entities,
221
        pagination: Some(pagination),
222
    }) = ListTransactions::new()
223
        .user_id(user.id)
224
        .limit(10)
225
        .offset(10)
226
        .run()
227
        .await?
228
    {
229
        assert_eq!(
230
            entities.len(),
231
            10,
232
            "Expected 10 transactions on second page"
233
        );
234
        assert_eq!(pagination.total_count, 25);
235
        assert_eq!(pagination.offset, 10);
236
        assert!(pagination.has_more);
237
    } else {
238
        panic!("Expected TaggedTransactions result with pagination");
239
    }
240

            
241
    // Test offset=20, limit=10 returns last page (only 5 remaining)
242
    if let Some(CmdResult::TaggedTransactions {
243
        entities,
244
        pagination: Some(pagination),
245
    }) = ListTransactions::new()
246
        .user_id(user.id)
247
        .limit(10)
248
        .offset(20)
249
        .run()
250
        .await?
251
    {
252
        assert_eq!(entities.len(), 5, "Expected 5 transactions on last page");
253
        assert_eq!(pagination.total_count, 25);
254
        assert!(!pagination.has_more);
255
    } else {
256
        panic!("Expected TaggedTransactions result with pagination");
257
    }
258

            
259
    // Test offset beyond total returns empty
260
    if let Some(CmdResult::TaggedTransactions {
261
        entities,
262
        pagination: Some(pagination),
263
    }) = ListTransactions::new()
264
        .user_id(user.id)
265
        .limit(10)
266
        .offset(100)
267
        .run()
268
        .await?
269
    {
270
        assert!(entities.is_empty(), "Expected no transactions beyond total");
271
        assert_eq!(pagination.total_count, 25);
272
        assert!(!pagination.has_more);
273
    } else {
274
        panic!("Expected TaggedTransactions result with pagination");
275
    }
276
}
277

            
278
#[local_db_sqlx_test]
279
async fn test_pagination_date_filter(pool: PgPool) -> anyhow::Result<()> {
280
    let user = USER.get().unwrap();
281
    user.commit()
282
        .await
283
        .expect("Failed to commit user to database");
284

            
285
    let commodity_result = CreateCommodity::new()
286
        .symbol("TST".to_string())
287
        .name("Test Commodity".to_string())
288
        .user_id(user.id)
289
        .run()
290
        .await?;
291

            
292
    let commodity_id = if let Some(CmdResult::String(id)) = commodity_result {
293
        uuid::Uuid::parse_str(&id)?
294
    } else {
295
        panic!("Expected commodity ID string result");
296
    };
297

            
298
    let account1 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
299
        CreateAccount::new()
300
            .name("Account 1".to_string())
301
            .user_id(user.id)
302
            .run()
303
            .await?
304
    {
305
        account
306
    } else {
307
        panic!("Expected account entity result");
308
    };
309

            
310
    let account2 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
311
        CreateAccount::new()
312
            .name("Account 2".to_string())
313
            .user_id(user.id)
314
            .run()
315
            .await?
316
    {
317
        account
318
    } else {
319
        panic!("Expected account entity result");
320
    };
321

            
322
    let base_time = Utc::now();
323
    for i in 0..30 {
324
        let post_date = base_time - Duration::days(i);
325
        create_test_transaction(
326
            user,
327
            account1.id,
328
            account2.id,
329
            commodity_id,
330
            post_date,
331
            100 + i,
332
        )
333
        .await?;
334
    }
335

            
336
    // Test date_from filter (last 10 days)
337
    let date_from = base_time - Duration::days(9);
338
    if let Some(CmdResult::TaggedTransactions {
339
        entities,
340
        pagination: Some(pagination),
341
    }) = ListTransactions::new()
342
        .user_id(user.id)
343
        .date_from(date_from)
344
        .run()
345
        .await?
346
    {
347
        assert_eq!(
348
            entities.len(),
349
            10,
350
            "Expected 10 transactions from last 10 days"
351
        );
352
        assert_eq!(pagination.total_count, 10);
353
    } else {
354
        panic!("Expected TaggedTransactions result with pagination");
355
    }
356

            
357
    // Test date_to filter (older than 20 days)
358
    let date_to = base_time - Duration::days(20);
359
    if let Some(CmdResult::TaggedTransactions {
360
        entities,
361
        pagination: Some(pagination),
362
    }) = ListTransactions::new()
363
        .user_id(user.id)
364
        .date_to(date_to)
365
        .run()
366
        .await?
367
    {
368
        assert_eq!(
369
            entities.len(),
370
            10,
371
            "Expected 10 transactions older than 20 days"
372
        );
373
        assert_eq!(pagination.total_count, 10);
374
    } else {
375
        panic!("Expected TaggedTransactions result with pagination");
376
    }
377

            
378
    // Test date range filter (days 10-19)
379
    let date_from = base_time - Duration::days(19);
380
    let date_to = base_time - Duration::days(10);
381
    if let Some(CmdResult::TaggedTransactions {
382
        entities,
383
        pagination: Some(pagination),
384
    }) = ListTransactions::new()
385
        .user_id(user.id)
386
        .date_from(date_from)
387
        .date_to(date_to)
388
        .run()
389
        .await?
390
    {
391
        assert_eq!(entities.len(), 10, "Expected 10 transactions in date range");
392
        assert_eq!(pagination.total_count, 10);
393
    } else {
394
        panic!("Expected TaggedTransactions result with pagination");
395
    }
396

            
397
    // Test date range with pagination
398
    let date_from = base_time - Duration::days(29);
399
    if let Some(CmdResult::TaggedTransactions {
400
        entities,
401
        pagination: Some(pagination),
402
    }) = ListTransactions::new()
403
        .user_id(user.id)
404
        .date_from(date_from)
405
        .limit(5)
406
        .run()
407
        .await?
408
    {
409
        assert_eq!(entities.len(), 5, "Expected 5 transactions with limit");
410
        assert_eq!(pagination.total_count, 30);
411
        assert!(pagination.has_more);
412
    } else {
413
        panic!("Expected TaggedTransactions result with pagination");
414
    }
415
}