summaryrefslogtreecommitdiff
path: root/actualbudget_to_schist_transformer/tests/common/transaction_matchers.rs
blob: c3763c169f3b6f044b097a68914b0124664aee9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use schist_models::{account::Account, transaction::Transaction};

use super::ACCOUNT_NAMES_DICT;

pub fn get_transaction_matchers(
) -> Vec<Box<dyn Fn(&Transaction, &[Account]) -> bool>> {
    vec![
        Box::new(|t, a| matches_bank_account_initial_transaction(t, a)),
        Box::new(|t, a| matches_cash_account_initial_transaction(t, a)),
        Box::new(|t, a| matches_income_transaction(t, a, "08")),
        Box::new(|t, a| matches_income_transaction(t, a, "09")),
        Box::new(|t, a| matches_income_transaction(t, a, "10")),
        Box::new(|t, a| matches_income_transaction(t, a, "11")),
        Box::new(|t, a| matches_food_transaction(t, a, "08")),
        Box::new(|t, a| matches_food_transaction(t, a, "09")),
        Box::new(|t, a| matches_food_transaction(t, a, "10")),
        Box::new(|t, a| matches_food_transaction(t, a, "11")),
    ]
}

pub fn matches_bank_account_initial_transaction(
    t: &Transaction,
    accounts: &[Account],
) -> bool {
    let bank_account_id = accounts
        .iter()
        .find(|a| a.name == ACCOUNT_NAMES_DICT.bank_account)
        .expect("failed to find bank account")
        .id
        .clone();

    match t {
        Transaction {
            account_id,
            date,
            id: _id,
            description,
            payee,
            quantity: 1200_00,
        } => *account_id == bank_account_id
            && *date == "2024-08-01".parse().unwrap()
            && description == ""
            && payee == "Starting Balance",
        _ => false,
    }
}

pub fn matches_cash_account_initial_transaction(
    t: &Transaction,
    accounts: &[Account],
) -> bool {
    let bank_account_id = accounts
        .iter()
        .find(|a| a.name == ACCOUNT_NAMES_DICT.cash_account)
        .expect("failed to find cash account")
        .id
        .clone();

    match t {
        Transaction {
            account_id,
            date,
            id: _id,
            description,
            payee,
            quantity: 45_10,
        } => *account_id == bank_account_id
            && *date == "2024-08-01".parse().unwrap()
            && description == ""
            && payee == "Starting Balance",
        _ => false,
    }
}

pub fn matches_income_transaction(
    t: &Transaction,
    accounts: &[Account],
    month: &str,
) -> bool {
    let bank_account_id = accounts
        .iter()
        .find(|a| a.name == ACCOUNT_NAMES_DICT.bank_account)
        .expect("failed to find bank account")
        .id
        .clone();

    match t {
        Transaction {
            account_id,
            date,
            description,
            id: _id,
            payee,
            quantity: 846_46,
        } =>
            *account_id == bank_account_id
                && *date == format!("2024-{}-21", month).parse().unwrap()
                && description == "Salary"
                && payee == "Employer Ltd",
        _ => false,
    }
}

pub fn matches_food_transaction(
    t: &Transaction,
    accounts: &[Account],
    month: &str,
) -> bool {
    let bank_account_id = accounts
        .iter()
        .find(|a| a.name == ACCOUNT_NAMES_DICT.bank_account)
        .expect("failed to find bank account")
        .id
        .clone();

    match t {
        Transaction {
            account_id,
            date,
            description,
            id: _id,
            payee,
            quantity: -200_00,
        } => *account_id == bank_account_id
            && *date == format!("2024-{}-08", month).parse().unwrap()
            && description == ""
            && payee == "Tesco",
        _ => false,
    }
}