From 51711fb42a44b452cdf1b4fd6a716d0251f23dda Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Fri, 6 Dec 2024 15:16:52 +0000 Subject: Tests transaction categorisations in AB transformer --- .../common/transaction_categorisation_matchers.rs | 141 ++++++++++++++++++++- .../tests/run.rs | 9 +- 2 files changed, 145 insertions(+), 5 deletions(-) (limited to 'rust') diff --git a/rust/actualbudget_to_schist_transformer/tests/common/transaction_categorisation_matchers.rs b/rust/actualbudget_to_schist_transformer/tests/common/transaction_categorisation_matchers.rs index 20846e2..04ce6a7 100644 --- a/rust/actualbudget_to_schist_transformer/tests/common/transaction_categorisation_matchers.rs +++ b/rust/actualbudget_to_schist_transformer/tests/common/transaction_categorisation_matchers.rs @@ -1,6 +1,141 @@ -use schist_models::transaction_categorisation::TransactionCategorisation; +use schist_models::{account::Account, category::Category, transaction::Transaction, transaction_categorisation::TransactionCategorisation}; + +use super::{category_names::{CATEGORY_NAME_FOOD, CATEGORY_NAME_INCOME, CATEGORY_NAME_STARTING_BALANCES}, matches_bank_account_initial_transaction, matches_cash_account_initial_transaction, matches_food_transaction, matches_income_transaction}; pub fn get_transaction_categorisation_matchers( -) -> Vec bool>> { - todo!() +) -> Vec bool>> { + vec![ + Box::new(|tc, t, c, a| matches_bank_account_initial_transaction_categorisation(tc, t, c, a)), + Box::new(|tc, t, c, a| matches_cash_account_initial_transaction_categorisation(tc, t, c, a)), + Box::new(|tc, t, c, a| matches_income_transaction_categorisation(tc, t, c, a, "08")), + Box::new(|tc, t, c, a| matches_income_transaction_categorisation(tc, t, c, a, "09")), + Box::new(|tc, t, c, a| matches_income_transaction_categorisation(tc, t, c, a, "10")), + Box::new(|tc, t, c, a| matches_income_transaction_categorisation(tc, t, c, a, "11")), + Box::new(|tc, t, c, a| matches_food_transaction_categorisation(tc, t, c, a, "08")), + Box::new(|tc, t, c, a| matches_food_transaction_categorisation(tc, t, c, a, "09")), + Box::new(|tc, t, c, a| matches_food_transaction_categorisation(tc, t, c, a, "10")), + Box::new(|tc, t, c, a| matches_food_transaction_categorisation(tc, t, c, a, "11")), + ] +} + +fn matches_bank_account_initial_transaction_categorisation( + transaction_categorisation: &TransactionCategorisation, + transactions: &[Transaction], + categories: &[Category], + accounts: &[Account], +) -> bool { + let bank_account_initial_transaction = transactions + .iter() + .find(|t| matches_bank_account_initial_transaction(t, accounts)) + .expect("failed to find bank account initial transaction"); + + let starting_balance_category = categories + .iter() + .find(|c| c.name == CATEGORY_NAME_STARTING_BALANCES) + .expect("failed to find starting balance category"); + + match transaction_categorisation { + TransactionCategorisation { + id: _id, + description, + quantity: 1200_00, + transaction_id, + category_id, + } => description == "" + && *transaction_id == bank_account_initial_transaction.id + && *category_id == starting_balance_category.id, + _ => false, + } +} + +fn matches_cash_account_initial_transaction_categorisation( + transaction_categorisation: &TransactionCategorisation, + transactions: &[Transaction], + categories: &[Category], + accounts: &[Account], +) -> bool { + let cash_account_initial_transaction = transactions + .iter() + .find(|t| matches_cash_account_initial_transaction(t, accounts)) + .expect("failed to find cash account initial transaction"); + + let starting_balance_category = categories + .iter() + .find(|c| c.name == CATEGORY_NAME_STARTING_BALANCES) + .expect("failed to find starting balance category"); + + match transaction_categorisation { + TransactionCategorisation { + id: _id, + description, + quantity: 45_10, + transaction_id, + category_id, + } => description == "" + && *transaction_id == cash_account_initial_transaction.id + && *category_id == starting_balance_category.id, + _ => false, + } +} + +fn matches_income_transaction_categorisation( + transaction_categorisation: &TransactionCategorisation, + transactions: &[Transaction], + categories: &[Category], + accounts: &[Account], + month: &str, +) -> bool { + let income_transaction = transactions + .iter() + .find(|t| matches_income_transaction(t, accounts, month)) + .expect(format!("failed to find income transaction for month {}", month).as_str()); + + let income_category = categories + .iter() + .find(|c| c.name == CATEGORY_NAME_INCOME) + .expect("failed to find income category"); + + match transaction_categorisation { + TransactionCategorisation { + id: _id, + description, + quantity: 846_46, + transaction_id, + category_id, + } => description == "" + && *transaction_id == income_transaction.id + && *category_id == income_category.id, + _ => false, + } +} + +fn matches_food_transaction_categorisation( + transaction_categorisation: &TransactionCategorisation, + transactions: &[Transaction], + categories: &[Category], + accounts: &[Account], + month: &str, +) -> bool { + let food_transaction = transactions + .iter() + .find(|t| matches_food_transaction(t, accounts, month)) + .expect(format!("failed to find food transaction for month {}", month).as_str()); + + let food_category = categories + .iter() + .find(|c| c.name == CATEGORY_NAME_FOOD) + .expect("failed to find food category"); + + match transaction_categorisation { + TransactionCategorisation { + id: _id, + description, + quantity: -200_00, + transaction_id, + category_id, + } => description == "" + && *transaction_id == food_transaction.id + && *category_id == food_category.id, + _ => false, + } } diff --git a/rust/actualbudget_to_schist_transformer/tests/run.rs b/rust/actualbudget_to_schist_transformer/tests/run.rs index 7cfe150..e882dc5 100644 --- a/rust/actualbudget_to_schist_transformer/tests/run.rs +++ b/rust/actualbudget_to_schist_transformer/tests/run.rs @@ -65,7 +65,7 @@ fn given_test_database_when_run_then_exports_expected_schist_state() { let transaction_matchers = get_transaction_matchers(); assert_eq!(transaction_matchers.len(), transactions.len()); - for transaction in transactions { + for transaction in transactions.clone() { assert!( transaction_matchers.iter().any(|m| m(&transaction, &accounts)), "no match found for transaction {:?}", @@ -77,7 +77,12 @@ fn given_test_database_when_run_then_exports_expected_schist_state() { assert_eq!(transaction_categorisation_matchers.len(), transaction_categorisations.len()); for transaction_categorisation in transaction_categorisations { assert!( - transaction_categorisation_matchers.iter().any(|m| m(&transaction_categorisation)), + transaction_categorisation_matchers.iter().any(|m| m( + &transaction_categorisation, + &transactions, + &categories, + &accounts, + )), "no match found for transaction_categorisation {:?}", transaction_categorisation, ); -- cgit v1.2.3