1
use super::*;
2
use num_rational::Rational64;
3
use server::command::report::view::BreakdownRowView;
4

            
5
10
fn row(name: &str, amounts: &[(&str, i64)]) -> BreakdownRowView {
6
    BreakdownRowView {
7
10
        tag_value: name.to_string(),
8
10
        is_uncategorized: false,
9
10
        amounts: amounts
10
10
            .iter()
11
            .map(|(s, v)| AmountView {
12
10
                commodity_symbol: (*s).to_string(),
13
10
                amount: Rational64::from_integer(*v),
14
10
            })
15
            .collect(),
16
    }
17
10
}
18

            
19
6
fn labels(cards: &[SummaryCard]) -> Vec<&str> {
20
6
    cards
21
6
        .iter()
22
16
        .filter(|c| !c.highlight)
23
10
        .map(|c| c.label.as_str())
24
        .collect()
25
6
}
26

            
27
#[test]
28
2
fn top_cards_follow_the_report_row_order() {
29
2
    let periods = vec![BreakdownPeriodView {
30
2
        label: String::new(),
31
2
        rows: vec![row("small", &[("USD", 1)]), row("big", &[("USD", 100)])],
32
2
    }];
33
2
    assert_eq!(labels(&breakdown_summary(&periods, 2)), ["small", "big"]);
34
2
    assert_eq!(labels(&breakdown_summary(&periods, 1)), ["small"]);
35
2
}
36

            
37
#[test]
38
2
fn cards_sum_a_category_across_periods_in_first_seen_order() {
39
    let period = |rows| BreakdownPeriodView {
40
4
        label: String::new(),
41
4
        rows,
42
4
    };
43
2
    let periods = vec![
44
2
        period(vec![row("a", &[("USD", 60)]), row("b", &[("USD", -50)])]),
45
2
        period(vec![row("b", &[("USD", -40)])]),
46
    ];
47
2
    let cards = breakdown_summary(&periods, 2);
48
2
    assert_eq!(labels(&cards), ["a", "b"]);
49
2
    assert_eq!(cards[2].amounts[0].amount, Rational64::from_integer(-90));
50
2
}