feat(core): new schedules round to the currency's minor unit (D-72)
The owner's decision D-72: a new monthly-even schedule rounds each month
to its currency's minor unit (yen whole, dinar to the thousandth), where
the cent rule gave two places to every currency; a schedule written
earlier replays under the cent rule, so nothing journaled moves
(BS-prepaid-4, BS-entry-8).
The rule is the schedule's own flag, journaled on its schedule.registered
line ("rounding": "minor_unit", absent for the cent rule), not the
sequence position of the journal feature: a flag survives a snapshot, a
copy and a replay that starts mid-journal. The new feature
schedule_minor_unit gates it (KNOWN_FEATURES, ensure_feature in
Store::register_schedule): books that haven't declared it keep the cent
rule and write nothing new, and an older reader stops at the declaration
as newer books (BS-format-1); an unknown rounding is newer, never
corrupt. A dollar schedule plans the same under both rules.
Tests: a dinar schedule written before the feature replays to the same
plan and the same posted line after the owner's upgrade, while one written
after plans 0.333, 0.333, 0.334. WB-prepaid-2 becomes yes.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
768ed7d92b
commit
09308d5528
5 files changed
+449
-35
No files matched your search
@@ -8,4 +8,4 @@
|
||||
- After the upgrade an older version of wanderbooks can't open these books; the backup made first keeps them as they were. Going back is the owner's choice, never yours.
|
||||
- This upgrade is `documents_v2`: invoice numbers, terms and line accounts (S4), sent records (S5), payments that settle several documents with early-payment discounts and a processor's fee (S6), and write-offs (S7). Until it is given, those writes are refused and everything else works as before.
|
||||
|
||||
- The journal features an upgrade can add: `members_cumulative`, `account_renamed`, `require_tax_category`, `tax_categories_gain_exempt`, `disposal_before_service`, `depreciation_catch_up`, `party_creditors`, `payment_instruments`, `vendor_1099_decisions`, `schedule_convention`, `month_reposting`, `asset_basis`, `projects`, `document_line_projects`, `document_standing`, `documents_v2`, `attachment_text` (P6: a filed file's extracted text, what a human annotated of it, and the entries a payment settles), `drafts_v2` (P4: a draft's state, awaiting its evidence, and a discarded draft), and `source_revisions` (P5: correcting who decided a convention, a decision or an open item, and when). All are declared at once; a version that doesn't know one stops at the declaration instead of misreading the books.
|
||||
- The journal features an upgrade can add: `members_cumulative`, `account_renamed`, `require_tax_category`, `tax_categories_gain_exempt`, `disposal_before_service`, `depreciation_catch_up`, `party_creditors`, `payment_instruments`, `vendor_1099_decisions`, `schedule_convention`, `schedule_minor_unit` (K-U5: a new equal-months schedule's months are whole units of its currency, whole yen or thousandths of a dinar, where the cent rule gave two places for every currency; schedules written before keep their rule), `month_reposting`, `asset_basis`, `projects`, `document_line_projects`, `document_standing`, `documents_v2`, `attachment_text` (P6: a filed file's extracted text, what a human annotated of it, and the entries a payment settles), `drafts_v2` (P4: a draft's state, awaiting its evidence, and a discarded draft), and `source_revisions` (P5: correcting who decided a convention, a decision or an open item, and when). All are declared at once; a version that doesn't know one stops at the declaration instead of misreading the books.
|
||||
@@ -55,6 +55,7 @@ pub const KNOWN_FEATURES: &[&str] = &[
|
||||
VENDOR_1099_DECISIONS,
|
||||
MONTH_REPOSTING,
|
||||
SCHEDULE_CONVENTION,
|
||||
SCHEDULE_MINOR_UNIT,
|
||||
ASSET_BASIS,
|
||||
TAX_CATEGORIES_GAIN_EXEMPT,
|
||||
DISPOSAL_BEFORE_SERVICE,
|
||||
@@ -167,6 +168,18 @@ pub const MONTH_REPOSTING: &str = "month_reposting";
|
||||
/// months (P3 defect 9, plan/served.md D-11).
|
||||
pub const SCHEDULE_CONVENTION: &str = "schedule_convention";
|
||||
|
||||
/// Feature: `schedule.registered` carries `rounding` (`minor_unit`): a
|
||||
/// monthly_even schedule written after this feature rounds each month to
|
||||
/// its currency's minor unit (a yen schedule in whole yen, a dinar one in
|
||||
/// thousandths); one with no `rounding` is the cent rule, two places for
|
||||
/// every currency, as every schedule before it is (BS-prepaid-4, D-72).
|
||||
/// An older version would read the schedule under the cent rule and
|
||||
/// refuse its first month as a posting that doesn't match its plan, or,
|
||||
/// replaying a month it can derive, post ¥33,333.33 where the books say
|
||||
/// ¥33,333. The flag rides on the schedule, never on where the feature
|
||||
/// was declared: a schedule written before it replays as it was posted.
|
||||
pub const SCHEDULE_MINOR_UNIT: &str = "schedule_minor_unit";
|
||||
|
||||
/// Feature: `asset.basis_set`, a new event type: an asset's basis facts
|
||||
/// with the value at contribution and the §704(c) method (P3, defect 13).
|
||||
pub const ASSET_BASIS: &str = "asset_basis";
|
||||
@@ -1088,6 +1101,43 @@ mod tests {
|
||||
assert_eq!(up.adds(), vec!["the journal feature audits"]);
|
||||
}
|
||||
|
||||
/// The version before K-U5 (every feature but `schedule_minor_unit`)
|
||||
/// stops at the declaration as newer, so it never reads a ¥33,333
|
||||
/// month under the cent rule (and refuses, or posts ¥33,333.33 where
|
||||
/// the books say ¥33,333); and a `schedule.registered` naming a
|
||||
/// rounding this version doesn't know is newer, never corrupt
|
||||
/// (BS-prepaid-4, D-72, BS-format-1). Books that declared every
|
||||
/// earlier feature still wait for it.
|
||||
#[test]
|
||||
fn schedule_minor_unit_is_its_own_feature() {
|
||||
assert!(KNOWN_FEATURES.contains(&SCHEDULE_MINOR_UNIT));
|
||||
let before: Vec<&str> = KNOWN_FEATURES
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|f| *f != SCHEDULE_MINOR_UNIT)
|
||||
.collect();
|
||||
let l = line(
|
||||
FORMAT_SET,
|
||||
json!({"format": 2, "features": [SCHEDULE_CONVENTION, SCHEDULE_MINOR_UNIT]}),
|
||||
);
|
||||
match declared_knowing(&l, &before) {
|
||||
Err(StoreError::NewerJournal { need, .. }) => {
|
||||
assert_eq!(need, "journal features schedule_minor_unit")
|
||||
}
|
||||
other => panic!("expected NewerJournal, got {other:?}"),
|
||||
}
|
||||
let older = JournalFormat {
|
||||
format: 2,
|
||||
features: before.iter().map(|f| f.to_string()).collect(),
|
||||
};
|
||||
assert!(matches!(
|
||||
older.require_feature(SCHEDULE_MINOR_UNIT),
|
||||
Err(StoreError::FormatUpgradePending { .. })
|
||||
));
|
||||
let up = older.upgrade().expect("waits for it");
|
||||
assert_eq!(up.adds(), vec!["the journal feature schedule_minor_unit"]);
|
||||
}
|
||||
|
||||
/// This version's format: the newest it reads, every feature it
|
||||
/// knows, each once, sorted (what new books and an upgrade declare).
|
||||
#[test]
|
||||
|
||||
@@ -4961,31 +4961,68 @@ impl Store {
|
||||
/// nothing, in books that haven't declared it): an older version reads
|
||||
/// every schedule as equal months, and would refuse a daily one's
|
||||
/// postings as wrong (rather than as newer).
|
||||
///
|
||||
/// A monthly_even schedule's months round to its currency's minor
|
||||
/// unit in books that declared `schedule_minor_unit` (D-72,
|
||||
/// BS-prepaid-4), which the journal records as `"rounding":
|
||||
/// "minor_unit"` on the schedule itself; in books that haven't it is
|
||||
/// the cent rule, as before, and nothing new is written.
|
||||
///
|
||||
/// ```text
|
||||
/// GOAL: a new monthly-even schedule's months are whole units of its
|
||||
/// currency, and what an earlier version journaled never moves.
|
||||
/// SOURCE: BS-prepaid-4, BS-format-1, BS-entry-8; D-72 (owner,
|
||||
/// 2026-11-08).
|
||||
/// INVARIANTS:
|
||||
/// 1. The rule is a fact of the books when the schedule is written:
|
||||
/// minor unit exactly when the books declared the feature and the
|
||||
/// convention is monthly_even, and the flag is journaled with the
|
||||
/// schedule, so replay never consults the feature's position.
|
||||
/// 2. A schedule journaling the flag requires the feature
|
||||
/// (`ensure_feature`): an older version stops at the declaration,
|
||||
/// never at the schedule.
|
||||
/// 3. Books without the feature write exactly the line they always
|
||||
/// did: no `rounding` key.
|
||||
/// SCENARIOS:
|
||||
/// 1. IF the books declared the feature and ¥100,000 is spread over
|
||||
/// 3 equal months: SHOULD plan ¥33,333, ¥33,333, ¥33,334.
|
||||
/// 2. IF they haven't: SHOULD refuse it as a month finer than a yen
|
||||
/// (¥33,333.33), as before, until the owner's upgrade.
|
||||
/// ```
|
||||
pub fn register_schedule(
|
||||
&mut self,
|
||||
new: ledger_core::NewSchedule,
|
||||
) -> Result<ledger_core::ScheduleId, StoreError> {
|
||||
self.rehearse(|l| l.register_schedule(new.clone()))?;
|
||||
ledger_core::schedules::validate_schedule(&new, self.ledger.chart())?;
|
||||
let rounding = if new.convention == ledger_core::ScheduleConvention::MonthlyEven
|
||||
&& self.format.has_feature(crate::format::SCHEDULE_MINOR_UNIT)
|
||||
{
|
||||
ledger_core::ScheduleRounding::MinorUnit
|
||||
} else {
|
||||
ledger_core::ScheduleRounding::Cent
|
||||
};
|
||||
self.rehearse(|l| l.register_schedule_under(new.clone(), rounding))?;
|
||||
ledger_core::schedules::validate_schedule_under(&new, rounding, self.ledger.chart())?;
|
||||
let id = self.ledger.next_schedule_id();
|
||||
if new.convention == ledger_core::ScheduleConvention::Daily {
|
||||
self.ensure_feature(crate::format::SCHEDULE_CONVENTION)?;
|
||||
}
|
||||
let line = self.append_journal(
|
||||
"schedule.registered",
|
||||
json!({
|
||||
"id": id,
|
||||
"description": new.description,
|
||||
"kind": serde_json::to_value(new.kind)?,
|
||||
"total": {"amount": new.total.amount.to_string(), "currency": new.total.currency.code()},
|
||||
"start": new.start.to_string(),
|
||||
"months": new.months,
|
||||
"balance_account": new.balance_account,
|
||||
"pl_account": new.pl_account,
|
||||
"convention": new.convention.key(),
|
||||
}),
|
||||
)?;
|
||||
let got = self.ledger.register_schedule(new)?;
|
||||
let mut payload = json!({
|
||||
"id": id,
|
||||
"description": new.description,
|
||||
"kind": serde_json::to_value(new.kind)?,
|
||||
"total": {"amount": new.total.amount.to_string(), "currency": new.total.currency.code()},
|
||||
"start": new.start.to_string(),
|
||||
"months": new.months,
|
||||
"balance_account": new.balance_account,
|
||||
"pl_account": new.pl_account,
|
||||
"convention": new.convention.key(),
|
||||
});
|
||||
if !rounding.is_cent() {
|
||||
self.ensure_feature(crate::format::SCHEDULE_MINOR_UNIT)?;
|
||||
payload["rounding"] = json!(rounding.key());
|
||||
}
|
||||
let line = self.append_journal("schedule.registered", payload)?;
|
||||
let got = self.ledger.register_schedule_under(new, rounding)?;
|
||||
debug_assert_eq!(got, id);
|
||||
apply_line_to_sqlite(&self.conn, &line)?;
|
||||
Ok(id)
|
||||
@@ -7414,16 +7451,36 @@ fn replay_line(
|
||||
));
|
||||
}
|
||||
};
|
||||
let got = ledger.replay_register_schedule(ledger_core::NewSchedule {
|
||||
description: get_str(p, "description")?.to_string(),
|
||||
kind: serde_json::from_value(p["kind"].clone())?,
|
||||
total: parse_money(p, "total")?,
|
||||
start: parse_date(get_str(p, "start")?)?,
|
||||
months: get_u64(p, "months")? as u32,
|
||||
balance_account: get_u64(p, "balance_account")?,
|
||||
pl_account: get_u64(p, "pl_account")?,
|
||||
convention,
|
||||
})?;
|
||||
// No rounding (every schedule before D-72): the cent rule, as
|
||||
// it was posted. The rule is the schedule's own, never the
|
||||
// books' later format. A rule this version doesn't know is
|
||||
// newer, never corrupt (BS-format-1).
|
||||
let rounding = match p.get("rounding") {
|
||||
None | Some(Value::Null) => ledger_core::ScheduleRounding::Cent,
|
||||
Some(Value::String(r)) => {
|
||||
ledger_core::ScheduleRounding::parse(r).ok_or_else(|| {
|
||||
crate::format::newer_content(line, format!("the schedule rounding {r}"))
|
||||
})?
|
||||
}
|
||||
Some(_) => {
|
||||
return Err(StoreError::JournalCorrupt(
|
||||
"schedule.registered rounding".into(),
|
||||
));
|
||||
}
|
||||
};
|
||||
let got = ledger.replay_register_schedule_under(
|
||||
ledger_core::NewSchedule {
|
||||
description: get_str(p, "description")?.to_string(),
|
||||
kind: serde_json::from_value(p["kind"].clone())?,
|
||||
total: parse_money(p, "total")?,
|
||||
start: parse_date(get_str(p, "start")?)?,
|
||||
months: get_u64(p, "months")? as u32,
|
||||
balance_account: get_u64(p, "balance_account")?,
|
||||
pl_account: get_u64(p, "pl_account")?,
|
||||
convention,
|
||||
},
|
||||
rounding,
|
||||
)?;
|
||||
if got != want {
|
||||
return Err(StoreError::ReplayMismatch(format!(
|
||||
"schedule id: journal says {want}, replay got {got}"
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
//! A new monthly-even schedule rounds to its currency's minor unit; one
|
||||
//! written before the journal feature `schedule_minor_unit` keeps the
|
||||
//! cent rule on replay (K-U5, BS-prepaid-4, D-72).
|
||||
//!
|
||||
//! ```text
|
||||
//! GOAL: a yen schedule's months are whole yen, and what an earlier
|
||||
//! version journaled never moves.
|
||||
//! SOURCE: BS-prepaid-4, BS-format-1, BS-entry-8; plan/served.md D-72
|
||||
//! (owner, 2026-11-08).
|
||||
//! INVARIANTS:
|
||||
//! 1. In books that declared the feature, a monthly_even schedule's
|
||||
//! line carries `"rounding": "minor_unit"` and its months are whole
|
||||
//! units of its currency.
|
||||
//! 2. In books that haven't, nothing new is written: the line has no
|
||||
//! `rounding`, the plan is the cent rule's, and a month finer than
|
||||
//! the currency's unit is refused as before.
|
||||
//! 3. A schedule's rule is its own: books upgraded after a cent-rule
|
||||
//! schedule was written replay it, and post its months, under the
|
||||
//! cent rule.
|
||||
//! 4. A reader that doesn't know the feature stops at its declaration
|
||||
//! as newer books (format.rs's schedule_minor_unit_is_its_own_feature).
|
||||
//! ```
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use ledger_core::{
|
||||
AccountType, Currency, Money, NewSchedule, ScheduleConvention, ScheduleKind, ScheduleRounding,
|
||||
};
|
||||
use ledger_store::format::{SCHEDULE_MINOR_UNIT, current};
|
||||
use ledger_store::{JOURNAL_FILE, Store, StoreError};
|
||||
use rust_decimal::Decimal;
|
||||
use serde_json::Value;
|
||||
use std::path::Path;
|
||||
|
||||
fn d(y: i32, m: u32, day: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(y, m, day).unwrap()
|
||||
}
|
||||
|
||||
fn kwd() -> Currency {
|
||||
Currency::new("KWD").unwrap()
|
||||
}
|
||||
|
||||
fn amount(s: &str, c: Currency) -> Money {
|
||||
Money::new(s.parse::<Decimal>().unwrap(), c)
|
||||
}
|
||||
|
||||
fn plan_text(store: &Store, id: u64) -> Vec<String> {
|
||||
store
|
||||
.ledger()
|
||||
.get_schedule(id)
|
||||
.unwrap()
|
||||
.plan()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|(_, a)| a.to_string())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The journal's `schedule.registered` payloads, in order.
|
||||
fn registered(dir: &Path) -> Vec<Value> {
|
||||
std::fs::read_to_string(dir.join(JOURNAL_FILE))
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map(|l| serde_json::from_str::<Value>(l).unwrap())
|
||||
.filter(|l| l["type"] == "schedule.registered")
|
||||
.map(|l| l["payload"].clone())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn books(dir: &Path, currency: Currency, with_feature: bool) -> (Store, u64, u64) {
|
||||
let mut format = current();
|
||||
if !with_feature {
|
||||
format.features.retain(|f| f != SCHEDULE_MINOR_UNIT);
|
||||
}
|
||||
let mut s = Store::create_in_format(dir, currency, &format).unwrap();
|
||||
let prepaid = s
|
||||
.add_account("1300", "Prepaid", AccountType::Asset, None, false)
|
||||
.unwrap();
|
||||
let expense = s
|
||||
.add_account("6300", "Insurance", AccountType::Expense, None, false)
|
||||
.unwrap();
|
||||
(s, prepaid, expense)
|
||||
}
|
||||
|
||||
fn schedule(total: Money, prepaid: u64, expense: u64) -> NewSchedule {
|
||||
NewSchedule {
|
||||
description: "Insurance".into(),
|
||||
kind: ScheduleKind::PrepaidExpense,
|
||||
total,
|
||||
start: d(2027, 1, 1),
|
||||
months: 3,
|
||||
balance_account: prepaid,
|
||||
pl_account: expense,
|
||||
convention: ScheduleConvention::MonthlyEven,
|
||||
}
|
||||
}
|
||||
|
||||
/// INVARIANT 1, by hand: 100,000 yen over 3 is 33,333.33..., to the yen
|
||||
/// 33,333, the last month 100,000 - 66,666 = 33,334; 1.000 dinar is 0.333,
|
||||
/// 0.333, 0.334. The line carries the rule, every month posts whole units,
|
||||
/// and the books replay on reopening (the replay compares each posted
|
||||
/// line as text).
|
||||
#[test]
|
||||
fn new_books_round_a_yen_and_a_dinar_schedule_to_the_unit() {
|
||||
for (currency, total, want) in [
|
||||
(Currency::JPY, "100000", ["33333", "33333", "33334"]),
|
||||
(kwd(), "1.000", ["0.333", "0.333", "0.334"]),
|
||||
] {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let (mut s, prepaid, expense) = books(dir.path(), currency, true);
|
||||
let id = s
|
||||
.register_schedule(schedule(amount(total, currency), prepaid, expense))
|
||||
.unwrap();
|
||||
assert_eq!(plan_text(&s, id), want);
|
||||
assert_eq!(
|
||||
s.ledger().get_schedule(id).unwrap().rounding,
|
||||
ScheduleRounding::MinorUnit
|
||||
);
|
||||
let line = ®istered(dir.path())[0];
|
||||
assert_eq!(line["rounding"], "minor_unit");
|
||||
assert_eq!(line["convention"], "monthly_even");
|
||||
assert_eq!(s.run_schedules(d(2027, 12, 31)).unwrap().len(), 3);
|
||||
s.verify().unwrap();
|
||||
let ledger_before = s.ledger().clone();
|
||||
drop(s);
|
||||
let reopened = Store::open(dir.path()).unwrap();
|
||||
assert_eq!(reopened.ledger(), &ledger_before);
|
||||
assert_eq!(plan_text(&reopened, id), want);
|
||||
reopened.verify().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// A daily schedule is written as it always was: no `rounding`.
|
||||
#[test]
|
||||
fn a_daily_schedule_carries_no_rounding() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let (mut s, prepaid, expense) = books(dir.path(), Currency::JPY, true);
|
||||
s.register_schedule(NewSchedule {
|
||||
convention: ScheduleConvention::Daily,
|
||||
..schedule(amount("100000", Currency::JPY), prepaid, expense)
|
||||
})
|
||||
.unwrap();
|
||||
assert!(registered(dir.path())[0].get("rounding").is_none());
|
||||
}
|
||||
|
||||
/// INVARIANT 2: books that haven't declared the feature (waiting for the
|
||||
/// owner's upgrade) keep writing and planning as before: a yen schedule
|
||||
/// whose months aren't whole yen is refused, a schedule that divides
|
||||
/// evenly is taken without a `rounding`, and nothing journals the feature
|
||||
/// uninvited.
|
||||
#[test]
|
||||
fn books_without_the_feature_keep_the_cent_rule_and_write_nothing_new() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let (mut s, prepaid, expense) = books(dir.path(), Currency::JPY, false);
|
||||
let head = s.head();
|
||||
match s.register_schedule(schedule(amount("1000", Currency::JPY), prepaid, expense)) {
|
||||
Err(StoreError::Ledger(e)) => assert!(e.to_string().contains("333.33"), "{e}"),
|
||||
other => panic!("expected a refusal, got {other:?}"),
|
||||
}
|
||||
assert_eq!(s.head(), head, "a refusal journals nothing");
|
||||
let id = s
|
||||
.register_schedule(schedule(amount("900", Currency::JPY), prepaid, expense))
|
||||
.unwrap();
|
||||
assert_eq!(plan_text(&s, id), ["300", "300", "300"]);
|
||||
assert_eq!(
|
||||
s.ledger().get_schedule(id).unwrap().rounding,
|
||||
ScheduleRounding::Cent
|
||||
);
|
||||
assert!(registered(dir.path())[0].get("rounding").is_none());
|
||||
}
|
||||
|
||||
/// INVARIANT 3 and the replay test: a dinar schedule written before the
|
||||
/// feature plans 0.33, 0.33, 0.340 (the cent rule; each a valid dinar
|
||||
/// amount) and posts its first month at that figure. After the owner's
|
||||
/// upgrade, the same books replay to the same plan and the same posted
|
||||
/// line; the old schedule still posts its other months at the cent rule's
|
||||
/// figures, while a schedule written after the upgrade gets 0.333, 0.333,
|
||||
/// 0.334.
|
||||
#[test]
|
||||
fn a_schedule_written_before_the_feature_replays_to_the_same_plan_after_it() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let (mut s, prepaid, expense) = books(dir.path(), kwd(), false);
|
||||
let old = s
|
||||
.register_schedule(schedule(amount("1.000", kwd()), prepaid, expense))
|
||||
.unwrap();
|
||||
let before = plan_text(&s, old);
|
||||
assert_eq!(before, ["0.33", "0.33", "0.340"]);
|
||||
s.post_schedule_month(old, d(2027, 1, 31)).unwrap();
|
||||
let first_month = s
|
||||
.ledger()
|
||||
.posted_entries()
|
||||
.last()
|
||||
.unwrap()
|
||||
.lines()
|
||||
.iter()
|
||||
.map(|l| l.amount.amount.to_string())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(first_month, ["0.33", "0.33"]);
|
||||
|
||||
assert!(s.upgrade_format().unwrap().is_some());
|
||||
assert!(s.journal_format().has_feature(SCHEDULE_MINOR_UNIT));
|
||||
assert_eq!(plan_text(&s, old), before, "the upgrade moves no plan");
|
||||
drop(s);
|
||||
|
||||
// Replay from the journal, as a fresh process does.
|
||||
let mut s = Store::open(dir.path()).unwrap();
|
||||
s.verify().unwrap();
|
||||
let schedule_old = s.ledger().get_schedule(old).unwrap();
|
||||
assert_eq!(schedule_old.rounding, ScheduleRounding::Cent);
|
||||
assert_eq!(plan_text(&s, old), before, "replay keeps the cent rule");
|
||||
let replayed = s
|
||||
.ledger()
|
||||
.posted_entries()
|
||||
.last()
|
||||
.unwrap()
|
||||
.lines()
|
||||
.iter()
|
||||
.map(|l| l.amount.amount.to_string())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(replayed, first_month, "the posted line is as it was");
|
||||
|
||||
// The rest of the old schedule posts at the cent rule's figures, and a
|
||||
// new one, in the same books, at the dinar's.
|
||||
s.post_schedule_month(old, d(2027, 2, 28)).unwrap();
|
||||
s.post_schedule_month(old, d(2027, 3, 31)).unwrap();
|
||||
let new = s
|
||||
.register_schedule(schedule(amount("1.000", kwd()), prepaid, expense))
|
||||
.unwrap();
|
||||
assert_eq!(plan_text(&s, new), ["0.333", "0.333", "0.334"]);
|
||||
assert_eq!(
|
||||
s.ledger().get_schedule(new).unwrap().rounding,
|
||||
ScheduleRounding::MinorUnit
|
||||
);
|
||||
let lines = registered(dir.path());
|
||||
assert!(lines[0].get("rounding").is_none());
|
||||
assert_eq!(lines[1]["rounding"], "minor_unit");
|
||||
s.verify().unwrap();
|
||||
drop(s);
|
||||
let again = Store::open(dir.path()).unwrap();
|
||||
assert_eq!(plan_text(&again, old), before);
|
||||
assert_eq!(plan_text(&again, new), ["0.333", "0.333", "0.334"]);
|
||||
again.verify().unwrap();
|
||||
}
|
||||
|
||||
/// A US-dollar schedule is the same under both rules, in every book:
|
||||
/// written before or after the feature, it plans the same figures.
|
||||
#[test]
|
||||
fn a_dollar_schedule_plans_the_same_in_old_and_new_books() {
|
||||
let plan_in = |with_feature: bool| {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let (mut s, prepaid, expense) = books(dir.path(), Currency::USD, with_feature);
|
||||
let id = s
|
||||
.register_schedule(schedule(amount("1000.00", Currency::USD), prepaid, expense))
|
||||
.unwrap();
|
||||
plan_text(&s, id)
|
||||
};
|
||||
assert_eq!(plan_in(false), ["333.33", "333.33", "333.34"]);
|
||||
assert_eq!(plan_in(true), plan_in(false));
|
||||
}
|
||||
|
||||
/// A `schedule.registered` naming a rounding this version doesn't know is
|
||||
/// newer books, never corrupt (BS-format-1): the line is whole and
|
||||
/// chained, and this reader says it is old.
|
||||
#[test]
|
||||
fn an_unknown_rounding_is_newer_not_corrupt() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let (s, prepaid, expense) = books(dir.path(), Currency::USD, true);
|
||||
drop(s);
|
||||
{
|
||||
let mut w = ledger_store::JournalWriter::open(&dir.path().join(JOURNAL_FILE)).unwrap();
|
||||
w.append(
|
||||
"schedule.registered",
|
||||
serde_json::json!({
|
||||
"id": 1, "description": "x", "kind": "prepaid_expense",
|
||||
"total": {"amount": "1000.00", "currency": "USD"},
|
||||
"start": "2027-01-01", "months": 3,
|
||||
"balance_account": prepaid, "pl_account": expense,
|
||||
"convention": "monthly_even", "rounding": "to_the_nickel"
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
match Store::open(dir.path()) {
|
||||
Err(StoreError::NewerJournal { need, .. }) => {
|
||||
assert!(need.contains("to_the_nickel"), "{need}")
|
||||
}
|
||||
other => panic!("expected NewerJournal, got {:?}", other.map(|_| ())),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user