diff options
| author | Joe Carstairs <me@joeac.net> | 2024-11-21 21:40:31 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2024-11-21 21:40:31 +0000 |
| commit | 9274d625c5e04e7fdca0d0449c44608a4617050c (patch) | |
| tree | deddfa82cec85ed7c26e77b668d3e581db5156b8 /rust/actualbudget_queries | |
| parent | 6174d31572fb4d6582029a6ec2ea4391d704a7cb (diff) | |
Super cool zero budget integration testing
Diffstat (limited to 'rust/actualbudget_queries')
3 files changed, 293 insertions, 2 deletions
diff --git a/rust/actualbudget_queries/tests/actualbudget_zero_budgets.rs b/rust/actualbudget_queries/tests/actualbudget_zero_budgets.rs index 4e9ea19..a10af31 100644 --- a/rust/actualbudget_queries/tests/actualbudget_zero_budgets.rs +++ b/rust/actualbudget_queries/tests/actualbudget_zero_budgets.rs @@ -1,16 +1,21 @@ mod common; use actualbudget_queries::actualbudget_zero_budgets::get_all_actualbudget_zero_budgets; -use common::db_url::DB_URL; +use common::{db_url::DB_URL, get_reference_data::get_categories, zero_budget_matchers::get_zero_budget_matchers}; use diesel::{Connection, SqliteConnection}; #[test] fn given_test_database_when_get_all_zero_budgets_then_return_zero_budgets() { let connection = &mut SqliteConnection::establish(DB_URL).unwrap(); + let categories = get_categories(connection); let zero_budgets = get_all_actualbudget_zero_budgets(connection); assert!(zero_budgets.is_ok()); let zero_budgets = zero_budgets.unwrap(); - assert_eq!(25, zero_budgets.len()); + let zero_budget_matchers = get_zero_budget_matchers(); + assert_eq!(zero_budget_matchers.len(), zero_budgets.len()); + for matcher in zero_budget_matchers { + assert!(zero_budgets.iter().any(|zb| matcher(zb, &categories))) + } } diff --git a/rust/actualbudget_queries/tests/common/mod.rs b/rust/actualbudget_queries/tests/common/mod.rs index 01112b3..9c24578 100644 --- a/rust/actualbudget_queries/tests/common/mod.rs +++ b/rust/actualbudget_queries/tests/common/mod.rs @@ -3,3 +3,4 @@ pub mod category_names; pub mod db_url; pub mod get_reference_data; pub mod transaction_matchers; +pub mod zero_budget_matchers; diff --git a/rust/actualbudget_queries/tests/common/zero_budget_matchers.rs b/rust/actualbudget_queries/tests/common/zero_budget_matchers.rs new file mode 100644 index 0000000..46b893d --- /dev/null +++ b/rust/actualbudget_queries/tests/common/zero_budget_matchers.rs @@ -0,0 +1,285 @@ +use std::collections::HashMap; + +use actualbudget_models::{actualbudget_category::ActualbudgetCategory, actualbudget_zero_budget::ActualbudgetZeroBudget}; + +use super::category_names::CATEGORY_NAMES_DICT; + +pub fn get_zero_budget_matchers() -> Vec<Box<dyn Fn(&ActualbudgetZeroBudget, &HashMap<String, ActualbudgetCategory>) -> bool>> { + vec![ + Box::new(|t, c| matches_food_budget(t, c, "08")), + Box::new(|t, c| matches_food_budget(t, c, "09")), + Box::new(|t, c| matches_food_budget(t, c, "10")), + Box::new(|t, c| matches_food_budget(t, c, "11")), + Box::new(|t, c| matches_zero_food_budget(t, c, "12")), + Box::new(|t, c| matches_general_budget(t, c, "08")), + Box::new(|t, c| matches_general_budget(t, c, "09")), + Box::new(|t, c| matches_general_budget(t, c, "10")), + Box::new(|t, c| matches_general_budget(t, c, "11")), + Box::new(|t, c| matches_zero_general_budget(t, c, "12")), + Box::new(|t, c| matches_bills_budget(t, c, "08")), + Box::new(|t, c| matches_bills_budget(t, c, "09")), + Box::new(|t, c| matches_bills_budget(t, c, "10")), + Box::new(|t, c| matches_bills_budget(t, c, "11")), + Box::new(|t, c| matches_zero_bills_budget(t, c, "12")), + Box::new(|t, c| matches_bills_flexible_budget(t, c, "08")), + Box::new(|t, c| matches_bills_flexible_budget(t, c, "09")), + Box::new(|t, c| matches_bills_flexible_budget(t, c, "10")), + Box::new(|t, c| matches_bills_flexible_budget(t, c, "11")), + Box::new(|t, c| matches_zero_bills_flexible_budget(t, c, "12")), + Box::new(|t, c| matches_savings_budget(t, c, "08")), + Box::new(|t, c| matches_savings_budget(t, c, "09")), + Box::new(|t, c| matches_savings_budget(t, c, "10")), + Box::new(|t, c| matches_savings_budget(t, c, "11")), + Box::new(|t, c| matches_zero_savings_budget(t, c, "12")), + ] +} + +fn matches_food_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let food_category_id = categories + .get(CATEGORY_NAMES_DICT.food) + .expect("failed to find food category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 200_00, + do_carry_over: false, + .. + } => + *category_id == food_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_zero_food_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let food_category_id = categories + .get(CATEGORY_NAMES_DICT.food) + .expect("failed to find food category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 0, + do_carry_over: false, + .. + } => + *category_id == food_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_general_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let general_category_id = categories + .get(CATEGORY_NAMES_DICT.general) + .expect("failed to find general category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 100_00, + do_carry_over: false, + .. + } => + *category_id == general_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_zero_general_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let general_category_id = categories + .get(CATEGORY_NAMES_DICT.general) + .expect("failed to find general category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 0, + do_carry_over: false, + .. + } => + *category_id == general_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_bills_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let bills_category_id = categories + .get(CATEGORY_NAMES_DICT.bills) + .expect("failed to find bills category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 125_00, + do_carry_over: false, + .. + } => + *category_id == bills_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_zero_bills_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let bills_category_id = categories + .get(CATEGORY_NAMES_DICT.bills) + .expect("failed to find bills category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 0, + do_carry_over: false, + .. + } => + *category_id == bills_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_bills_flexible_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let bills_flexible_category_id = categories + .get(CATEGORY_NAMES_DICT.bills_flexible) + .expect("failed to find bills (flexible) category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 25_00, + do_carry_over: false, + .. + } => + *category_id == bills_flexible_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_zero_bills_flexible_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let bills_flexible_category_id = categories + .get(CATEGORY_NAMES_DICT.bills_flexible) + .expect("failed to find bills (flexible) category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 0, + do_carry_over: false, + .. + } => + *category_id == bills_flexible_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_savings_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let savings_category_id = categories + .get(CATEGORY_NAMES_DICT.savings) + .expect("failed to find savings category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 400_00, + do_carry_over: false, + .. + } => + *category_id == savings_category_id + && date.month() == month, + _ => false, + } +} + +fn matches_zero_savings_budget( + t: &ActualbudgetZeroBudget, + categories: &HashMap<String, ActualbudgetCategory>, + month: &str, +) -> bool { + let savings_category_id = categories + .get(CATEGORY_NAMES_DICT.savings) + .expect("failed to find savings category") + .id + .clone(); + + match t { + ActualbudgetZeroBudget { + month: date, + category_id, + amount: 0, + do_carry_over: false, + .. + } => + *category_id == savings_category_id + && date.month() == month, + _ => false, + } +} |
