Lines
100 %
Functions
Branches
use super::*;
use finance::error::ReportError;
use num_rational::Rational64;
fn amount(symbol: &str, id: u128) -> CommodityAmount {
CommodityAmount {
commodity_id: Uuid::from_u128(id),
commodity_symbol: symbol.to_owned(),
amount: Rational64::from_integer(1),
}
#[test]
fn amounts_sort_by_symbol_then_id() {
let mut list = vec![amount("USD", 1), amount("EUR", 9), amount("EUR", 2)];
sort_amounts(&mut list);
let order: Vec<_> = list
.iter()
.map(|a| (a.commodity_symbol.as_str(), a.commodity_id.as_u128()))
.collect();
assert_eq!(order, [("EUR", 2), ("EUR", 9), ("USD", 1)]);
fn account(id: u128, parent: Option<u128>) -> AccountRow {
AccountRow {
account_id: Uuid::from_u128(id),
parent_id: parent.map(Uuid::from_u128),
account_name: format!("a{id}"),
account_type: None,
fn split(amounts: &mut AccountAmounts, account: u128, commodity: u128, value: i64) {
accumulate_split(
amounts,
Uuid::from_u128(account),
Uuid::from_u128(commodity),
Rational64::from_integer(value),
"$",
)
.expect("fits");
fn values(list: &[CommodityAmount]) -> Vec<(u128, i64)> {
list.iter()
.map(|a| (a.commodity_id.as_u128(), *a.amount.numer()))
.collect()
fn a_parent_keeps_its_own_amounts_apart_from_the_roll_up() {
let mut amounts = AccountAmounts::new();
split(&mut amounts, 1, 7, 10);
split(&mut amounts, 2, 7, 20);
let tree = build_tree(&[account(1, None), account(2, Some(1))], &amounts).expect("tree");
assert_eq!(values(&tree[0].amounts), [(7, 30)]);
assert_eq!(values(&tree[0].own_amounts), [(7, 10)]);
assert_eq!(values(&tree[0].children[0].own_amounts), [(7, 20)]);
fn commodities_sharing_a_symbol_stay_separate_in_own_amounts() {
split(&mut amounts, 1, 8, 5);
split(&mut amounts, 2, 8, 2);
assert_eq!(values(&tree[0].amounts), [(7, 10), (8, 7)]);
assert_eq!(values(&tree[0].own_amounts), [(7, 10), (8, 5)]);
fn a_roll_up_that_overflows_is_a_report_error() {
split(&mut amounts, 1, 7, i64::MAX);
split(&mut amounts, 2, 7, i64::MAX);
let err = build_tree(&[account(1, None), account(2, Some(1))], &amounts).unwrap_err();
assert!(matches!(err, ReportError::AmountOverflow), "{err:?}");
fn splits_that_overflow_one_account_are_a_report_error() {
let again = accumulate_split(
&mut amounts,
Uuid::from_u128(1),
Uuid::from_u128(7),
Rational64::from_integer(1),
);
assert!(matches!(again, Err(ReportError::AmountOverflow)));
fn a_stored_zero_denominator_is_a_report_error_not_a_panic() {
for (numer, denom) in [(5, 0), (1, i64::MIN), (i64::MIN, -1)] {
assert!(
matches!(ratio(numer, denom), Err(ReportError::InvalidRatio { .. })),
"{numer}/{denom}"
assert_eq!(ratio(3, -6).expect("valid"), Rational64::new(-1, 2));