[ledger] Core: haléře, dates, the event log and apply(), merchant keys and rules, one-to-one matching, import planning
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
//! Mandatory test 1: money never loses or creates a haléř.
|
||||
|
||||
use fiscus_ledger::money::{split, Haler, MoneyError};
|
||||
use proptest::prelude::*;
|
||||
|
||||
const BIG: i64 = 1_000_000_000_000; // ten billion CZK in haléře: far beyond any household
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn split_shares_sum_to_the_total(total in -BIG..BIG, weights in prop::collection::vec(0u64..10_000, 1..12)) {
|
||||
prop_assume!(weights.iter().any(|w| *w > 0));
|
||||
let shares = split(Haler(total), &weights).unwrap();
|
||||
prop_assert_eq!(shares.len(), weights.len());
|
||||
prop_assert_eq!(Haler::sum(shares.iter().copied()).unwrap(), Haler(total));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_zero_weight_gets_nothing(total in -BIG..BIG, n in 1usize..8) {
|
||||
let mut weights = vec![1u64; n];
|
||||
weights.push(0);
|
||||
let shares = split(Haler(total), &weights).unwrap();
|
||||
prop_assert_eq!(*shares.last().unwrap(), Haler(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shares_differ_by_at_most_one_haler_for_equal_weights(total in -BIG..BIG, n in 1usize..12) {
|
||||
let shares = split(Haler(total), &vec![1u64; n]).unwrap();
|
||||
let min = shares.iter().map(|h| h.0).min().unwrap();
|
||||
let max = shares.iter().map(|h| h.0).max().unwrap();
|
||||
prop_assert!(max - min <= 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_remainder_rule_is_deterministic(total in -BIG..BIG, weights in prop::collection::vec(1u64..100, 1..8)) {
|
||||
prop_assert_eq!(split(Haler(total), &weights).unwrap(), split(Haler(total), &weights).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_payment_and_its_refund_cancel_out(amount in 1i64..BIG, refunds in prop::collection::vec(1u64..100, 1..5)) {
|
||||
// A refund split into parts: the parts plus the payment come back to zero exactly.
|
||||
let parts = split(Haler(amount), &refunds).unwrap();
|
||||
let mut all = vec![Haler(-amount)];
|
||||
all.extend(parts);
|
||||
prop_assert_eq!(Haler::sum(all).unwrap(), Haler(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sums_are_order_independent(mut xs in prop::collection::vec(-BIG..BIG, 0..50)) {
|
||||
let a = Haler::sum(xs.iter().copied().map(Haler)).unwrap();
|
||||
xs.reverse();
|
||||
let b = Haler::sum(xs.iter().copied().map(Haler)).unwrap();
|
||||
prop_assert_eq!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn czk_text_round_trips(h in -BIG..BIG) {
|
||||
let text = Haler(h).to_czk();
|
||||
prop_assert_eq!(Haler::parse_czk(&text).unwrap(), Haler(h));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow_is_an_error_never_a_wrap() {
|
||||
assert_eq!(Haler(i64::MAX).checked_add(Haler(1)), Err(MoneyError::Overflow));
|
||||
assert_eq!(Haler(i64::MIN).checked_sub(Haler(1)), Err(MoneyError::Overflow));
|
||||
assert_eq!(Haler(i64::MIN).checked_neg(), Err(MoneyError::Overflow));
|
||||
assert_eq!(Haler::sum([Haler(i64::MAX), Haler(1)]), Err(MoneyError::Overflow));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_rejects_no_weight() {
|
||||
assert_eq!(split(Haler(100), &[]), Err(MoneyError::NoWeight));
|
||||
assert_eq!(split(Haler(100), &[0, 0]), Err(MoneyError::NoWeight));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_known_cases() {
|
||||
// 100,00 Kč in three: the first share takes the leftover haléř.
|
||||
assert_eq!(split(Haler(10_000), &[1, 1, 1]).unwrap(), vec![Haler(3_334), Haler(3_333), Haler(3_333)]);
|
||||
// A refund (negative) splits the same way, mirrored.
|
||||
assert_eq!(split(Haler(-10_000), &[1, 1, 1]).unwrap(), vec![Haler(-3_334), Haler(-3_333), Haler(-3_333)]);
|
||||
// Weighted 2:1.
|
||||
assert_eq!(split(Haler(1_000), &[2, 1]).unwrap(), vec![Haler(667), Haler(333)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn czk_text() {
|
||||
assert_eq!(Haler(-123_450).to_czk(), "-1 234,50");
|
||||
assert_eq!(Haler(5).to_czk(), "0,05");
|
||||
assert_eq!(Haler(100_000_000).to_czk(), "1 000 000,00");
|
||||
assert_eq!(Haler::parse_czk("1234,5").unwrap(), Haler(123_450));
|
||||
assert_eq!(Haler::parse_czk("12").unwrap(), Haler(1_200));
|
||||
assert_eq!(Haler::parse_czk("-0,01").unwrap(), Haler(-1));
|
||||
for bad in ["", "-", "1,234", "1.5.2", "abc", "1,", ",5", "1e3", "١٢", "99999999999999999999"] {
|
||||
assert!(Haler::parse_czk(bad).is_err(), "{bad:?} must be refused");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user