From 742b9fa46d74762f58ae939afd980a532cc4636f Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Tue, 6 Oct 2020 14:13:14 +0300 Subject: Account for days elapsed based on latest date Instead of counting the number of days for the averge through the number of iterations, let it be the difference between the latest date on entry and the start of the period. Id est: * 'Missing' dates from the input are implicit. * The order of the days doesn't affect the output (although it doesn't make sense to put the days out of order). --- budget/tests/budget.rs | 29 +++++++++++++++-------------- budget/tests/test.toml | 6 +++--- 2 files changed, 18 insertions(+), 17 deletions(-) (limited to 'budget/tests') diff --git a/budget/tests/budget.rs b/budget/tests/budget.rs index 1cc02e7..feae240 100644 --- a/budget/tests/budget.rs +++ b/budget/tests/budget.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use chrono::prelude::*; +use chrono::NaiveDate; use budget::*; @@ -61,6 +61,10 @@ fn can_parse_account() -> Result<(), ParseError>{ }, ], }, + Day { + date: NaiveDate::from_ymd(2020, 10, 4), + expenses: Vec::::new(), + }, Day { date: NaiveDate::from_ymd(2020, 10, 2), expenses: vec![ @@ -82,10 +86,6 @@ fn can_parse_account() -> Result<(), ParseError>{ }, ], }, - Day { - date: NaiveDate::from_ymd(2020, 10, 3), - expenses: Vec::::new(), - }, ], }; @@ -99,32 +99,33 @@ fn can_parse_account() -> Result<(), ParseError>{ #[test] fn can_calculate() -> Result<(), ParseError> { let mut should_be = Calculated { - all_day_average: 7.57, - essential_day_average: 6.3, + all_day_average: 5.6775, + essential_day_average: 4.725, categories_day_average: HashMap::::new(), essential_subtotal: 18.9, categories_subtotal: HashMap::::new(), total: 22.71, balance: 397.29, - days_left: 52.48216644649934, - days_left_essential: 63.06190476190476, + days_left: 69.9762219286658, + days_left_essential: 84.08253968253969, + last_day: NaiveDate::from_ymd(2020, 10, 04), }; should_be.categories_day_average.insert( "supplies".to_string(), - 1.27, + 0.9525, ); should_be.categories_day_average.insert( "products".to_string(), - 2.3333333333333335, + 1.75, ); should_be.categories_day_average.insert( "transport".to_string(), - 2.3000000000000003, + 1.725, ); should_be.categories_day_average.insert( "utilities".to_string(), - 1.6666666666666667, + 1.25, ); should_be.categories_subtotal.insert( @@ -145,7 +146,7 @@ fn can_calculate() -> Result<(), ParseError> { ); let account = budget::parse_account("tests/test.toml")?; - let actually_is = budget::calculate(&account); + let actually_is = budget::calculate(&account).unwrap(); assert_eq!(actually_is, should_be); diff --git a/budget/tests/test.toml b/budget/tests/test.toml index 1bb1ce7..a29467e 100644 --- a/budget/tests/test.toml +++ b/budget/tests/test.toml @@ -38,6 +38,9 @@ date = 2020-10-01 category = "products" shared = 2 +[[days]] +date = 2020-10-04 + [[days]] date = 2020-10-02 @@ -52,6 +55,3 @@ date = 2020-10-02 price = 6.9 category = "transport" -[[days]] -date = 2020-10-03 - -- cgit v1.2.3