fix csv import for money tracker
All checks were successful
Test and deploy staging / staging (push) Successful in 1m44s
All checks were successful
Test and deploy staging / staging (push) Successful in 1m44s
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { cents, csvDate, parseCsv } from './money-tracker';
|
||||
import { cents, csvDate, importCents, parseCsv, parseTransactionImport } from './money-tracker';
|
||||
|
||||
describe('transaction import parsing', () => {
|
||||
it('handles quoted commas and semicolon-separated European amounts', () => {
|
||||
@@ -15,6 +15,51 @@ describe('transaction import parsing', () => {
|
||||
|
||||
it('rejects invalid dates and amounts rather than altering them', () => {
|
||||
expect(csvDate('31/02/2026')).toBeNull();
|
||||
expect(csvDate('Sat 1 Aug 2025')).toBeNull();
|
||||
expect(cents('1.234,56')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('accepts the spreadsheet export with tabs, spacer rows, named dates and euro amounts', () => {
|
||||
const rows = parseCsv(
|
||||
'date\taccount\tdescription\tamount\n' +
|
||||
'Fri 1 Aug 2025\tDaily\tStart balance\t€123.45\n' +
|
||||
'Fri 1 Aug 2025\tBusiness\tStart balance\t-€12.34\n' +
|
||||
'Fri 1 Aug 2025\tShared\tStart balance\t€1,234.56\n' +
|
||||
'\t\t\t\n' +
|
||||
'Sat 2 Aug 2025\tShared\tExample bill\t-€1,234.50\n'
|
||||
);
|
||||
expect(rows).toHaveLength(5);
|
||||
expect(rows[4]).toEqual(['Sat 2 Aug 2025', 'Shared', 'Example bill', '-€1,234.50']);
|
||||
expect(csvDate(rows[1][0])).toBe('2025-08-01');
|
||||
expect(csvDate(rows[4][0])).toBe('2025-08-02');
|
||||
expect(rows.slice(1).map((row) => importCents(row[3]))).toEqual([
|
||||
12345, -1234, 123456, -123450
|
||||
]);
|
||||
expect(
|
||||
parseCsv(
|
||||
'date,account,description,amount\n"Fri 1 Aug 2025",Shared,Start balance,"€1,234.56"'
|
||||
)[1]
|
||||
).toEqual(['Fri 1 Aug 2025', 'Shared', 'Start balance', '€1,234.56']);
|
||||
expect(
|
||||
parseCsv('date,account,description,amount\nFri 1 Aug 2025,Shared,Start balance,€1,234.56')[1]
|
||||
).toHaveLength(5);
|
||||
expect(
|
||||
parseTransactionImport(
|
||||
'date\taccount\tdescription\tamount\n' +
|
||||
'Sat 2 Aug 2025\tShared\tExample bill\t-€1,234.50\n' +
|
||||
'\t\t\t\n' +
|
||||
'Fri 1 Aug 2025\tDaily\tStart balance\t€123.45\n' +
|
||||
'Fri 1 Aug 2025\tBusiness\tStart balance\t-€12.34\n'
|
||||
).map((row) => [row.date, row.account, row.amountCents])
|
||||
).toEqual([
|
||||
['2025-08-01', 'Daily', 12345],
|
||||
['2025-08-01', 'Business', -1234],
|
||||
['2025-08-02', 'Shared', -123450]
|
||||
]);
|
||||
expect(() =>
|
||||
parseTransactionImport(
|
||||
'date,account,description,amount\nFri 1 Aug 2025,Shared,Start balance,€1,234.56'
|
||||
)
|
||||
).toThrow('Quote values containing commas');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user