1
use num_rational::Rational64;
2
use serde_json::json;
3

            
4
use super::*;
5
use finance::error::ReportError;
6

            
7
22
fn usd(num: i64) -> CommodityAmount {
8
22
    CommodityAmount {
9
22
        commodity_id: Uuid::from_u128(1),
10
22
        commodity_symbol: "USD".to_string(),
11
22
        amount: Rational64::new(num, 1),
12
22
    }
13
22
}
14

            
15
9
fn root(name: &str, amount: i64) -> ReportNode {
16
9
    ReportNode {
17
9
        account_id: Uuid::from_u128(2),
18
9
        account_name: name.to_string(),
19
9
        account_path: name.to_string(),
20
9
        depth: 0,
21
9
        account_type: None,
22
9
        amounts: vec![usd(amount)],
23
9
        own_amounts: vec![usd(amount)],
24
9
        children: vec![],
25
9
    }
26
9
}
27

            
28
3
fn period() -> ActivityPeriod {
29
3
    let income = ActivityGroupResult::new("Income".to_string(), true, vec![root("Salary", -500)])
30
3
        .expect("income total");
31
3
    let expense = ActivityGroupResult::new("Expense".to_string(), false, vec![root("Rent", 200)])
32
3
        .expect("expense total");
33
3
    ActivityPeriod::new(None, vec![income, expense]).expect("net")
34
3
}
35

            
36
#[test]
37
1
fn the_stored_net_is_the_raw_sum_and_the_displayed_net_is_its_negation() {
38
1
    let period = period();
39

            
40
1
    assert_eq!(period.net, vec![usd(-300)]);
41
1
    assert_eq!(period.displayed_net().expect("net"), vec![usd(300)]);
42
1
}
43

            
44
#[test]
45
1
fn serialized_activity_is_uniformly_raw() {
46
1
    let period = period();
47

            
48
1
    let value = serde_json::to_value(&period).expect("serializes");
49

            
50
1
    assert_eq!(value["net"][0]["amount"], json!([-300, 1]));
51
1
    assert_eq!(value["groups"][0]["total"][0]["amount"], json!([-500, 1]));
52
1
    assert_eq!(
53
1
        value["groups"][0]["roots"][0]["amounts"][0]["amount"],
54
1
        json!([-500, 1])
55
    );
56
1
}
57

            
58
#[test]
59
1
fn a_flipped_group_shows_its_rows_and_total_negated() {
60
1
    let shown = period().groups[0].displayed().expect("displayed");
61

            
62
1
    assert_eq!(shown.total, vec![usd(500)]);
63
1
    assert_eq!(shown.roots[0].amounts, vec![usd(500)]);
64
1
}
65

            
66
#[test]
67
1
fn totals_that_overflow_are_a_report_error() {
68
1
    let roots = vec![root("A", i64::MAX), root("B", i64::MAX)];
69
1
    let err = ActivityGroupResult::new("Income".to_string(), false, roots).unwrap_err();
70
1
    assert!(matches!(err, ReportError::AmountOverflow), "{err:?}");
71
1
}
72

            
73
#[test]
74
1
fn negating_an_i64_min_numerator_is_a_report_error() {
75
1
    let flipped = ActivityGroupResult::new("Income".to_string(), true, vec![root("A", i64::MIN)])
76
1
        .expect("a single amount fits");
77
1
    assert!(matches!(
78
1
        flipped.displayed(),
79
        Err(ReportError::AmountOverflow)
80
    ));
81

            
82
1
    let period = ActivityPeriod::new(None, vec![flipped]).expect("a single total fits");
83
1
    assert!(matches!(
84
1
        period.displayed_net(),
85
        Err(ReportError::AmountOverflow)
86
    ));
87
1
}