Lines
100 %
Functions
Branches
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use finance::price::Price;
use finance::split::Split;
use finance::tag::Tag;
use sqlx::types::Uuid;
use super::revision;
fn uuid(n: u128) -> Uuid {
Uuid::from_u128(n)
}
fn at(text: &str) -> DateTime<Utc> {
DateTime::parse_from_rfc3339(text)
.expect("valid instant")
.with_timezone(&Utc)
fn tag(name: &str, value: &str, description: Option<&str>) -> Tag {
Tag {
id: Uuid::new_v4(),
tag_name: name.to_string(),
tag_value: value.to_string(),
description: description.map(str::to_string),
fn split(n: u128, account: u128, commodity: u128, value: i64) -> Split {
Split {
id: uuid(n),
tx_id: uuid(99),
account_id: uuid(account),
commodity_id: uuid(commodity),
reconcile_state: None,
reconcile_date: None,
value_num: value,
value_denom: 1,
lot_id: None,
type Reconcile = (Option<bool>, Option<&'static str>, Option<u128>);
fn price(n: u128, rate: i64, date: &str, from: u128, to: u128) -> Price {
Price {
date: at(date),
commodity_id: uuid(5),
currency_id: uuid(4),
commodity_split: Some(uuid(to)),
currency_split: Some(uuid(from)),
value_num: rate,
value_denom: 9,
type TagText = (&'static str, &'static str, Option<&'static str>);
#[derive(Clone)]
struct State {
date: &'static str,
note: Option<&'static str>,
accounts: [u128; 2],
commodity: u128,
value: i64,
tags: Vec<TagText>,
transaction_tags: Vec<TagText>,
rate: i64,
price_date: &'static str,
links: (u128, u128),
reconcile: Reconcile,
fn base() -> State {
State {
date: "2026-10-27T12:00:00Z",
note: Some("n"),
accounts: [11, 12],
commodity: 4,
value: 10,
tags: vec![("memo", "m", Some("d")), ("k", "v", None)],
transaction_tags: vec![("trip", "japan", Some("spring")), ("cleared", "yes", None)],
rate: 10,
price_date: "2026-10-20T10:11:12.345678Z",
links: (1, 2),
reconcile: (Some(false), Some("2026-10-28T09:00:00.5Z"), Some(30)),
fn tags_of(texts: &[TagText]) -> Vec<Tag> {
texts
.iter()
.map(|(name, value, description)| tag(name, value, *description))
.collect()
fn digest(state: &State, reverse: bool) -> String {
let (reconcile_state, reconcile_date, lot) = state.reconcile;
let mut splits = vec![
reconcile_state,
reconcile_date: reconcile_date.map(at),
lot_id: lot.map(uuid),
..split(1, state.accounts[0], state.commodity, -state.value)
},
split(2, state.accounts[1], 5, 9),
];
let mut tags = tags_of(&state.tags);
let mut transaction_tags = tags_of(&state.transaction_tags);
let mut prices = vec![
price(
7,
state.rate,
state.price_date,
state.links.0,
state.links.1,
),
price(8, 3, "2026-10-01T00:00:00Z", 1, 2),
if reverse {
splits.reverse();
tags.reverse();
transaction_tags.reverse();
prices.reverse();
let split_tags = HashMap::from([(uuid(1), tags)]);
revision(
at(state.date),
state.note,
&transaction_tags,
&splits,
&split_tags,
&prices,
)
#[test]
fn each_covered_field_changes_the_digest() {
let base = base();
let original = digest(&base, false);
let changes: Vec<(&str, State)> = vec![
(
"date",
date: "2026-10-27T12:00:01Z",
..base.clone()
"note",
note: Some("m"),
"note removed",
note: None,
"account",
accounts: [11, 13],
"commodity",
commodity: 6,
"value",
value: 11,
"tag name",
tags: vec![("memo2", "m", Some("d")), ("k", "v", None)],
"tag value",
tags: vec![("memo", "m2", Some("d")), ("k", "v", None)],
"tag description",
tags: vec![("memo", "m", None), ("k", "v", None)],
"tag removed",
tags: vec![("k", "v", None)],
"transaction tag value",
transaction_tags: vec![("trip", "korea", Some("spring")), ("cleared", "yes", None)],
"transaction tag description",
transaction_tags: vec![("trip", "japan", None), ("cleared", "yes", None)],
"transaction tag removed",
transaction_tags: vec![("cleared", "yes", None)],
"split tag moved to the transaction",
transaction_tags: vec![
("trip", "japan", Some("spring")),
("cleared", "yes", None),
("memo", "m", Some("d")),
],
"reconcile state",
reconcile: (Some(true), base.reconcile.1, base.reconcile.2),
"reconcile state cleared",
reconcile: (None, base.reconcile.1, base.reconcile.2),
"reconcile date",
reconcile: (
base.reconcile.0,
Some("2026-10-28T09:00:00.500001Z"),
base.reconcile.2,
"reconcile date cleared",
reconcile: (base.reconcile.0, None, base.reconcile.2),
"lot",
reconcile: (base.reconcile.0, base.reconcile.1, Some(31)),
"lot cleared",
reconcile: (base.reconcile.0, base.reconcile.1, None),
"rate",
rate: 11,
"price date",
price_date: "2026-10-20T10:11:12.345679Z",
"price links",
links: (2, 1),
for (name, changed) in changes {
assert_ne!(digest(&changed, false), original, "{name}");
fn order_of_splits_tags_and_prices_does_not_matter() {
assert_eq!(digest(&base, true), digest(&base, false));
assert_eq!(digest(&base, false).len(), 64);
fn a_padded_note_name_is_the_note_in_the_stored_revision() {
let when = at("2026-03-01T00:00:00Z");
let padded = vec![tag(" note\u{a0}", "lunch", None), tag("k", "v", None)];
let plain = vec![tag("note", "lunch", None), tag("k", "v", None)];
let stored = super::stored_revision(when, padded, &[], &HashMap::new(), &[]);
assert_eq!(
stored,
super::stored_revision(when, plain, &[], &HashMap::new(), &[])
);
when,
Some("lunch"),
&[tag("k", "v", None)],
&[],
&HashMap::new(),
&[]