Lines
100 %
Functions
Branches
use num_rational::Rational64;
use serde_json::json;
use super::*;
use finance::error::ReportError;
fn usd(num: i64) -> CommodityAmount {
CommodityAmount {
commodity_id: Uuid::from_u128(1),
commodity_symbol: "USD".to_string(),
amount: Rational64::new(num, 1),
}
fn root(name: &str, amount: i64) -> ReportNode {
ReportNode {
account_id: Uuid::from_u128(2),
account_name: name.to_string(),
account_path: name.to_string(),
depth: 0,
account_type: None,
amounts: vec![usd(amount)],
own_amounts: vec![usd(amount)],
children: vec![],
fn period() -> ActivityPeriod {
let income = ActivityGroupResult::new("Income".to_string(), true, vec![root("Salary", -500)])
.expect("income total");
let expense = ActivityGroupResult::new("Expense".to_string(), false, vec![root("Rent", 200)])
.expect("expense total");
ActivityPeriod::new(None, vec![income, expense]).expect("net")
#[test]
fn the_stored_net_is_the_raw_sum_and_the_displayed_net_is_its_negation() {
let period = period();
assert_eq!(period.net, vec![usd(-300)]);
assert_eq!(period.displayed_net().expect("net"), vec![usd(300)]);
fn serialized_activity_is_uniformly_raw() {
let value = serde_json::to_value(&period).expect("serializes");
assert_eq!(value["net"][0]["amount"], json!([-300, 1]));
assert_eq!(value["groups"][0]["total"][0]["amount"], json!([-500, 1]));
assert_eq!(
value["groups"][0]["roots"][0]["amounts"][0]["amount"],
json!([-500, 1])
);
fn a_flipped_group_shows_its_rows_and_total_negated() {
let shown = period().groups[0].displayed().expect("displayed");
assert_eq!(shown.total, vec![usd(500)]);
assert_eq!(shown.roots[0].amounts, vec![usd(500)]);
fn totals_that_overflow_are_a_report_error() {
let roots = vec![root("A", i64::MAX), root("B", i64::MAX)];
let err = ActivityGroupResult::new("Income".to_string(), false, roots).unwrap_err();
assert!(matches!(err, ReportError::AmountOverflow), "{err:?}");
fn negating_an_i64_min_numerator_is_a_report_error() {
let flipped = ActivityGroupResult::new("Income".to_string(), true, vec![root("A", i64::MIN)])
.expect("a single amount fits");
assert!(matches!(
flipped.displayed(),
Err(ReportError::AmountOverflow)
));
let period = ActivityPeriod::new(None, vec![flipped]).expect("a single total fits");
period.displayed_net(),