1
use super::{AmountView, BreakdownPeriodView, BreakdownRowView, breakdown_csv, csv_field, decimal};
2
use num_rational::Rational64;
3

            
4
3
fn row(
5
3
    tag_value: &str,
6
3
    is_uncategorized: bool,
7
3
    amounts: &[(&str, Rational64)],
8
3
) -> BreakdownRowView {
9
    BreakdownRowView {
10
3
        tag_value: tag_value.to_string(),
11
3
        is_uncategorized,
12
3
        amounts: amounts
13
3
            .iter()
14
            .map(|(symbol, amount)| AmountView {
15
4
                commodity_symbol: (*symbol).to_string(),
16
4
                amount: *amount,
17
4
            })
18
            .collect(),
19
    }
20
3
}
21

            
22
3
fn period(label: &str, rows: Vec<BreakdownRowView>) -> BreakdownPeriodView {
23
3
    BreakdownPeriodView {
24
3
        label: label.to_string(),
25
3
        rows,
26
3
    }
27
3
}
28

            
29
#[test]
30
1
fn decimal_rounds_half_away_from_zero_to_two_places() {
31
1
    let cases = [
32
1
        (Rational64::new(300, 1), "300"),
33
1
        (Rational64::new(25, 2), "12.5"),
34
1
        (Rational64::new(1, 3), "0.33"),
35
1
        (Rational64::new(2, 3), "0.67"),
36
1
        (Rational64::new(1, 200), "0.01"),
37
1
        (Rational64::new(-1, 200), "-0.01"),
38
1
        (Rational64::new(-1, 1000), "0"),
39
1
        (Rational64::new(-123_456_789, 100), "-1234567.89"),
40
1
        (Rational64::new(i64::MIN, 1), "-9223372036854775808"),
41
1
    ];
42
1
    for (value, expected) in cases {
43
9
        assert_eq!(decimal(value), expected, "{value}");
44
    }
45
1
}
46

            
47
#[test]
48
1
fn csv_field_quotes_only_what_rfc4180_requires() {
49
1
    assert_eq!(csv_field("Food"), "Food");
50
1
    assert_eq!(csv_field("-12.5"), "-12.5");
51
1
    assert_eq!(csv_field("a,b"), "\"a,b\"");
52
1
    assert_eq!(csv_field("say \"hi\""), "\"say \"\"hi\"\"\"");
53
1
    assert_eq!(csv_field("two\nlines"), "\"two\nlines\"");
54
1
    assert_eq!(csv_field("cr\r"), "\"cr\r\"");
55
1
}
56

            
57
#[test]
58
1
fn an_ungrouped_report_has_no_period_column() {
59
1
    let csv = breakdown_csv(
60
1
        "category",
61
1
        &[period(
62
1
            "",
63
1
            vec![
64
1
                row(
65
1
                    "Еда",
66
1
                    false,
67
1
                    &[
68
1
                        ("USD", Rational64::new(25, 2)),
69
1
                        ("EUR", Rational64::new(3, 1)),
70
1
                    ],
71
1
                ),
72
1
                row("", true, &[("USD", Rational64::new(-1, 3))]),
73
1
            ],
74
1
        )],
75
1
    );
76
1
    assert_eq!(
77
1
        csv,
78
1
        "\u{feff}category,Commodity,Amount\r\n\
79
1
         Еда,USD,12.5\r\nЕда,EUR,3\r\n(uncategorized),USD,-0.33\r\n"
80
    );
81
1
}
82

            
83
#[test]
84
1
fn a_grouped_report_leads_with_the_period() {
85
1
    let csv = breakdown_csv(
86
1
        "kind, main",
87
1
        &[
88
1
            period(
89
1
                "2026-01",
90
1
                vec![row("Rent", false, &[("USD", Rational64::new(300, 1))])],
91
1
            ),
92
1
            period("2026-02", vec![]),
93
1
        ],
94
1
    );
95
1
    assert_eq!(
96
1
        csv,
97
1
        "\u{feff}Period,\"kind, main\",Commodity,Amount\r\n2026-01,Rent,USD,300\r\n"
98
    );
99
1
}