summaryrefslogtreecommitdiff
path: root/rust/actualbudget_to_schist_transformer/tests/common
diff options
context:
space:
mode:
Diffstat (limited to 'rust/actualbudget_to_schist_transformer/tests/common')
-rw-r--r--rust/actualbudget_to_schist_transformer/tests/common/transaction_categorisation_matchers.rs141
1 files changed, 138 insertions, 3 deletions
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<Box<dyn Fn(&TransactionCategorisation) -> bool>> {
- todo!()
+) -> Vec<Box<dyn Fn(&TransactionCategorisation, &[Transaction], &[Category], &[Account]) -> 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,
+ }
}