1
use super::*;
2
use finance::error::ReportError;
3
use num_rational::Rational64;
4

            
5
3
fn amount(symbol: &str, id: u128) -> CommodityAmount {
6
3
    CommodityAmount {
7
3
        commodity_id: Uuid::from_u128(id),
8
3
        commodity_symbol: symbol.to_owned(),
9
3
        amount: Rational64::from_integer(1),
10
3
    }
11
3
}
12

            
13
#[test]
14
1
fn amounts_sort_by_symbol_then_id() {
15
1
    let mut list = vec![amount("USD", 1), amount("EUR", 9), amount("EUR", 2)];
16
1
    sort_amounts(&mut list);
17
1
    let order: Vec<_> = list
18
1
        .iter()
19
3
        .map(|a| (a.commodity_symbol.as_str(), a.commodity_id.as_u128()))
20
        .collect();
21
1
    assert_eq!(order, [("EUR", 2), ("EUR", 9), ("USD", 1)]);
22
1
}
23

            
24
6
fn account(id: u128, parent: Option<u128>) -> AccountRow {
25
6
    AccountRow {
26
6
        account_id: Uuid::from_u128(id),
27
6
        parent_id: parent.map(Uuid::from_u128),
28
6
        account_name: format!("a{id}"),
29
6
        account_type: None,
30
6
    }
31
6
}
32

            
33
8
fn split(amounts: &mut AccountAmounts, account: u128, commodity: u128, value: i64) {
34
8
    accumulate_split(
35
8
        amounts,
36
8
        Uuid::from_u128(account),
37
8
        Uuid::from_u128(commodity),
38
8
        Rational64::from_integer(value),
39
8
        "$",
40
8
    )
41
8
    .expect("fits");
42
8
}
43

            
44
5
fn values(list: &[CommodityAmount]) -> Vec<(u128, i64)> {
45
5
    list.iter()
46
7
        .map(|a| (a.commodity_id.as_u128(), *a.amount.numer()))
47
        .collect()
48
5
}
49

            
50
#[test]
51
1
fn a_parent_keeps_its_own_amounts_apart_from_the_roll_up() {
52
1
    let mut amounts = AccountAmounts::new();
53
1
    split(&mut amounts, 1, 7, 10);
54
1
    split(&mut amounts, 2, 7, 20);
55
1
    let tree = build_tree(&[account(1, None), account(2, Some(1))], &amounts).expect("tree");
56
1
    assert_eq!(values(&tree[0].amounts), [(7, 30)]);
57
1
    assert_eq!(values(&tree[0].own_amounts), [(7, 10)]);
58
1
    assert_eq!(values(&tree[0].children[0].own_amounts), [(7, 20)]);
59
1
}
60

            
61
#[test]
62
1
fn commodities_sharing_a_symbol_stay_separate_in_own_amounts() {
63
1
    let mut amounts = AccountAmounts::new();
64
1
    split(&mut amounts, 1, 7, 10);
65
1
    split(&mut amounts, 1, 8, 5);
66
1
    split(&mut amounts, 2, 8, 2);
67
1
    let tree = build_tree(&[account(1, None), account(2, Some(1))], &amounts).expect("tree");
68
1
    assert_eq!(values(&tree[0].amounts), [(7, 10), (8, 7)]);
69
1
    assert_eq!(values(&tree[0].own_amounts), [(7, 10), (8, 5)]);
70
1
}
71

            
72
#[test]
73
1
fn a_roll_up_that_overflows_is_a_report_error() {
74
1
    let mut amounts = AccountAmounts::new();
75
1
    split(&mut amounts, 1, 7, i64::MAX);
76
1
    split(&mut amounts, 2, 7, i64::MAX);
77
1
    let err = build_tree(&[account(1, None), account(2, Some(1))], &amounts).unwrap_err();
78
1
    assert!(matches!(err, ReportError::AmountOverflow), "{err:?}");
79
1
}
80

            
81
#[test]
82
1
fn splits_that_overflow_one_account_are_a_report_error() {
83
1
    let mut amounts = AccountAmounts::new();
84
1
    split(&mut amounts, 1, 7, i64::MAX);
85
1
    let again = accumulate_split(
86
1
        &mut amounts,
87
1
        Uuid::from_u128(1),
88
1
        Uuid::from_u128(7),
89
1
        Rational64::from_integer(1),
90
1
        "$",
91
1
    );
92
1
    assert!(matches!(again, Err(ReportError::AmountOverflow)));
93
1
}
94

            
95
#[test]
96
1
fn a_stored_zero_denominator_is_a_report_error_not_a_panic() {
97
1
    for (numer, denom) in [(5, 0), (1, i64::MIN), (i64::MIN, -1)] {
98
3
        assert!(
99
3
            matches!(ratio(numer, denom), Err(ReportError::InvalidRatio { .. })),
100
            "{numer}/{denom}"
101
        );
102
    }
103
1
    assert_eq!(ratio(3, -6).expect("valid"), Rational64::new(-1, 2));
104
1
}