Lines
100 %
Functions
Branches
use super::{AmountView, BreakdownPeriodView, BreakdownRowView, breakdown_csv, csv_field, decimal};
use num_rational::Rational64;
fn row(
tag_value: &str,
is_uncategorized: bool,
amounts: &[(&str, Rational64)],
) -> BreakdownRowView {
BreakdownRowView {
tag_value: tag_value.to_string(),
is_uncategorized,
amounts: amounts
.iter()
.map(|(symbol, amount)| AmountView {
commodity_symbol: (*symbol).to_string(),
amount: *amount,
})
.collect(),
}
fn period(label: &str, rows: Vec<BreakdownRowView>) -> BreakdownPeriodView {
BreakdownPeriodView {
label: label.to_string(),
rows,
#[test]
fn decimal_rounds_half_away_from_zero_to_two_places() {
let cases = [
(Rational64::new(300, 1), "300"),
(Rational64::new(25, 2), "12.5"),
(Rational64::new(1, 3), "0.33"),
(Rational64::new(2, 3), "0.67"),
(Rational64::new(1, 200), "0.01"),
(Rational64::new(-1, 200), "-0.01"),
(Rational64::new(-1, 1000), "0"),
(Rational64::new(-123_456_789, 100), "-1234567.89"),
(Rational64::new(i64::MIN, 1), "-9223372036854775808"),
];
for (value, expected) in cases {
assert_eq!(decimal(value), expected, "{value}");
fn csv_field_quotes_only_what_rfc4180_requires() {
assert_eq!(csv_field("Food"), "Food");
assert_eq!(csv_field("-12.5"), "-12.5");
assert_eq!(csv_field("a,b"), "\"a,b\"");
assert_eq!(csv_field("say \"hi\""), "\"say \"\"hi\"\"\"");
assert_eq!(csv_field("two\nlines"), "\"two\nlines\"");
assert_eq!(csv_field("cr\r"), "\"cr\r\"");
fn an_ungrouped_report_has_no_period_column() {
let csv = breakdown_csv(
"category",
&[period(
"",
vec![
row(
"Еда",
false,
&[
("USD", Rational64::new(25, 2)),
("EUR", Rational64::new(3, 1)),
],
),
row("", true, &[("USD", Rational64::new(-1, 3))]),
)],
);
assert_eq!(
csv,
"\u{feff}category,Commodity,Amount\r\n\
Еда,USD,12.5\r\nЕда,EUR,3\r\n(uncategorized),USD,-0.33\r\n"
fn a_grouped_report_leads_with_the_period() {
"kind, main",
period(
"2026-01",
vec![row("Rent", false, &[("USD", Rational64::new(300, 1))])],
period("2026-02", vec![]),
"\u{feff}Period,\"kind, main\",Commodity,Amount\r\n2026-01,Rent,USD,300\r\n"