Lines
100 %
Functions
Branches
use super::*;
async fn create_test_transaction(
user: &User,
account1_id: Uuid,
account2_id: Uuid,
commodity_id: Uuid,
post_date: DateTime<Utc>,
amount: i64,
) -> anyhow::Result<Uuid> {
let tx_id = Uuid::new_v4();
let split1 = Split {
id: Uuid::new_v4(),
tx_id,
account_id: account1_id,
commodity_id,
value_num: -amount,
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
};
let split2 = Split {
account_id: account2_id,
value_num: amount,
let splits = vec![FinanceEntity::Split(split1), FinanceEntity::Split(split2)];
CreateTransaction::new()
.user_id(user.id)
.splits(splits)
.id(tx_id)
.post_date(post_date)
.enter_date(Utc::now())
.run()
.await?;
Ok(tx_id)
}
#[local_db_sqlx_test]
async fn test_pagination_limit(pool: PgPool) -> anyhow::Result<()> {
let user = USER.get().unwrap();
user.commit()
.await
.expect("Failed to commit user to database");
let commodity_result = CreateCommodity::new()
.symbol("TST".to_string())
.name("Test Commodity".to_string())
let commodity_id = if let Some(CmdResult::String(id)) = commodity_result {
uuid::Uuid::parse_str(&id)?
} else {
panic!("Expected commodity ID string result");
let account1 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
CreateAccount::new()
.name("Account 1".to_string())
.await?
{
account
panic!("Expected account entity result");
let account2 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
.name("Account 2".to_string())
let base_time = Utc::now();
for i in 0..25 {
let post_date = base_time - Duration::days(i);
create_test_transaction(
user,
account1.id,
account2.id,
post_date,
100 + i,
)
// Test limit=10 returns exactly 10 transactions
if let Some(CmdResult::TaggedTransactions {
entities,
pagination: Some(pagination),
}) = ListTransactions::new()
.limit(10)
assert_eq!(entities.len(), 10, "Expected exactly 10 transactions");
assert_eq!(pagination.total_count, 25);
assert_eq!(pagination.limit, 10);
assert_eq!(pagination.offset, 0);
assert!(pagination.has_more);
panic!("Expected TaggedTransactions result with pagination");
// Test limit=5 returns exactly 5 transactions
.limit(5)
assert_eq!(entities.len(), 5, "Expected exactly 5 transactions");
// Test limit=100 returns all 25 transactions (limit > total)
.limit(100)
assert_eq!(entities.len(), 25, "Expected all 25 transactions");
assert!(!pagination.has_more);
async fn test_pagination_offset(pool: PgPool) -> anyhow::Result<()> {
// Test offset=10, limit=10 returns second page
.offset(10)
assert_eq!(
entities.len(),
10,
"Expected 10 transactions on second page"
);
assert_eq!(pagination.offset, 10);
// Test offset=20, limit=10 returns last page (only 5 remaining)
.offset(20)
assert_eq!(entities.len(), 5, "Expected 5 transactions on last page");
// Test offset beyond total returns empty
.offset(100)
assert!(entities.is_empty(), "Expected no transactions beyond total");
async fn test_pagination_date_filter(pool: PgPool) -> anyhow::Result<()> {
for i in 0..30 {
// Test date_from filter (last 10 days)
let date_from = base_time - Duration::days(9);
.date_from(date_from)
"Expected 10 transactions from last 10 days"
assert_eq!(pagination.total_count, 10);
// Test date_to filter (older than 20 days)
let date_to = base_time - Duration::days(20);
.date_to(date_to)
"Expected 10 transactions older than 20 days"
// Test date range filter (days 10-19)
let date_from = base_time - Duration::days(19);
let date_to = base_time - Duration::days(10);
assert_eq!(entities.len(), 10, "Expected 10 transactions in date range");
// Test date range with pagination
let date_from = base_time - Duration::days(29);
assert_eq!(entities.len(), 5, "Expected 5 transactions with limit");
assert_eq!(pagination.total_count, 30);