test(import): a bank file masks a tax id's shape and names its line; a chart file refuses, naming row and column
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
98b1adb754
commit
fcc9220de7
3 files changed
+101
-2
No files matched your search
@@ -217,7 +217,7 @@ fn parse_rows(
|
||||
if amount.is_zero() {
|
||||
continue; // zero-amount rows carry no money
|
||||
}
|
||||
let line = record.position().map_or(idx as u64 + 1, |p| p.line());
|
||||
let line = crate::line_of(text, &record, idx + 1);
|
||||
rows.push((line, fields[0].to_string(), fields[1].to_string(), amount));
|
||||
}
|
||||
let order = date_order(rows.iter().map(|r| r.1.as_str()), order)?;
|
||||
@@ -351,6 +351,57 @@ mod tests {
|
||||
assert_eq!(txns[0].amount, Decimal::new(-123456, 2));
|
||||
}
|
||||
|
||||
/// parse_bank_csv_masked SCENARIOS 1 and 2, INVARIANTS 1 and 2: a bank's
|
||||
/// reference is masked to its last four and named by line; a long
|
||||
/// reference stays; amounts and dates are unchanged.
|
||||
#[test]
|
||||
fn a_tax_id_shape_in_a_description_is_masked_and_named() {
|
||||
let csv = "Date,Description,Amount\n\
|
||||
2026-01-02,ACH 000-00-0000 payroll,-12.50\n\
|
||||
2026-01-03,CHECK 1234567890,-3.25\n\
|
||||
\n\
|
||||
2026-01-04,\"WIRE 000000000, REF 00-0000000\",40.00\n";
|
||||
let got = parse_bank_csv_masked(csv, None).unwrap();
|
||||
let plain = parse_bank_csv_with(csv, None).unwrap();
|
||||
assert_eq!(got.transactions.len(), 3);
|
||||
assert_eq!(
|
||||
got.transactions[0].description,
|
||||
"ACH \u{2022}\u{2022}\u{2022}-\u{2022}\u{2022}-0000 payroll"
|
||||
);
|
||||
assert_eq!(got.transactions[1].description, "CHECK 1234567890");
|
||||
assert_eq!(
|
||||
got.transactions[2].description,
|
||||
"WIRE \u{2022}\u{2022}\u{2022}\u{2022}\u{2022}0000, REF \u{2022}\u{2022}-\u{2022}\u{2022}\u{2022}0000"
|
||||
);
|
||||
for (m, p) in got.transactions.iter().zip(&plain) {
|
||||
assert_eq!((m.date, m.amount), (p.date, p.amount));
|
||||
}
|
||||
// The file's lines, the blank one counted: 2, and 5.
|
||||
assert_eq!(
|
||||
got.masked,
|
||||
vec![
|
||||
MaskedCell {
|
||||
line: 2,
|
||||
column: "Description",
|
||||
numbers: 1
|
||||
},
|
||||
MaskedCell {
|
||||
line: 5,
|
||||
column: "Description",
|
||||
numbers: 2
|
||||
},
|
||||
]
|
||||
);
|
||||
assert!(
|
||||
got.transactions
|
||||
.iter()
|
||||
.all(|t| { !ledger_core::settings::holds_printed_tin(&t.description) })
|
||||
);
|
||||
// A file with none masks none.
|
||||
let clean = parse_bank_csv_masked("2026-01-02,Rent,-1.00\n", None).unwrap();
|
||||
assert!(clean.masked.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_column_count_rejected() {
|
||||
let csv = "a,b\n";
|
||||
|
||||
@@ -121,7 +121,7 @@ pub fn parse_chart_csv(text: &str) -> Result<Vec<ChartRow>, ChartCsvError> {
|
||||
// in any cell is refused, naming the row and the column; the name
|
||||
// is read as strictly as an account name is (any run of nine
|
||||
// digits).
|
||||
let line = record.position().map_or(idx as u64 + 1, |p| p.line());
|
||||
let line = crate::line_of(text, &record, idx + 1);
|
||||
let columns = ["code", "name", "type", "parent_code", "tax_category"];
|
||||
for (n, value) in fields.iter().enumerate() {
|
||||
let looks = if n == 1 {
|
||||
@@ -200,6 +200,37 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
/// INVARIANT 4: a cell that looks like a tax id refuses the file, naming
|
||||
/// its row and column and never the number; a ten-digit code goes through
|
||||
/// (a name is read strictly, as an account name is).
|
||||
#[test]
|
||||
fn a_tax_id_in_a_cell_is_refused_with_its_row_and_column() {
|
||||
let header = "code,name,type,parent_code\n";
|
||||
let why = |rows: &str| match parse_chart_csv(&format!("{header}{rows}")) {
|
||||
Err(ChartCsvError::LooksLikeTin(why)) => why,
|
||||
other => panic!("expected a refusal, got {other:?}"),
|
||||
};
|
||||
let w = why("1000,Cash,asset,\n1010,Payroll 000-00-0000,asset,1000\n");
|
||||
assert!(
|
||||
w.starts_with("the name cell of row 3 looks like it holds a tax id"),
|
||||
"{w}"
|
||||
);
|
||||
assert!(w.contains("books.set_tin") && w.contains("W-9"), "{w}");
|
||||
assert!(!w.contains("0000"), "never the number: {w}");
|
||||
// A name is read strictly, as an account name is: any nine digits.
|
||||
assert!(why("1000,Card 1234 56789,asset,\n").contains("name cell of row 2"));
|
||||
// Another column is read by its shape.
|
||||
assert!(why("000-00-0000,Cash,asset,\n").contains("code cell of row 2"));
|
||||
assert!(why("1000,Cash,asset,00-0000000\n").contains("parent_code cell of row 2"));
|
||||
// A blank line counts as a line of the file.
|
||||
assert!(why("1000,Cash,asset,\n\n1010,Bad 000000000,asset,\n").contains("row 4"));
|
||||
// What is no tax id goes through.
|
||||
let ok = parse_chart_csv(&format!(
|
||||
"{header}1234567890,Loan 2026,liability,\n"
|
||||
));
|
||||
assert_eq!(ok.unwrap().len(), 1);
|
||||
}
|
||||
|
||||
/// Found by fuzz_import_csv (2026-10-13): a row with a blank code or
|
||||
/// name was accepted.
|
||||
#[test]
|
||||
|
||||
@@ -29,6 +29,23 @@ pub mod templates;
|
||||
pub mod workpaper;
|
||||
pub mod xlsx;
|
||||
|
||||
/// The line of `text` (the first is 1) a CSV record starts on, counted from
|
||||
/// its byte offset: the csv reader reports a record's position from where
|
||||
/// it began looking, before it skipped any blank lines, and a row is named
|
||||
/// by the line a person sees it on.
|
||||
pub(crate) fn line_of(text: &str, record: &csv::StringRecord, fallback: usize) -> u64 {
|
||||
record.position().map_or(fallback as u64, |p| {
|
||||
let bytes = text.as_bytes();
|
||||
let mut at = usize::try_from(p.byte())
|
||||
.unwrap_or(usize::MAX)
|
||||
.min(bytes.len());
|
||||
while bytes.get(at).is_some_and(|b| matches!(b, b'\n' | b'\r')) {
|
||||
at += 1;
|
||||
}
|
||||
bytes[..at].iter().filter(|b| **b == b'\n').count() as u64 + 1
|
||||
})
|
||||
}
|
||||
|
||||
pub use asset_register::{asset_register_pdf, asset_register_xlsx};
|
||||
pub use bank_csv::{
|
||||
BankCsvError, BankTransaction, DateOrder, MaskedBankImport, MaskedCell, parse_bank_csv,
|
||||
|
||||
Reference in new issue
Block a user